2017-06-22 48 views
0

我使用PDFBox來分割一個PDF文件,但我',有一些問題來計算每個頁面的大小。Apache PDFBox - PAGE LENGHT

的PDF的大小爲170139:

22/06/2017 10:14 <DIR>   . 
22/06/2017 10:14 <DIR>   .. 
22/06/2017 08:36   1.194.504 PDF-01PAG.pdf 
22/06/2017 10:10  11.333.168 PDF-12PAG.pdf 
21/06/2017 15:53   1.218.918 PDF-13PAG.pdf 
22/06/2017 10:13   170.139 PDF-28PAG.pdf 
       4 files  13.916.729 bytes 
       2 folder  94.124.666.880 bytes 

生成的PDF的大小爲61082:

22/06/2017 10:20 <DIR>   . 
22/06/2017 10:20 <DIR>   .. 
22/06/2017 10:31   61.082 PDF-28PAG-p0001.pdf 
       1 files(s)   61.082 bytes 
       2 folder(s)  94.122.778.624 bytes 

如何計算每個頁面的大小從PDF正確無需編寫/使用文件(File#length())?

這裏測試:

@Test 
public void testCalculatePDF28P() { 
    long result = test.calculate(getResorce("PDF/PDF-28PAG.pdf")); 
    Assert.assertEquals(170139L, result); 
} 

@Test 
public void testCalculatePDPage() 
    throws IOException { 
    InputStream resorce = getResorce("PDF/PDF-28PAG.pdf"); 
    try (PDDocument document = PDDocument.load(resorce)) { 
     PDPage page = document.getPage(0); 
     long result = test.calculate(page.getContents()); 
     File file = new File("PDF/PDF-28PAG.pdf"); 
     new PDFPage(file, page, 0).writeTo(output); 
     Assert.assertEquals(61082L, result); 
    } 
} 

這裏的計算器:

public final class SizeCalculator { 

     public long calculate(
      final InputStream input) { 
      try { 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       byte[] bytes = new byte[1024]; 
       int count; 
       while ((count = input.read(bytes)) > 0) { 
        out.write(bytes, 0, count); 
       } 
       return out.size(); 
      } catch (IOException e) { 
       throw new RuntimeException(e); 
      } 
     } 
    } 
+0

請注意,許多產品處理PDF文件編碼有些事情在這些方面有所不同r輸出。這可能會導致很大的差異。因此,頁面的大小沒有硬性價值。 – mkl

回答

0

反正

使用它:

  PDDocument documentoSaida = new PDDocument(); 
      for (int i = startPage; i < endPage; i++) { 
       ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       PDDocument tempFile = new PDDocument(); 
       PDPage page = document.getPage(i); 
       tempFile.addPage(page); 
       tempFile.save(baos); 
       p(i + ": " + (baos.size()/1024) + "KB"); 
       tempFile.close(); 
      }