'Is there a way to set bounding box of figure in PDFBox
I'm trying to create a PDF that's PAC3 compliant and I need to add images to the PDF. I was able add an image to the PDF but when I run the PDF in PAC 3. I get an error because my image doesn't have a bounding box. PAC3 output: image of PAC3 Here's my code for adding an image to the pdf document.
PDStructureElement currentElem;
public void drawImage(PDStructureElement parent, float width, float height, float x,float y) throws IOException {
currentElem = addContentToParent(null, StandardStructureTypes.Figure, parent);
currentElem.setAlternateDescription("logo");
PDImageXObject logoImg = PDImageXObject.createFromFile("logo.jpg", this.pdf);
PDPageContentStream contentStream = new PDPageContentStream(this.pdf, this.pdf.getPage(0), PDPageContentStream.AppendMode.APPEND, false);
setNextMarkedContentDictionary();
contentStream.beginMarkedContent(COSName.IMAGE, PDPropertyList.create(currentMarkedContentDictionary));
contentStream.drawImage(logoImg, x, y,45,42);
contentStream.endMarkedContent();
contentStream.close();
addContentToParent(COSName.IMAGE, null, currentElem);
}
My Question: How do I add a bounding box to a image? Is it even possible with PDFBox?
Solution 1:[1]
I don't know if this is much of a solid solution but I did find a way to pass the PAC 3 tests.
Originally I would write the contents of the page out page by page and draw the image after I finished writing the text on the page. I changed it so that I didn't draw the images until after all the pages within the doc already had their text written.
To put it simply: if you draw the image on the page after all the text has been written it doesn't produce any error messages.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | confusedDeveloper66 |