如何在我的PDF页面添加页眉和页脚? 我想在表头中有3列,其他表中有3列,在页脚中有3列。 我的页面可能是A3或A4,横向或纵向。iText 5页眉和页脚
任何人都可以帮助我吗?我无法在互联网上找到很好的例子。
谢谢!
托马索
如何在我的PDF页面添加页眉和页脚? 我想在表头中有3列,其他表中有3列,在页脚中有3列。 我的页面可能是A3或A4,横向或纵向。iText 5页眉和页脚
任何人都可以帮助我吗?我无法在互联网上找到很好的例子。
谢谢!
托马索
例如:
public class MyPageEventListener extends PdfPageEventHelper {
. . .
@Override
public void onEndPage(PdfWriter writer, Document document) {
//code skeleton to write page header
PdfPTable tbl = new PdfPTable(3);
tbl.addCell("1st cell");
tbl.addCell("2nd cell");
tbl.addCell("3rd cell");
float x = document.leftMargin();
float hei = getMyHeaderHeight(); //custom method that return header's height
//align bottom between page edge and page margin
float y = document.top() + hei;
//write the table
tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
}
}
注册该侦听器简单地做
writer.setPageEvent(new MyPageEventListener());
要做到这一点,首先在内存中生成整个PDF内容最简单的方法,那么一旦所有的页面被创建,你需要在pdfStamper打开内存PDF和遍历所有添加到页眉和页脚对象中的页面都是正确的坐标。
如果您在itextPDF中添加页码的快速谷歌搜索,您会发现许多示例可以快速适应您的需求。
关键是它是在你创建pdf之后完成的,而不是之前。
你的代码是很混乱的,使用的是未定义的变量(如c_document或头),并有意大利(?)的评论,你可以请clearify呢? – bmurauer 2014-10-03 11:46:20