Hello,

After hours of vain research I need the help of the pdfbox community.

Here is my problem:

I have a pdf document produced by XEP4.19, which have metadata and password 
protection. I need to open this document, modify some page (replace embedded 
graphics page by their original PDF graphic file) and then save the document.

In order to achieve this I create a new document in which I import the text 
pages of the original document and import the graphic pdf page when needed. 
Then I save then close the new document.
Here is my java code (I'm using pdfbox 1.6.0):

public void testPdfBox() throws Exception {

        final File originalPDF = PDFBOX_SAMPLE;
        final PDFParser parser = new PDFParser(new BufferedInputStream(new 
FileInputStream(
                originalPDF)));
        parser.parse();

        final PDDocument originialPdfDoc = parser.getPDDocument();

        // Open the protection on the orig doc if necessary.
        final boolean isOriginalDocEncrypted = originialPdfDoc.isEncrypted();
        if (isOriginalDocEncrypted) {
            originialPdfDoc.openProtection(new 
StandardDecryptionMaterial("secret"));
        }

        final PDDocument updatedDoc = new PDDocument();

        final List<PDPage> allPages = 
originialPdfDoc.getDocumentCatalog().getAllPages();
        final Iterator<PDPage> it = allPages.iterator();
        int i = 0;
        while (it.hasNext()) {
            i++;
            final PDPage oldPage = it.next();
            if (i == PDFBOX_PAGE_NB) {
                // Append the graphic file.
                final PDFParser graphParser = new PDFParser(new 
BufferedInputStream(
                        new FileInputStream(PDFBOX_PDF_GRAPH)));
                graphParser.parse();
                final PDDocument graphDoc = graphParser.getPDDocument();
                final List<PDPage> graphPageList = 
graphDoc.getDocumentCatalog().getAllPages();
                final PDPage page = graphPageList.iterator().next();
                updatedDoc.addPage(page);
            } else {
                updatedDoc.importPage(oldPage);
            }
        }

        // Set the metadata of the 'old' PDF to the new one
        
updatedDoc.setDocumentInformation(originialPdfDoc.getDocumentInformation());
        
updatedDoc.setEncryptionDictionary(originialPdfDoc.getEncryptionDictionary());

        if (isOriginalDocEncrypted) {
            final AccessPermission originalAccessPermission = 
originialPdfDoc.getSecurityHandler()
                    .getCurrentAccessPermission();
            final StandardProtectionPolicy originalProtectionPolicy = new 
StandardProtectionPolicy(
                    "secret", null, originalAccessPermission);
            updatedDoc.protect(originalProtectionPolicy);
        }

        updatedDoc.save(new BufferedOutputStream(new 
FileOutputStream(PDFBOX_RESULT)));
        updatedDoc.close();
        originialPdfDoc.close();

        // Only for test purpose.
        openPDFDoc(PDFBOX_RESULT);

    }

    private final void openPDFDoc(final File pdfFile) throws Exception {
        final File originalPDF = pdfFile;
        final PDFParser parser = new PDFParser(new BufferedInputStream(new 
FileInputStream(
                originalPDF)));
        parser.parse();

        final PDDocument originialPdfDoc = parser.getPDDocument();

        // Open the protection on the orig doc if necessary.
        final boolean isOriginalDocEncrypted = originialPdfDoc.isEncrypted();
        if (isOriginalDocEncrypted) {
            originialPdfDoc.openProtection(new 
StandardDecryptionMaterial("secret"));
        }
    }


The new document is as I expect: correct pages, correct metadata,... but PDFBox 
is not able to open the new document. I get the following exception:
org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied 
password does not match either the owner or user password in the document.
    at 
org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:239)
    at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1325)

Here are some additional observations:
I can open the document with adobe reader. File/Properties/Security show the 
correct permissions, but if I look in the "Show Details..." then the 
permissions are not the same! everything is allowed...The original PDF document 
had the same permissions in both the Security tab and the "Show Details..." 
window.

pdftk is not able to open the document even if I give the correct password or 
if I give it no psswd. I get the following:
Error: Failed to open PDF file: 
   /home/[...]/newDocument.pdf
   OWNER PASSWORD REQUIRED, but not given (or incorrect)
Done.  Input errors, so no output created.

 I you need any clarification just let me know.
Any help will be really appreciated.
Laurent.

Reply via email to