Remove PDF Restrictions

The Java code below removes any PDF restrictions if they exist on the document .  The program accepts two arguments, the input and output file names, which can be the same if desired.  The program requires a couple pieces from the PDFBox subproject library, available at https://pdfbox.apache.org/download.cgi

This code will not open a PDF that requires a master password, but it's a simple addition to add that to the PDDocument.load().


import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;

public class pdfRemove {

  public static void main(String[] args) throws InvalidPasswordException, IOException {
    File file = new File(args[0]); 
PDDocument document = PDDocument.load(file);

    if (document.isEncrypted()) {
      document.setAllSecurityToBeRemoved(true);
}

    document.save(new File(args[1]));
document.close(); 
    }
  
}

No comments:

Post a Comment

Upload CSV Files to Google BigQuery

This Google App Script gets each file in the "ToBeUploaded" folder in Google Drive and creates a Google BigQuery load job with tha...