[英]PdfBox adding multiple images into pdf
我嘗試使用pdfbox 2.0.8將多個圖像添加到pdf中,但是目前僅會添加一個。 我有兩個不同的圖像,應該附加到兩個不同的acrofield,但是只會添加列表的最后一個。
這是我的測試功能:
@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.APPEND
或AppendMode.PREPEND
具體取決於應在新內容之上還是之下繪制新內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.