如何使用 PDFBOX 加载受密码保护的 PDF 表单
我有一小段代码来加载不受保护的 PDF 表单
PDDocument pdfDoc;
pdfDoc = PDDocument.load(filePath);
谁能帮帮我..谢谢
试试这个代码:
private void openPDFDoc(final File pdfFile) throws Exception {
File originalPDF = pdfFile;
PDFParser parser = new PDFParser(new BufferedInputStream(new FileInputStream(
originalPDF)));
parser.parse();
PDDocument originialPdfDoc = parser.getPDDocument();
boolean isOriginalDocEncrypted = originialPdfDoc.isEncrypted();
if (isOriginalDocEncrypted) {
originialPdfDoc.openProtection(new StandardDecryptionMaterial("password"));
}
}
你可以使用
public static void main(String[] args){
PDDocument pd;
try {
File input = new File("p.pdf"); // password protected PDF file from where you would like to extract
pd = PDDocument.load(input,"your_password");
pd.setAllSecurityToBeRemoved(true);
//for printing pdf file data
PDFTextStripper reader = new PDFTextStripper();
String pageText = reader.getText(pd);
System.out.println(pageText);
} catch (Exception e){
e.printStackTrace();
}
}