gpt4 book ai didi

java - document.close() 需要很长时间才能将 pdf 数据写入硬盘

转载 作者:行者123 更新时间:2023-12-01 13:37:12 25 4
gpt4 key购买 nike

以下是 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);
}
}

我尝试处理一个大约700MB的pdf文件,将pdf数据写入硬盘几乎需要半个小时(我认为“document.close()”花了太长时间),而我的电脑杯I5-2430 2.4G内存6GB,硬盘520G 7200转还不错,所以我想问一下有没有做一些优化来加快itext写入速度。

非常感谢

埃里克

最佳答案

您要在 PDF 中嵌入视频吗? ;)

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

  • 真正的处理在您调用 document.close() 时开始,在此之前只完成一些准备工作。
  • 该处理涉及一些随机文件访问,其规模变化很大。尝试将文档放入RAM磁盘( /dev/shm )。
  • 处理需要几 GB 内存,而您忘记了 give it到您的流程。

无论如何,了解更多信息,例如,任务是否受 CPU 或磁盘限制。

关于java - document.close() 需要很长时间才能将 pdf 数据写入硬盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21179242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com