簡體   English   中英

有沒有辦法在 PDFBox 中設置圖形的邊界框

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

我正在嘗試創建一個符合 PAC3 的 PDF,我需要將圖像添加到 PDF。 我能夠將圖像添加到 PDF 但是當我在 PAC 3 中運行 PDF 時。我收到一個錯誤,因為我的圖像沒有邊界框。 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);

    }

我的問題:如何為圖像添加邊界框? PDFBox甚至可以嗎?

最初我找到了一種通過 PAC 3 測試的解決方法,我會在所有內容編寫完成后繪制圖像,無論出於何種原因,這對我的 13 頁文檔有效,但是當我嘗試轉換 2 頁文檔時,錯誤又開始出現。

從那時起,我找到了為圖像提供邊界框的正確方法,您需要將此代碼添加到圖像創建中,它將滿足 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