PdfBox adding multiple images into pdf

蓝咒 提交于 2019-12-11 14:35:36

问题


I try to add multiple images into a pdf with pdfbox 2.0.8, but currently only one will be added. I have two different images which should be attached to two different acrofields, but only the last one of my list will be added.

This is my test function:

@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;
}

}


回答1:


You create a content stream for the target page using

PDPageContentStream contentStream = new PDPageContentStream(pdDocument, page)

This constructor is documented as

/**
 * 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

So using this constructor you overwrite all existing content streams of that page! In particular you overwrite any previously added instructions for drawing another image...

You should use a different constructor, e.g.

/**
 * 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

using AppendMode.APPEND or AppendMode.PREPEND depending on whether the new content should be drawn over or under previously drawn content.



来源:https://stackoverflow.com/questions/47934390/pdfbox-adding-multiple-images-into-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!