在java中使用iText pdf替换pdf页面的颜色

时间:2023-02-10 22:17:58

I'm creating report based on client activity. I'm creating this report with the help of the iText PDF library. I want to create the first two pages with a blue background color (for product name and disclaimer notes) and the remaining pages in white (without a background color). I colored two pages at the very beginning of report with blue using following code.

我正在根据客户活动创建报告。我是在iText PDF库的帮助下创建此报告的。我想创建前两页的蓝色背景颜色(产品名称和免责声明)和剩余的白色页面(没有背景颜色)。我在报告的最开始用蓝色使用以下代码着色了两页。

Rectangle pageSize = new Rectangle(PageSize.A4);
pageSize.setBackgroundColor(new BaseColor(84, 141, 212));
Document document = new Document( pageSize );

But when I move to 3rd page using document.newpage(), the page is still in blue. I can't change the color of 3rd page. I want to change the color of 3rd page onward to white. How can I do this using iText?

但是当我使用document.newpage()移动到第3页时,页面仍然是蓝色的。我无法改变第3页的颜色。我想将第3页的颜色改为白色。我怎么能用iText做到这一点?

1 个解决方案

#1


This is a follow-up question of How can I add page background color of pdf using iText in java

这是一个后续问题如何在java中使用iText添加pdf的页面背景颜色

While the advice given in the answer to that question works, it's not the best advice you could get. If I had seen your original question earlier, I would have answered it differently. I would have recommended you to use page events, as is done in the PageBackgrounds example.

虽然在回答这个问题时给出的建议是有效的,但这不是你能得到的最好的建议。如果我之前看过你原来的问题,我会以不同的方式回答。我建议您使用页面事件,就像在PageBackgrounds示例中所做的那样。

In this example, I create a blue background for page 1 and 2, and a grey background for all the subsequent even pages. See page_backgrounds.pdf

在此示例中,我为第1页和第2页创建了蓝色背景,为所有后续偶数页创建了灰色背景。见page_backgrounds.pdf

How is this achieved? Well, using the same technique as used in my answer to this related question: How to draw border for whole pdf pages using iText library 5.5.2

这是如何实现的?那么,使用与我对这个相关问题的答案中使用的相同的技术:如何使用iText库5.5.2绘制整个pdf页面的边框

I create a page event like this:

我创建了一个像这样的页面事件:

public class Background extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        int pagenumber = writer.getPageNumber();
        if (pagenumber % 2 == 1 && pagenumber != 1)
            return;
        PdfContentByte canvas = writer.getDirectContentUnder();
        Rectangle rect = document.getPageSize();
        canvas.setColorFill(pagenumber < 3 ? BaseColor.BLUE : BaseColor.LIGHT_GRAY);
        canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
        canvas.fill();
    }
}

As you can see, I first check for the page number. If it's an odd number and if it's not equal to 1, I don't do anything.

如您所见,我首先检查页码。如果它是一个奇数,如果它不等于1,我什么都不做。

However, if I'm on page 1 or 2, or if the page number is even, I get the content from the writer, and I get the dimension of the page from the document. I then set the fill color to either blue or light gray (depending on the page number), and I construct the path for a rectangle that covers the complete page. Finally, I fill that rectangle with the fill color.

但是,如果我在第1页或第2页,或者页码是偶数,我从作者那里获取内容,并从文档中获取页面的维度。然后我将填充颜色设置为蓝色或浅灰色(取决于页码),然后构建覆盖整个页面的矩形的路径。最后,我用填充颜色填充该矩形。

Now that we've got our custom Background event, we can use it like this:

现在我们已经获得了自定义Background事件,我们可以像这样使用它:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
Background event = new Background();
writer.setPageEvent(event);

Feel free to adapt the Background class if you need a different behavior.

如果您需要不同的行为,请随意调整Background类。

#1


This is a follow-up question of How can I add page background color of pdf using iText in java

这是一个后续问题如何在java中使用iText添加pdf的页面背景颜色

While the advice given in the answer to that question works, it's not the best advice you could get. If I had seen your original question earlier, I would have answered it differently. I would have recommended you to use page events, as is done in the PageBackgrounds example.

虽然在回答这个问题时给出的建议是有效的,但这不是你能得到的最好的建议。如果我之前看过你原来的问题,我会以不同的方式回答。我建议您使用页面事件,就像在PageBackgrounds示例中所做的那样。

In this example, I create a blue background for page 1 and 2, and a grey background for all the subsequent even pages. See page_backgrounds.pdf

在此示例中,我为第1页和第2页创建了蓝色背景,为所有后续偶数页创建了灰色背景。见page_backgrounds.pdf

How is this achieved? Well, using the same technique as used in my answer to this related question: How to draw border for whole pdf pages using iText library 5.5.2

这是如何实现的?那么,使用与我对这个相关问题的答案中使用的相同的技术:如何使用iText库5.5.2绘制整个pdf页面的边框

I create a page event like this:

我创建了一个像这样的页面事件:

public class Background extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        int pagenumber = writer.getPageNumber();
        if (pagenumber % 2 == 1 && pagenumber != 1)
            return;
        PdfContentByte canvas = writer.getDirectContentUnder();
        Rectangle rect = document.getPageSize();
        canvas.setColorFill(pagenumber < 3 ? BaseColor.BLUE : BaseColor.LIGHT_GRAY);
        canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
        canvas.fill();
    }
}

As you can see, I first check for the page number. If it's an odd number and if it's not equal to 1, I don't do anything.

如您所见,我首先检查页码。如果它是一个奇数,如果它不等于1,我什么都不做。

However, if I'm on page 1 or 2, or if the page number is even, I get the content from the writer, and I get the dimension of the page from the document. I then set the fill color to either blue or light gray (depending on the page number), and I construct the path for a rectangle that covers the complete page. Finally, I fill that rectangle with the fill color.

但是,如果我在第1页或第2页,或者页码是偶数,我从作者那里获取内容,并从文档中获取页面的维度。然后我将填充颜色设置为蓝色或浅灰色(取决于页码),然后构建覆盖整个页面的矩形的路径。最后,我用填充颜色填充该矩形。

Now that we've got our custom Background event, we can use it like this:

现在我们已经获得了自定义Background事件,我们可以像这样使用它:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
Background event = new Background();
writer.setPageEvent(event);

Feel free to adapt the Background class if you need a different behavior.

如果您需要不同的行为,请随意调整Background类。