简体   繁体   English

有没有办法在 PDFBox 中设置图形的边界框

[英]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.我正在尝试创建一个符合 PAC3 的 PDF,我需要将图像添加到 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.我能够将图像添加到 PDF 但是当我在 PAC 3 中运行 PDF 时。我收到一个错误,因为我的图像没有边界框。 PAC3 output: image of PAC3 Here's my code for adding an image to the pdf document. PAC3 output: PAC3 的图像这是我将图像添加到 pdf 文档的代码。

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? PDFBox甚至可以吗?

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.最初我找到了一种通过 PAC 3 测试的解决方法,我会在所有内容编写完成后绘制图像,无论出于何种原因,这对我的 13 页文档有效,但是当我尝试转换 2 页文档时,错误又开始出现。

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)从那时起,我找到了为图像提供边界框的正确方法,您需要将此代码添加到图像创建中,它将满足 PAC 3 测试(正确方法)

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM