如何在 java 的 itext pdf 库中为段落添加边框?

How to add border to paragraph in itext pdf library in java?

我在 java 中使用 itext pdf 库创建了一个段落。我必须为段落添加边框,而不是为整个文档添加边框。怎么做?

试试这个:

public static void main(String[] args) {
     Document document = new Document();
    // step 2
    PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("somepath"));
    document.setPageSize(PageSize.LETTER);
    document.setMargins(36, 72, 108, 180);
    document.setMarginMirroring(false);
    // step 3
    document.open();
    // step 4
    Rectangle rect= new Rectangle(36,108);
    rect.enableBorderSide(1);
    rect.enableBorderSide(2);
    rect.enableBorderSide(4);
    rect.enableBorderSide(8);
    rect.setBorder(2);
    rect.setBorderColor(BaseColor.BLACK);
    rect.setBorderWidth(2);
    document.add(rect);
}

请查看 BorderForParagraph 示例。它展示了如何为这样的段落添加边框:

没有允许您为 Paragraph 创建边框的方法,但您可以创建一个 PdfPageEvent 实现,允许您根据 Paragraph 的开始和结束位置绘制矩形Paragraph:

class ParagraphBorder extends PdfPageEventHelper {
    public boolean active = false;
    public void setActive(boolean active) {
        this.active = active;
    }

    public float offset = 5;
    public float startPosition;

    @Override
    public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
        this.startPosition = paragraphPosition;
    }

    @Override
    public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), paragraphPosition - offset,
                document.right() - document.left(), startPosition - paragraphPosition);
            cb.stroke();
        }
    }
}

如您所见,我引入了一个名为activeboolean参数。默认情况下,我已将此参数设置为 false。我还创建了一个 offset(更改此值以微调结果)和一个 startPosition 参数。

每次 iText 开始呈现 Paragraph 对象时,startPosition 值都会更新。每次 iText 结束渲染一个 Paragraph 时,如果 activetrue 则绘制一个矩形(否则什么也不会发生)。

我们这样使用这个事件:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    ParagraphBorder border = new ParagraphBorder();
    writer.setPageEvent(border);
    document.open();
    document.add(new Paragraph("Hello,"));
    document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
    border.setActive(true);
    document.add(new Paragraph("This paragraph now has a border. Isn't that fantastic? By changing the event, we can even provide a background color, change the line width of the border and many other things. Now let's deactivate the event."));
    border.setActive(false);
    document.add(new Paragraph("This paragraph no longer has a border."));
    document.close();
}

如您所见,我们使用 setPageEvent() 方法向 PdfWriter 声明事件。我们这样激活事件:

border.setActive(true);

然后我们这样停用它:

border.setActive(false);

只是概念验证!如果您希望它适用于跨越一页以上的段落,您将需要实施 onStartPage()onEndPage() 方法。这显示在 BorderForParagraph2:

onStartPage()onEndPage() 的实现是显而易见的:

class ParagraphBorder extends PdfPageEventHelper {
    public boolean active = false;
    public void setActive(boolean active) {
        this.active = active;
    }

    public float offset = 5;
    public float startPosition;

    @Override
    public void onStartPage(PdfWriter writer, Document document) {
        startPosition = document.top();
    }

    @Override
    public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
        this.startPosition = paragraphPosition;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), document.bottom() - offset,
                document.right() - document.left(), startPosition - document.bottom());
            cb.stroke();
        }
    }

    @Override
    public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), paragraphPosition - offset,
                document.right() - document.left(), startPosition - paragraphPosition);
            cb.stroke();
        }
    }
}