简体   繁体   中英

Using PDF Stamper to password protect PDFs having digital signature

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. ie 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.

Perhaps, you may remove this line and try again

| PdfWriter.DO_NOT_ENCRYPT_METADATA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM