简体   繁体   English

或者在Java中使用iText pdf更改pdf页面的颜色

[英]Change the color of pdf pages alternatively using iText pdf in java

I'm creating report based on client activity. 我正在根据客户活动创建报告。 I'm creating this report with the help of the iText PDF library. 我正在iText PDF库的帮助下创建此报告。 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 );

But when I move to 3rd page using document.newpage() , the page is still in blue. 但是当我使用document.newpage()移至第三页时,该页面仍为蓝色。 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做到这一点?

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. 在此示例中,我为页面1和2创建蓝色背景,为所有后续偶数页面创建灰色背景。 See page_backgrounds.pdf 参见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 . 但是,如果我在第1页或第2页上,或者页码是偶数,则从writer那里获得内容,从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: 现在我们有了自定义的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类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM