f



Allow Printing and Assembly on encrypted PDF

I'm using iText to create PDFs.  The Document Restrictions is set to
"Allow Printing = true" and it's not password protected (even though
according to java API that it is) when I try to print the PDFs. As
soon as I set the restriction "Allow Assembly" to true, I need to
provide a password when I try to insert one document into another.  Is
there a way to combine two PDFs but still prevent the user from
modifying the contents of the documents??  The following is the code
that I use to create and encrypt when generating the PDF:

import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class Encrypt extends java.lang.Object {

    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
        if (args.length != 3) {
            System.err.println("This tools needs 3 parameters:\njava
Encrypt srcfile destfile password");
        }
        else {
            try {
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(args[0]);
                // we retrieve the total number of pages
                int n = reader.getNumberOfPages();
                System.out.println("There are " + n + " pages in the
original file.");

                // step 1: creation of a document-object
                Document document = new
Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the
document
                PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(args[1]));
                writer.setEncryption(PdfWriter.STRENGTH128BITS,
args[2], null, PdfWriter.AllowPrinting | PdfWriter.AllowAssembly);
                // step 3: we open the document
                document.open();
                PdfContentByte cb = writer.getDirectContent();
                PdfImportedPage page;
                int rotation;
                int i = 0;
                // step 4: we add content
                while (i < n) {
                    i++;
 
document.setPageSize(reader.getPageSizeWithRotation(i));
                    document.newPage();
                    page = writer.getImportedPage(reader, i);
                    rotation = reader.getPageRotation(i);
                    if (rotation == 90 || rotation == 270) {
                        cb.addTemplate(page, 0, -1f, 1f, 0, 0,
reader.getPageSizeWithRotation(i).height());
                    }
                    else {
                        cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    System.out.println("Processed page " + i);
                }
                // step 5: we close the document
                document.close();
            }
            catch(Exception e) {
                System.err.println(e.getClass().getName() + ": " +
e.getMessage());
            }
        }
    }

}

Thanks.

0
kwtung77 (1)
5/25/2007 11:51:31 PM
comp.text.pdf 5596 articles. 0 followers. ramon (1518) is leader. Post Follow

0 Replies
946 Views

Similar Articles

[PageSpeed] 51

Reply: