Ganeshja
Ganeshja

Reputation: 2765

PDFBox : NullPointerException in StandardSecurityHandler.java

I have a encrypted PDF Form document, where need to fill in the forms with desired value and at last need to save the document Here is my piece of code of what I tried

            PDDocument document;
            document = PDDocument.load(sourcePath);
            if( document.isEncrypted() )
            {
                try
                {
                    document.decrypt( "" );
                }
                catch( InvalidPasswordException e )
                {

                }
            }
            PDAcroForm form = document.getDocumentCatalog().getAcroForm();
            PDField Field_1= form.getField("topmostSubform[0].CMS1500Form[0].PatientName[0]");          
            Field_1.setValue("ABC");    
            document.save("C:\\Users\\347702\\Desktop\\21.pdf");
            document.close();

But it throws me the below exception

java.lang.NullPointerException at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.prepareDocumentForEncryption(StandardSecurityHandler.java:303)

I have also tried with adding the below statement, above doc.save("");

document.setAllSecurityToBeRemoved(true);

Now I can execute the program without any errors but the desired value (Value : "ABC") is not been filled in saved document .. it looks blank

Can anyone help me out please !!

Thanks

Upvotes: 1

Views: 1795

Answers (1)

plinth
plinth

Reputation: 49179

I looked at the code for StandardSecurityHandler.java, which is here. Inside this method, we see the following code:

    version = computeVersionNumber();
    revision = computeRevisionNumber();
    encryptionDictionary.setFilter(FILTER);
    encryptionDictionary.setVersion(version);
    encryptionDictionary.setRevision(revision);
    encryptionDictionary.setLength(keyLength);

    String ownerPassword = policy.getOwnerPassword();
    String userPassword = policy.getUserPassword();

Allowing that the version we're looking at might not be the same as yours, the most likely cause is that the policy object is null (which is line 305, not 303, but that's the first line after this where this could fail from a NullPointerException.

Now as to form fields not being filled in, you should save with no encryption and look for the string (PatientName[0]), which should be the field name of the field you're trying to edit. If you find it, you should check to see if the dictionary contains the key /V with the associated value (ABC), which means that the code correctly set the field. If this is the case, I'm willing to bet that your document also has XFA in it, which means that the actual data for the form may not necessarily be stored in the form, but may also need to be stored in an XFA data packet. The good news is that the XFA spec is available from Adobe. The bad news is that the XFA spec is 1500 pages. The good news is that XFA is a big suite of XML nodes with a downloadable schema. The bad news is the schema is not a typical standard format and the available tools for consuming it are (apparently) unmaintained.

Upvotes: 1

Related Questions