Is there a way to set bounding box of figure in PDFBox

Viewed 102

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?

1 Answers

Originally I found a workaround for passing the PAC 3 tests, I would draw the images after everything was written and for whatever reason that worked on my 13 page document but when I tried to convert a 2 page document the error started to appear again.

Since then I found the proper way of giving an image a bounding box you will need to add this code to the image creation and it will satisfy PAC 3 tests (the proper way)

        PDRectangle boundingBox = new PDRectangle(x, y, width, height);

        COSDictionary attr = new COSDictionary();
        attr.setString(COSName.BBOX, boundingBox.toString());

        currentElem.getCOSObject().setItem(COSName.A, attr);
Related