我想分割PDF文件並添加密碼以保護它們。 我可以拆分PDF,但無法爲每個拆分文件添加密碼。 從我的代碼中,我發現錯誤「java.io.IOException:未找到PDF標頭簽名」。 我不知道解決這些問題。「(分割PDF並在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();
}
}
`
謝謝大家幫忙
在嘗試添加密碼之前是否保存了_split_文件? – devnull