使用 android 中的 iText 库在底部动态添加段落

Add paragraph dynamically at the bottom with iText library in android

我正在使用下面的代码,但这不起作用它只在底部显示一行,而我想在每一页的底部显示一个段落。

public void onEndPage(PdfWriter writer, Document document) {
          Rectangle rect = writer.getBoxSize("art");
          ColumnText.showTextAligned(writer.getDirectContent(),
                  Element.ALIGN_LEFT, new Phrase(ActivityWaTestamentInput.pDFHeaderText,headerFont),
                  rect.getLeft(), rect.getTop(), 0);
          ColumnText.showTextAligned(writer.getDirectContent(),
                  Element.ALIGN_LEFT, new Paragraph(ActivityWaTestamentInput.pDFFooterText,footerFont),
                  rect.getLeft() , rect.getBottom() - 18, 0); 

如文档所述,ColumnText.showTextAligned() 将只显示一行。如果你需要多行,那么你也可以使用ColumnText,但你需要换一种方式使用它。

请下载免费电子书 The Best iText Questions on Whosebug。在 "Absolute positioning of text" 部分,您会找到几个涉及 ColumnText.

的示例

这些是您应该检查的一些问题和答案:

  • How to fit a String inside a rectangle?

这是来自以下问题之一的答案的代码片段:

ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(120f, 500f, 250f, 780f);
Paragraph p = new Paragraph("This is a long paragraph that doesn't"
    + "fit the width we defined for the simple column of the" 
    + "ColumnText object, so it will be distributed over several"
    + "lines (and we don't know in advance how many).");
ct.addElement(p);
ct.go();

我硬编码了位置:

llx = 120;
lly = 500;
urx = 250;
ury = 780;

这是一个矩形,左下角为(120, 500),宽130,高380。如果您希望文本显示在页面底部,您需要调整这些值。