总页数:
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); 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);
document.add(table); document.close(); return new ByteArrayInputStream(out.toByteArray()); }
|
另外,这篇例子也有可能有用:
https://kb.itextpdf.com/home/it7kb/examples/page-events-for-headers-and-footers