使用iTEXT创建Java PDF

使用iTEXT创建Java PDF,itext,Itext,如何使用java在iText中创建具有背景颜色的段落。 我试过使用Chunk,但文本的突出显示颜色达到了它的长度,行与行之间没有应用背景颜色。你的任务是创建一个带有背景颜色的段落(特别是行与行之间不间断的颜色)可通过页面事件监听器实现,该监听器本地存储段落开始位置,并在段落结束时绘制背景矩形: public class ParagraphBackground extends PdfPageEventHelper { public BaseColor color = BaseColor.Y

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

你的任务是创建一个带有背景颜色的段落(特别是行与行之间不间断的颜色)可通过页面事件监听器实现,该监听器本地存储段落开始位置,并在段落结束时绘制背景矩形:

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();
        }
    }
}
()

每当此页面事件侦听器在段落末尾设置为活动时,段落背景都会着色

它可以这样使用:

@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();
}
()

结果如下所示:

@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();
}


学分

这实际上是一个翻版,用小的变化来填充,而不是笔划背景矩形。他甚至早就想到过这样一个应用程序,正如他的示例程序所写:

这一段现在有了边界。那不是很棒吗?通过更改事件,我们甚至可以提供背景颜色


试试这个,我的答案可能重复了,你的问题解决了吗?还是有什么问题left@Ranji这就是我想要的!!!-伟大的在这种情况下,请将答案标记为已接受(单击左上角的勾号)。