Hi there,
 
I need to find the bit of code that will enable the ability for
combining PDF files, but won't allow users to modify the contents of the
files.  From the code below, I added "PdfWriter.AllowAssembly"  which
changes the Document Security for "Document Assmebly" = Allowed.  I
created two PDFs with this setting (Allow Printing and Assembly) but as
soon as I open one of the PDFs, and click "Insert", a dialog prompts me
for a password.  Is there a way to enable this functionality without
triggering a password protection???  I noticed that before I added
"PdfWriter.AllowAssembly", I was able to print the PDF without
triggering the password protection, even though from the java API, it
says:
 
AllowPrinting
<http://www.jdocs.com/itext/1.02/api/com/lowagie/text/pdf/PdfWriter.html
#AllowPrinting>  
          The operation permitted when the document is opened with the
user password
 
Why is it different for "AllowAssembly"???
 
Also, if I don't encrypt the file at all, I can combine PDFs with no
problem.  Is this a "all or nothing" situation??  Is there a way to
encrypt a file but allow Document Assembly without triggering the
password protection?
 
Thanks,
Katherine Tung
 
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());
            }
        }
    }
    
}
 
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to