有一个带有border-line的PageEvent也会为整个页面应用背景颜色,但是当这个PageEvent应用了" createPDF"的内容时隐藏。
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(new BaseColor(234,234,234,0))
块可见。我也尝试过背景事件,如:
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方法中,但app无法生成任何PDF文件!
如果你链接我如何让多个PageEvent像水印,听众页脚,背景等等,那将是非常有必要的。我尝试了其中一些,但我认为它们相互遮盖。
谢谢..
答案 0 :(得分:1)
矩形覆盖了内容,因为你是这样编程的。
如果您想在现有内容下添加不透明矩形,请替换:
PdfContentByte canvas = writer.getDirectContent();
使用:
PdfContentByte canvas = writer.getDirectContentUnder();