2014-02-26 51 views
0

我想分割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(); 
} 



    } 

`

謝謝大家幫忙

+0

在嘗試添加密碼之前是否保存了_split_文件? – devnull

回答

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(); 
} 

你的代碼是真的很難閱讀:你是混合PdfCopyPdfWriter。請在開始編碼之前閱讀文檔,並且不要使用引用我的名字的iText版本(我是Lowagie並且您正在使用com.lowagie類)開始編碼。請閱讀此頁獲取更多信息:http://itextpdf.com/salesfaq

+0

謝謝你對這個圖書館的解釋,所以我會從文檔中學習,並做到最好。 –

+0

@OkumuraAyaka *關於這個庫的解釋* - 嗯,你的問題並不是真正的iText特有的,修復也不是。相反,它是關於理解Java IO的。 – mkl