在java中使用iText pdf替换pdf页面的颜色 [英] Change the color of pdf pages alternatively using iText pdf in java

查看:308
本文介绍了在java中使用iText pdf替换pdf页面的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

但是当我使用 document.newpage()移至第3页时,页面仍为蓝色。
我无法改变第3页的颜色。我想将第3页的颜色改为白色。
如何使用iText进行此操作?

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?

推荐答案

这是如何使用iText添加pdf的页面背景颜色在java

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

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.

在这个例子中,我为第1页和第2页创建了一个蓝色背景,为所有后续偶数页创建了一个灰色背景。请参阅 page_backgrounds.pdf

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

这是如何实现的?好吧,使用与我对这个相关问题的答案中使用的技术相同的技术:如何使用iText库5.5.2绘制整个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

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

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

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

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页或第2页,或者如果页码是偶数,我从编写器获取内容,我从文档中获取页面的维度 。然后我将填充颜色设置为蓝色或浅灰色(取决于页码),然后构建覆盖整个页面的矩形的路径。最后,我用填充颜色填充该矩形。

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.

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

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

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.

这篇关于在java中使用iText pdf替换pdf页面的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆