Q: I am trying to flatten fields in a PDF document and I am getting a PDFPermissionException. How can I fix that error?

A: Qoppa’s PDF library and component products enforce all passwords and permissions defined in the PDF specifications.

No Open Password Provided
If the document is password protected and you have not provided the password to open, you will get the following error

com.qoppa.pdf.PDFPermissionException: Document Protected

No Permission Password Provided
If you entered the open password but the permissions do not allow to change the document, you will get the error below. In this case, you need to make sure to provide the permission password (also called owner password or master password) to ensure that all permissions are granted.

com.qoppa.pdf.PDFPermissionException: The document's permissions do not allow you to change this document.

How to Provide Permission Passwords to Unlock Permissions
If your document is protected by a password, you will need to:

  • Enter the permissions / master password through the IPassword interface
  • OR
  • Clear the document password and permissions for instance by using a PDF editor such as Adobe Acrobat or Qoppa’s PDF Studio.

Here is sample code to implement the IPassword interface programmatically.

public class FlattenFields 
{
	static class MyIPassword implements IPassword
	{
           public String [] getPasswords()
           {
             // this is a simple way to return a static list of possible passwords.
             // It is preferable to enter permission passwords rather than open passwords
             // as with permissions passwords, all permissions will be granted.
             // This method could also be edited to retrieve the password for each PDF
             // for instance from the database.
             return new String[]{"password1", "password2", "password3"};
          }
	}
 
	public static void main (String [] args)
        {
        try
        {
            // Load the document
            PDFFields pdfDoc = new PDFFields ("input.pdf", new MyIPassword());
 
            // Flatten fields
            pdfDoc.flattenFields(false, false);
 
            // Save the document
            pdfDoc.saveDocument ("output.pdf");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}