"page x of y" 在 table 中,如何对齐 table 中的文本而不是页面边框?
"page x of y" in a table, how to allign the text within a table instead of the page border?
我能够使用 Chapter 7: Handling events; setting viewer preferences and writer properties 解决 "Page X of Y" 问题 的积木示例创建第 x 页,共第 y 页。在示例中,"page x of y" 的文本通过 Canvas
与页面边框对齐。但通常 "x of y" 应该像这样放入 table 中:
在这种情况下,文本应在 table 内对齐,如何做到这一点?
在我的应用程序中,包含页面 x 或 y 的 table 应显示在每个页面上,并且仍然在固定位置,即页面的右上角。 table 整个文档的格式和大小不会改变。
首先,为了适应整个 table,您需要增加 Document
:
的底部边距
Document document = new Document(pdf);
document.setBottomMargin(100);
之后,您仍然可以使用Canvas
来添加一个table而不是一个段落。我将根据您提到的 PageXofY
示例来回答。
首先,创建一个普通的Table
:
Table table = new Table(UnitValue.createPercentArray(new float[] {50, 50}));
table.addCell(new Cell(4, 1));
table.addCell(new Cell().add("Filename: "));
table.addCell(new Cell().add("Issue date: "));
Paragraph pageXofY = new Paragraph().
add("Page " + String.valueOf(pageNumber) + " of ").
add(new Image(placeholder));
table.addCell(new Cell().add(pageXofY));
table.addCell(new Cell().add("Location: "));
请注意,我们仍然使用占位符FormXObject
来存储总页数。
将side
更改为字体大小,在我们的例子中是12
。像这样创建占位符:
placeholder = new PdfFormXObject(new Rectangle(0, 0, 2 * side, side));
对 writeTotal()
方法稍作改动。文本的 y
位置已更改为 -descent
:
public void writeTotal(PdfDocument pdf) {
Canvas canvas = new Canvas(placeholder, pdf);
canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
0, -descent, TextAlignment.LEFT);
}
现在您需要做的就是将此 table 添加到页面上的适当位置:
float marginX = 36;
Canvas canvas = new Canvas(pdfCanvas, pdf, new Rectangle(marginX, 10, pageSize.getWidth() - marginX * 2, 100));
canvas.add(table);
pdfCanvas.release();
结果如下所示:
我能够使用 Chapter 7: Handling events; setting viewer preferences and writer properties 解决 "Page X of Y" 问题 的积木示例创建第 x 页,共第 y 页。在示例中,"page x of y" 的文本通过 Canvas
与页面边框对齐。但通常 "x of y" 应该像这样放入 table 中:
在这种情况下,文本应在 table 内对齐,如何做到这一点? 在我的应用程序中,包含页面 x 或 y 的 table 应显示在每个页面上,并且仍然在固定位置,即页面的右上角。 table 整个文档的格式和大小不会改变。
首先,为了适应整个 table,您需要增加 Document
:
Document document = new Document(pdf);
document.setBottomMargin(100);
之后,您仍然可以使用Canvas
来添加一个table而不是一个段落。我将根据您提到的 PageXofY
示例来回答。
首先,创建一个普通的Table
:
Table table = new Table(UnitValue.createPercentArray(new float[] {50, 50}));
table.addCell(new Cell(4, 1));
table.addCell(new Cell().add("Filename: "));
table.addCell(new Cell().add("Issue date: "));
Paragraph pageXofY = new Paragraph().
add("Page " + String.valueOf(pageNumber) + " of ").
add(new Image(placeholder));
table.addCell(new Cell().add(pageXofY));
table.addCell(new Cell().add("Location: "));
请注意,我们仍然使用占位符FormXObject
来存储总页数。
将side
更改为字体大小,在我们的例子中是12
。像这样创建占位符:
placeholder = new PdfFormXObject(new Rectangle(0, 0, 2 * side, side));
对 writeTotal()
方法稍作改动。文本的 y
位置已更改为 -descent
:
public void writeTotal(PdfDocument pdf) {
Canvas canvas = new Canvas(placeholder, pdf);
canvas.showTextAligned(String.valueOf(pdf.getNumberOfPages()),
0, -descent, TextAlignment.LEFT);
}
现在您需要做的就是将此 table 添加到页面上的适当位置:
float marginX = 36;
Canvas canvas = new Canvas(pdfCanvas, pdf, new Rectangle(marginX, 10, pageSize.getWidth() - marginX * 2, 100));
canvas.add(table);
pdfCanvas.release();
结果如下所示: