Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java document.close()将pdf数据写入硬盘需要很长时间_Java_Performance_Pdf_Itext - Fatal编程技术网

Java document.close()将pdf数据写入硬盘需要很长时间

Java document.close()将pdf数据写入硬盘需要很长时间,java,performance,pdf,itext,Java,Performance,Pdf,Itext,下面是itext in action nup示例中的示例代码,如下所示: 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

下面是itext in action nup示例中的示例代码,如下所示:

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);
    }
}
import java.io.FileOutputStream;
导入java.io.IOException;
导入java.sql.SQLException;
导入com.itextpdf.text.Document;
导入com.itextpdf.text.DocumentException;
导入com.itextpdf.text.Rectangle;
导入com.itextpdf.text.pdf.PdfContentByte;
导入com.itextpdf.text.pdf.pdf导入页面;
导入com.itextpdf.text.pdf.PdfReader;
导入com.itextpdf.text.pdf.PdfWriter;
公共类NUpTool{
/**生成的PDF文件的格式*/
公共静态最终字符串RESULT=“results/part2/chapter06/RESULT%dup.pdf”;
/**
*操作PDF文件src,结果为文件dest
*@param src原始PDF
*@param dest生成的PDF
*@param-pow该PDF将使用N=Math.pow(2,pow)进行N-upp;
*@抛出异常
*@DocumentException
*@SQLException
*/
公共空操作EPDF(字符串src、字符串dest、int pow)
抛出IOException,DocumentException{
//src文件的读取器
PdfReader读取器=新PdfReader(src);
//初始化
矩形pageSize=reader.getPageSize(1);
矩形新闻大小=(pow%2)==0?
新矩形(pageSize.getWidth(),pageSize.getHeight()):
新矩形(pageSize.getHeight(),pageSize.getWidth());
矩形unitSize=新矩形(pageSize.getWidth(),pageSize.getHeight());
对于(int i=0;i
我试图处理一个700MB左右的pdf文件,将pdf数据写入硬盘几乎需要半个小时(我认为是“document.close()”需要的时间太长),还有我的电脑 I5-2430 2.4G内存6GB,硬盘520G 7200 RPM这还不算太坏,所以我想问一下,是否有任何优化措施来加快itext的写入速度

非常感谢


Eric

您是否在PDF中嵌入视频?;)

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

  • 真正的处理在调用
    document.close()
    时开始,在此之前只做一些准备工作
  • 处理过程涉及一些随机文件访问,这些文件的大小差别很大。尝试将文档放入ram磁盘()
  • 处理需要几GB的内存,而您忘了将其添加到进程中

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

700 MB对于任何PDF来说都是一个可疑的大容量。我打赌像字体这样的项目在每页都会被不必要地复制。。。请看@Jongware下的评论:是的,它的尺寸太大了。我想你的猜测是对的(但正如我所说,我真的不知道)。在运行程序时,你允许JVM使用多少内存?考虑允许更多。