英文:
Using PDF Stamper to password protect PDFs having digital signature
问题
以下是翻译好的部分:
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();
}
}
对于这段代码,它从src
读取PDF,然后将带有密码保护的PDF写入dest
。
现在,对于几乎所有的PDF文件,上述代码都有效。然而,有一种类型的PDF文件却无法正常运行。
这个PDF文件与其他文件有什么不同?
- 它的大小比其他文件要大,即110KB,而其他文件只有2KB(我认为这应该不是问题)。
- 这个PDF文件是数字签名的,我认为这可能是问题所在。
因此,我想知道我可能做错了什么,或者是否有其他方法可以对各种类型的PDF进行密码保护。非常感谢您的帮助。
英文:
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?
- 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).
- 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.
答案1
得分: 0
或许,你可以移除这行然后再次尝试
| PdfWriter.DO_NOT_ENCRYPT_METADATA
英文:
Perhaps, you may remove this line and try again
| PdfWriter.DO_NOT_ENCRYPT_METADATA
专注分享java语言的经验与见解,让所有开发者获益!
评论