1

I am using the following code to password protect a PDF.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Secure_file {
    private static String USER_PASSWORD = "password";
    private static String OWNER_PASSWORD = "secured";
    public static void main(String[] args) throws IOException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER,
    PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
    }
}

where it is reading the PDF from src and then writing the PDF to the dest which is password protected.

Now, while for almost all the PDF files, the above code is working. However, there is this one type of PDF file for which this is failing.

How is this PDF file different from other?

  1. Its size is comparatively larger than the others. i.e. 110 Kb while others are 2 kb (which I suppose should not be a problem).
  2. This PDF file is digitally signed, which i think might be causing the issue.

Hence, I would want to know, what i can be doing wrong or if there is an alternate way for password protecting all kinds of PDF. Any help is appreciated.

Allan
  • 25
  • 1
  • 8
  • *"for which this is failing"* - failing how? The output file is broken? An exception occurs? The computer starts to burn? – mkl Apr 09 '20 at 12:54
  • Other than that, if the PDF is signed and thereafter encrypted, the digital signature obviously is invalidated. – mkl Apr 09 '20 at 12:56
  • How are you digital signing it? Please share that code code also – shihabudheenk Apr 09 '20 at 17:51

1 Answers1

0

Perhaps, you may remove this line and try again

| PdfWriter.DO_NOT_ENCRYPT_METADATA
shihabudheenk
  • 593
  • 5
  • 18