iText PDF - 如何添加多行页脚?
iText PDF - How to add multiline footer?
我可以使用 iText PDF 向生成的 PDF 添加单行页脚,但我需要添加多行页脚。
我试过用 Java (\n
) 的换行符连接两个字符串,但没有机会(参见代码 #1)。此外,已尝试通过 class PdfPageEventHelper
的 onEndPage
方法的 float x, float y
参数设置多行页脚。也没有用(参见代码 #2)。
这是我目前尝试过的方法:
代码#1
Phrase phrase = new Phrase("line1" + "\n" + "line2", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase, 40, 30, 0);
代码#2
Phrase phrase = new Phrase("line1", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase, 40, 30, 0);
Phrase phrase2 = new Phrase("line2", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase2, 40, 0, 0);
您正在使用 ColumnText.showTextAligned()
。这是一种可用于添加单行文本的方法。您不应期望它能用于添加多行文本。
如果要添加多行,则必须定义一个Rectangle
,并且必须使用ColumnText
来添加此矩形内的内容。这(当然)在官方文档中有解释,更具体地在 Absolute positioning of text (iText 5) where you will find the question How to add text inside a rectangle?
部分
该问题答案中的代码是 C# 代码,但很容易将其转换为 Java:
Rectangle rect = new Rectangle(x1, y1, x2, y2);
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.SetSimpleColumn(rect);
ct.addElement(new Paragraph("This is the text added in the rectangle"));
ct.go();
定义 x1
、y1
、x2
和 y2
的值,使所有文本都适合矩形(不适合矩形的文本t fit 将被省略),并以这样的方式定位在页面底部。
我可以使用 iText PDF 向生成的 PDF 添加单行页脚,但我需要添加多行页脚。
我试过用 Java (\n
) 的换行符连接两个字符串,但没有机会(参见代码 #1)。此外,已尝试通过 class PdfPageEventHelper
的 onEndPage
方法的 float x, float y
参数设置多行页脚。也没有用(参见代码 #2)。
这是我目前尝试过的方法:
代码#1
Phrase phrase = new Phrase("line1" + "\n" + "line2", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase, 40, 30, 0);
代码#2
Phrase phrase = new Phrase("line1", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase, 40, 30, 0);
Phrase phrase2 = new Phrase("line2", fontNormal10);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, phrase2, 40, 0, 0);
您正在使用 ColumnText.showTextAligned()
。这是一种可用于添加单行文本的方法。您不应期望它能用于添加多行文本。
如果要添加多行,则必须定义一个Rectangle
,并且必须使用ColumnText
来添加此矩形内的内容。这(当然)在官方文档中有解释,更具体地在 Absolute positioning of text (iText 5) where you will find the question How to add text inside a rectangle?
该问题答案中的代码是 C# 代码,但很容易将其转换为 Java:
Rectangle rect = new Rectangle(x1, y1, x2, y2);
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.SetSimpleColumn(rect);
ct.addElement(new Paragraph("This is the text added in the rectangle"));
ct.go();
定义 x1
、y1
、x2
和 y2
的值,使所有文本都适合矩形(不适合矩形的文本t fit 将被省略),并以这样的方式定位在页面底部。