Add paragraph dynamically at the bottom with iText

2019-08-12 20:43发布

问题:

I am using below code but this is not working its show only one line in bottom while I want a paragraph in bottom in every page.

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); 

回答1:

As documented, ColumnText.showTextAligned() will only show one line. If you need multiple lines, then you can also use ColumnText, but you need to use it in a different way.

Please download the free ebook The Best iText Questions on StackOverflow. In the section "Absolute positioning of text", you'll find several examples that involve ColumnText.

These are some questions and answers you should check:

  • How to fit a String inside a rectangle?
  • How to draw a rectangle around multiline text
  • iText placement of Phrase within ColumnText

This is a code snippet from one of the answers to these questions:

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();

I hardcoded the position:

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

This is a rectangle with lower left corner (120, 500), a width of 130 and a height of 380. You will need to adapt these values if you want the text to appear at the bottom of the page.



标签: android itext