为什么这样pageEvent介绍如何使用iText库页面的内容? [英] Why this pageEvent cover the content of page using iText library?

查看:1285
本文介绍了为什么这样pageEvent介绍如何使用iText库页面的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与边界线PageEvent也适用背景色整页但是当这种PageEvent应用createPDF隐藏的内容。

There is a PageEvent with border-line also apply background color for whole page but when this PageEvent applied the content of "createPDF" hide.

public class BlackBorder extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContent();
        Rectangle rect = document.getPageSize();
        rect.setBackgroundColor(new BaseColor(234,234,234,0));
        rect.setBorder(Rectangle.BOX);  
        rect.setBorderWidth(10);  
        rect.setBorderColor(BaseColor.BLACK); 
        rect.setUseVariableBorders(true);  
        canvas.rectangle(rect);
    }
}


//Method of createPDF.
public void createPdf(String filename) throws IOException, DocumentException {

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    BlackBorder event_border = new BlackBorder();
    writer.setPageEvent(event_border);
    document.open();
    Chunk chunk = new Chunk(String.valueOf(FileCounter)+"No"+String.valueOf(FileCounter));
    chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.3f, BaseColor.CYAN);
    document.add(chunk);
    document.close();
}

如果我删除
    rect.setBackgroundColor(新BaseColor(234,234,234,0))

If i remove rect.setBackgroundColor(new BaseColor(234,234,234,0))

块将是可见的。此外,我已经试过背景事件,如:

the Chunk would be visible. Also i've tried Background event like:

public class Background extends PdfPageEventHelper {
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        PdfContentByte canvas = writer.getDirectContent();
        Rectangle rect = document.getPageSize();
        rect.setBackgroundColor(new BaseColor(234,234,234,0));
        canvas.rectangle(rect);
    }
}

和添加

    Background event = new Background();
    writer.setPageEvent(event);

在createPdf方法,但应用程序无法产生任何PDF文件!

in createPdf method but app couldn't generate any PDF file!

这将非常感激,如果你挂我怎么有多个PageEvent像水印巡航能力,夏理躯,背景等等。我尝试了一些人,但我认为他们互相掩护。

It would be much obliged if you linked me how to have multiple PageEvent like haveing watermark, hearder-footer , background etc. I tried some of them but i think they cover each other.

谢谢..

推荐答案

因为你编程这种方式的矩形覆盖的内容。

The rectangle covers the content because you programmed it this way.

如果你想下添加一个不透明的矩形的 的现有内容,替换:

If you want to add an opaque rectangle under the existing content, replace:

PdfContentByte canvas = writer.getDirectContent();

PdfContentByte canvas = writer.getDirectContentUnder();

这篇关于为什么这样pageEvent介绍如何使用iText库页面的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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