抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

总页数:

https://blog.csdn.net/iteye_4856/article/details/81791061

https://blog.csdn.net/jarniyy/article/details/51613131

最终参考官方文档:

https://kb.itextpdf.com/home/it5kb/examples/adding-page-numbers-to-an-existing-pdf

最终实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
public static void writePageNumber(InputStream stream, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(stream);
int n = reader.getNumberOfPages();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte pageContent;
for (int i = 1; i <= n; i++) {
pageContent = stamper.getOverContent(i);
ColumnText.showTextAligned(pageContent, Element.ALIGN_RIGHT,
new Phrase(String.format("%s / %s", i, n)), 559, 810, 0);
}
stamper.close();
reader.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public InputStream generateLabel(BolInfo bolInfo) throws Exception {
// 第一步 创建文档实例
Document document = new Document(PageSize.A4);
// 第二步 获取PdfWriter实例,内存中操作pdf
ByteArrayOutputStream out = new ByteArrayOutputStream();
final PdfWriter writer = PdfWriter.getInstance(document, out);
// 第三步 打开文档
document.open();
// 第四步 添加段落内容
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.setWidths(new float[]{0.5f, 0.5f});
zeroRow(table, writer);
oneRow(table);
twoRow(table, bolInfo);
threeRow(table);
fourRow(table, bolInfo);
fiveRow(table, bolInfo);
sixRow(table, bolInfo);
sevenRow(table, bolInfo);
sevenRow2(table, bolInfo);
eightRow(table);
nineRow(table);
tenRow(table, bolInfo);
elevenRow(table);
// twelveRow(table);
// thirteenRow(table);
// fourteen(table);
document.add(table);
// 第五步 操作完成后必须执行文档关闭操作。
document.close();
//第六步 输出 (定义pdf输出的路径)
return new ByteArrayInputStream(out.toByteArray());
}

另外,这篇例子也有可能有用:

https://kb.itextpdf.com/home/it7kb/examples/page-events-for-headers-and-footers

评论