我想分割PDF文件并添加密码以保护它们。 我可以拆分PDF但我无法为每个拆分文件添加密码。 从我的代码中我发现错误" java.io.IOException:找不到PDF标题签名。" 我不知道要解决它们:'(
`
public class TestSplitPDF {
private static String USER_PASS = "Hello123";
private static String OWNER_PASS = "12345";
public void createPdf(String filename) throws IOException, DocumentException {
OutputStream file = new FileOutputStream(new File(filename));
PdfReader reader = new PdfReader(filename);
Document document = new Document();
PdfCopy c = new PdfCopy(document,new FileOutputStream(filename));
PdfWriter pdfWriter = PdfWriter.getInstance(document, file);
pdfWriter.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
PdfWriter.AllowPrinting, PdfWriter.STRENGTH128BITS);
document = new Document();
document.open();
int pageSize = reader.getNumberOfPages();
System.out.println(filename +" Page : "+pageSize);
for (int i=1 ; i<pageSize ; i++) {
c.addPage(c.getImportedPage(reader, i));
}
document.close();
file.close();
//reader.close();
}
public void SplitPDF () {
try {
PdfReader Split_PDF_By_Size = new PdfReader("C:/JavaCode/PDFTest.pdf");
Document document=new Document();
PdfCopy copy=new PdfCopy(document,new FileOutputStream("C:/JavaCode/PDFTest_1.pdf"));
document.open();
int number_of_pages = Split_PDF_By_Size.getNumberOfPages();
int pagenumber=1; /* To generate file name dynamically */
int Find_PDF_Size; /* To get PDF size in bytes */
float combinedsize=0; /* To convert this to Kilobytes and estimate new PDF size */
String FileName = "C:/JavaCode/PDFTest_1.pdf";
for (int i = 1; i < number_of_pages; i++ ) {
if (combinedsize==0 && i !=1 ){ /* Generate new file only for second time when first document size
exceeds limit and incoming loop counter is not 1 */
document = new Document();
pagenumber++;
FileName="C:/JavaCode/PDFTest_"+ pagenumber+".pdf"; /* Dynamic file name */
copy = new PdfCopy(document,new FileOutputStream(FileName));
document.open();
copy.addPage(copy.getImportedPage(Split_PDF_By_Size, i)); /* Import pages from original document */
Find_PDF_Size=copy.getCurrentDocumentSize(); /* Estimate PDF size in bytes */
combinedsize=(float)Find_PDF_Size/1024; /* Convert bytes to kilobytes */
}//end if
else {
copy.addPage(copy.getImportedPage(Split_PDF_By_Size, i)); /* Import pages from original document */
Find_PDF_Size=copy.getCurrentDocumentSize(); /* Estimate PDF size in bytes */
combinedsize=(float)Find_PDF_Size/1024; /* Convert bytes to kilobytes */
}
if (combinedsize > 1024 || i==number_of_pages) { /* Close document if the page is the last page or if limit reaches */
document.close();
combinedsize=0; /* reset variable to generate next file, if required */
}//end if
}//end for
document.close();
System.out.println("PDF Split By Size Completed. Number of Documents Created:"+pagenumber);
for(int p=1 ; p<pagenumber ; p++) {
FileName = "C:/JavaCode/PDFTest_"+p+".pdf";
TestSplitPDF pdf2 = new TestSplitPDF();
pdf2.createPdf(FileName);
}//end for
}//end try
catch (Exception i)
{
System.out.println(i);
}//end catch
}//end splitPDF
public static void main (String args[]) throws IOException, DocumentException {
TestSplitPDF usePDF = new TestSplitPDF();
usePDF.SplitPDF();
}
}
`
感谢您的帮助。
答案 0 :(得分:2)
您创建file
对象的那一刻,您将销毁您即将阅读的PDF(它变成一个0字节的打开文件):
OutputStream file = new FileOutputStream(new File(filename));
然后,当您尝试阅读此文件时,您会收到IOException
:
PdfReader reader = new PdfReader(filename);
找不到PDF标题签名。这意味着PdfReader
在文件开头找不到%PDF-
。很明显,为什么会发生这种情况:文件不再是PDF文件,而是一个0字节的文件。
这是你应该如何修复你的方法:
public void createPdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),
PdfWriter.AllowPrinting, PdfWriter.STRENGTH128BITS);
stamper.close();
}
您的代码真的难以理解:您正在混合PdfCopy
和PdfWriter
。请在开始编码之前阅读文档,并且不要使用引用我姓名的iText版本开始编码(我是Lowagie并且你正在使用com.lowagie
类)。阅读此页面以获取更多信息:http://itextpdf.com/salesfaq