Any reason not to use the latest version of PDFBox (2.0.15)? It might solve
this issue...

On Fri, May 17, 2019 at 9:47 AM RJC 2009 <rjc2...@gmail.com> wrote:

> Hi All,
>
> I am using PDFBox version 2.0.5 and looking for help to protect a PDF with
> owner password.
>
> Here is my test program which is trying to disable the printing option and
> protect the document with an owner password.
>
> However, if I use the resultant PDF and run the program again with a
> DIFFERENT owner password then it ENABLE the printing option.
>
>
>
> Basically I need to disable the printing option in PDF with an owner
> password and the printing option cannot be enabled back without using the
> original owner password.
>
>
> Please advise if I am missing anything in my below testing programme.
>
>
> *import* java.io.File;
>
> *import* org.apache.pdfbox.pdmodel.PDDocument;
>
> *import* org.apache.pdfbox.pdmodel.encryption.AccessPermission;
>
> *import* org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
>
>
>
> *public* *class* TestEncryptPDF {
>
>
>
>        *public* *static* *void* main(String[] args) {
>
>
>
>               String inputPdfFileName = "D:\\temp\\test-input.pdf";
>
>               String cannotPrintPdfFileName = inputPdfFileName.replace(
> ".pdf", "-cannotPrint.pdf");
>
>               String canPrintPdfFileName = inputPdfFileName.replace(".pdf",
> "-cannPrint.pdf");
>
>               *setPdfPassword*(inputPdfFileName, cannotPrintPdfFileName,
> "OwnerPassword1234", *false*);
>
>
>
>               *setPdfPassword*(cannotPrintPdfFileName, canPrintPdfFileName,
> "BreakingOwnerPassworAndEnablingPrinting", *true*);
>
>        }
>
>
>
>        *private* *static* *void* setPdfPassword(String inputPdfFileName,
>
>                      String outputPdfFileName, String ownerPassword,
> *boolean* canPrint) {
>
>               *try* {
>
>                      PDDocument pdfDoc = PDDocument.*load*(*new* File(
> inputPdfFileName));
>
>                      AccessPermission ap = *new* AccessPermission();
>
>                      ap.setCanPrint(canPrint);
>
>                      ap.setCanAssembleDocument(*false*);
>
>                      ap.setCanExtractContent(*false*);
>
>                      ap.setCanExtractForAccessibility(*false*);
>
>                      ap.setCanFillInForm(*false*);
>
>                      ap.setCanModify(*false*);
>
>                      ap.setCanModifyAnnotations(*false*);
>
>                      ap.setReadOnly();
>
>                      // setting only owner *pwd*
>
>                      StandardProtectionPolicy spp = *new*
> StandardProtectionPolicy(
>
>                                   ownerPassword, *null*, ap);
>
>                      spp.setPreferAES(*true*);
>
>                      spp.setEncryptionKeyLength(128);
>
>                      spp.setPermissions(ap);
>
>                      pdfDoc.protect(spp);
>
>                      pdfDoc.save(outputPdfFileName);
>
>                      pdfDoc.close();
>
>               } *catch* (Exception e) {
>
>                      e.printStackTrace();
>
>               }
>
>        }
>
>
>
> }
>
> regards
>
> RJC
>

Reply via email to