使用iTEXT创建Java PDF

时间:2015-12-30 04:10:47

标签: itext

如何使用java在iText中创建具有背景颜色的段落。 我尝试使用Chunk但它的文本突出显示颜色长度,并且行之间没有应用bg颜色。

1 个答案:

答案 0 :(得分:0)

创建具有背景颜色的段落(特别是在行之间不间断)的任务可以通过页面事件侦听器实现,该侦听器本地存储段落开始位置并一旦绘制背景矩形段落末尾发出信号:

public class ParagraphBackground extends PdfPageEventHelper
{
    public BaseColor color = BaseColor.YELLOW;
    public void setColor(BaseColor color)
    {
        this.color = color;
    }

    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.saveState();
            cb.setColorFill(color);
            cb.rectangle(document.left(), document.bottom() - offset,
                document.right() - document.left(), startPosition - document.bottom());
            cb.fill();
            cb.restoreState();
        }
    }

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

ParagraphBackground.java

每当此页面事件侦听器在段落末尾处于活动状态时,段落背景都会着色。

可以像这样使用:

@Test
public void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("document-with-paragraph-backgrounds.pdf"));
    ParagraphBackground back = new ParagraphBackground();
    writer.setPageEvent(back);
    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:"));
    back.setActive(true);
    document.add(new Paragraph("This paragraph now has a background. Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. Now let's deactivate the event."));
    back.setActive(false);
    document.add(new Paragraph("This paragraph no longer has a background."));
    document.close();
}

ColorParagraphBackground.java

结果如下:

screenshot

<强>积分

这实际上是对Bruno Lowagie&#39; answer to "How to add border to paragraph in itext pdf library in java?"的剽窃,只需填写小的更改而不是笔划背景矩形。他甚至已经回过头来看过这样的应用程序,因为他的示例程序写道:

  

此段现在有一个边框。难道不是很棒吗? 通过更改事件,我们甚至可以提供背景颜色