gpt4 book ai didi

java - PdfBox 将多个图像添加到 pdf 中

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

我尝试使用 pdfbox 2.0.8 将多个图像添加到 pdf 中,但目前只会添加一个。我有两个不同的图像,应该附加到两个不同的 acrofields,但只会添加我的列表中的最后一个。

这是我的测试函数:

@Test
public void attachBulkImageToField(){

List<ImageData> data = new ArrayList<>();

data.add(new ImageData(signatureAusstellerField,signatureAussteller.toPath()));
data.add(new ImageData(signatureDienstleisterField, signatureDienstleister.toPath()));

ImageToFieldDrawer imgDrawer = new ImageToFieldDrawer(pdf);
assertTrue(imgDrawer.drawImageToField(data, Paths.get("d:\\imageBulk.pdf")));

}


public boolean drawImageToField(List<ImageData> data, final Path outPath) {
try {
for (ImageData element : data) {
addImageForField(element.getImagePath(), getAcroFieldWithName(element.getFieldName()));
}
savePdf(outPath);
return true;

} catch (IOException e) {
e.printStackTrace();
} catch (PDFSizeException e) {
e.printStackTrace();
}
return false;
}


private void savePdf(Path outPath) throws IOException {
pdDocument.save(outPath.toFile());
pdDocument.close();
}

private void addImageForField(Path signature, AcroField targetField) throws IOException {
PDPage page = pdDocument.getPage(targetField.getPageNr() - 1);
DrawImage image = new DrawImage(Files.readAllBytes(signature), 0, 0);
PDImageXObject pdImage = PDImageXObject.createFromFile(signature.toAbsolutePath().toString(), pdDocument);

try(PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page)){
contentStream.drawImage(pdImage, targetField.getX(), targetField.getY(), targetField.getWidth(), targetField.getHeight());
}
}



public class ImageData {

private String fieldName;
private Path imagePath;

public ImageData(String fieldName, Path imagePath) {
this.fieldName = fieldName;
this.imagePath = imagePath;
}

public String getFieldName() {
return fieldName;
}

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public Path getImagePath() {
return imagePath;
}

public void setImagePath(Path imagePath) {
this.imagePath = imagePath;
}

}

最佳答案

您使用以下方法为目标页面创建内容流

PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page)

这个构造函数被记录为

/**
* Create a new PDPage content stream. This constructor overwrites all existing content streams
* of this page.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException

因此,使用此构造函数,您将覆盖该页面的所有现有内容流!特别是,您会覆盖任何先前添加的用于绘制另一图像的指令...

您应该使用不同的构造函数,例如

/**
* Create a new PDPage content stream. If the appendContent parameter is set to
* {@link AppendMode#APPEND}, you may want to use
* {@link #PDPageContentStream(PDDocument, PDPage, PDPageContentStream.AppendMode, boolean, boolean)}
* instead, with the fifth parameter set to true.
*
* @param document The document the page is part of.
* @param sourcePage The page to write the contents to.
* @param appendContent Indicates whether content will be overwritten, appended or prepended.
* @param compress Tell if the content stream should compress the page contents.
* @throws IOException If there is an error writing to the page contents.
*/
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
boolean compress) throws IOException

使用AppendMode.APPENDAppendMode.PREPEND,具体取决于新内容是否应绘制在先前绘制的内容之上或之下。

关于java - PdfBox 将多个图像添加到 pdf 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47934390/

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