2014-01-17 65 views
0

下面是示例代码从iText的动作NUP样品中如下:document.close()需要很长的时间来写PDF数据到硬盘

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.sql.SQLException; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Rectangle; 
import com.itextpdf.text.pdf.PdfContentByte; 
import com.itextpdf.text.pdf.PdfImportedPage; 
import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.PdfWriter; 
public class NUpTool { 
    /** Format of the resulting PDF files. */ 
    public static final String RESULT = "results/part2/chapter06/result%dup.pdf"; 
    /** 
    * Manipulates a PDF file src with the file dest as result 
    * @param src the original PDF 
    * @param dest the resulting PDF 
    * @param pow the PDF will be N-upped with N = Math.pow(2, pow); 
    * @throws IOException 
    * @throws DocumentException 
    * @throws SQLException 
    */ 
    public void manipulatePdf(String src, String dest, int pow) 
    throws IOException, DocumentException { 
     // reader for the src file 
     PdfReader reader = new PdfReader(src); 
     // initializations 
     Rectangle pageSize = reader.getPageSize(1); 
     Rectangle newSize = (pow % 2) == 0 ? 
      new Rectangle(pageSize.getWidth(), pageSize.getHeight()) : 
      new Rectangle(pageSize.getHeight(), pageSize.getWidth()); 
     Rectangle unitSize = new Rectangle(pageSize.getWidth(), pageSize.getHeight()); 
     for(int i = 0; i < pow; i++) { 
      unitSize = new Rectangle(unitSize.getHeight()/2, unitSize.getWidth()); 
     } 
     int n = (int) Math.pow(2, pow); 
     int r = (int) Math.pow(2, pow/2); 
     int c = n/r; 
     // step 1 
     Document document = new Document(newSize, 0, 0, 0, 0); 
     // step 2 
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(String.format(dest, n))); 
     // step 3 
     document.open(); 
     // step 4 
     PdfContentByte cb = writer.getDirectContent(); 
     PdfImportedPage page; 
     Rectangle currentSize; 
     float offsetX, offsetY, factor; 
     int total = reader.getNumberOfPages(); 
     for(int i = 0; i < total;) { 
      if(i % n == 0) { 
       document.newPage(); 
      } 
      currentSize = reader.getPageSize(++i); 
      factor = Math.min(
       unitSize.getWidth()/currentSize.getWidth(), 
       unitSize.getHeight()/currentSize.getHeight()); 
      offsetX = unitSize.getWidth() * ((i % n) % c) + (unitSize.getWidth() - (currentSize.getWidth() * factor))/2f; 
      offsetY = newSize.getHeight() - (unitSize.getHeight() * (((i % n)/c) + 1)) + (unitSize.getHeight() - (currentSize.getHeight() * factor))/2f; 
      page = writer.getImportedPage(reader, i); 
      cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); 
     } 
     // step 5 
     document.close(); 
     reader.close(); 
    } 
    /** 
    * Main method. 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    * @throws SQLException 
    */ 
    public static void main(String[] args) 
    throws IOException, DocumentException, SQLException { 
     Stationery.main(args); 
     new NUpTool().manipulatePdf(Stationery.RESULT, RESULT, 1); 
     new NUpTool().manipulatePdf(Stationery.RESULT, RESULT, 2); 
     new NUpTool().manipulatePdf(Stationery.RESULT, RESULT, 3); 
     new NUpTool().manipulatePdf(Stationery.RESULT, RESULT, 4); 
    } 
} 

,我尝试处理约700MB一个pdf文件,并且它几乎需要半个小时才能将pdf数据写入硬盘(我认为是“document.close()”需要很长时间),而我的电脑杯 I5-2430 2.4G内存6GB,硬盘520G 7200 RPM不太不好,所以我想问的是有无论如何做一些优化,以加快itext写入速度。

非常感谢

埃里克

+0

运行该程序时,您允许JVM使用多少内存?考虑允许更多。 – mkl

回答

1

你嵌入到PDF的视频? ;)

我真的没有想法,但这太长了评论。我可以看到以下几种可能性:

  • 真正的处理过程是在您拨打document.close()之前开始的,然后才完成一些准备工作。
  • 处理过程涉及一些随机文件访问,这些访问的缩放比例差别很大。尝试将文档放入RAM磁盘(/dev/shm)。
  • 处理过程需要几GB的内存,您忘记了give it您的过程。

无论如何,要了解更多信息,例如,如果任务是CPU或磁盘绑定。

+0

对于任何PDF,700 MB是*可疑*大尺寸。我敢打赌,像每个页面都会不必要地复制字体等项目。 – usr2564301

+0

..看到http://stackoverflow.com/questions/20604124/large-pdf-generation-using-itext-taking-very-long-time – usr2564301

+0

@Jongware下的评论:是的,这是一个疯狂的大小。我想你的猜测是正确的(但正如我所说的,我没有真正的线索)。 – maaartinus

相关问题