gpt4 book ai didi

image - PDFBox 添加图片时内存不足

转载 作者:行者123 更新时间:2023-12-04 18:19:10 26 4
gpt4 key购买 nike

我正在使用 PDFBox 从我的 web 应用程序中提取数据并将其放入 PDF 中。我有一种方法可以在每个 PDF 页面上绘制标题。但是,当我向每一页添加图像时,文档内存不足。我想知道是否有人对解决方案有任何想法?这是我的 drawHeader 方法:

公共(public)静态无效drawHeader(PDDocument doc,PDPage页面,PDPageContentStream contentStream,int [] columnWidths,int pageNumber)抛出IOException {

    contentStream.beginText();
PDFont font = PDType1Font.HELVETICA_BOLD;
contentStream.setFont(font, 24);
contentStream.moveTextPositionByAmount(50, 750);
contentStream.drawString("Producer License Report");
contentStream.endText();

contentStream.beginText();
contentStream.moveTextPositionByAmount(550, 750);

contentStream.setFont(PDType1Font.HELVETICA_BOLD, 8);
contentStream.drawString("Page " + pageNumber);
contentStream.endText();

contentStream.drawLine(50, 740, 340, 740);
contentStream.drawLine(16, 680, 595, 680);

List<String> headerList = new LinkedList<String>();
headerList.add("NPN");
headerList.add("First Name");
headerList.add("Last Name");
headerList.add("Suffix");
headerList.add("License State");
headerList.add("Resident State");
headerList.add("License Number");

contentStream.setFont(PDType1Font.HELVETICA_BOLD, 9);
float textx = 15;
float texty = 685;

InputStream in = new FileInputStream(new File("logo.jpg"));
PDJpeg img = new PDJpeg(doc, in);
contentStream.drawImage(img, 375, 720);


for (int i = 0; i < headerList.size(); i++) {
String text = headerList.get(i);
contentStream.beginText();
contentStream.moveTextPositionByAmount(textx, texty);
contentStream.drawString(text);
contentStream.endText();
textx += (columnWidths[i] * 6.5);
}
}

最佳答案

我找到了解决办法!您必须在打开 contentStream 之前创建 Image-Object。

例子:

   /* Step 1: Prepare the document.
*/
doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);

/* Step 2: Prepare the image
* PDJpeg is the class you use when dealing with jpg images.
* You will need to mention the jpg file and the document to which it is to be added
* Note that if you complete these steps after the creating the content stream the PDF
* file created will show "Out of memory" error.
*/

PDXObjectImage image = null;
image = new PDJpeg(doc, new FileInputStream("image.jpg"));
PDPageContentStream contentStream = new PDPageContentStream(doc,
page);
....

关于image - PDFBox 添加图片时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11052091/

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