iText - Strange column- / page changing behaviour

2019-02-25 23:11发布

问题:

I am quite new to iText and trying to accomplish the following:

  • read a list of text files from local hd
  • arrange the texts of the files in a 2-column layout pdf file
  • add a consecutively numbered index before each text

I started with the MovieColumns1 example (http://itextpdf.com/examples/iia.php?id=64) and ended up with the following code:

    final float[][] COLUMNS_COORDS = { { 36, 36, 296, 806 }, { 299, 36, 559, 806 } };

    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, resultFile);
    document.open();

    ColumnText ct = new ColumnText(writer.getDirectContent());
    ct.setSimpleColumn(COLUMNS_COORDS[0][0], COLUMNS_COORDS[0][1],
        COLUMNS_COORDS[0][2], COLUMNS_COORDS[0][3]);

    File textDir = new File("c:/Users/raddatz/Desktop/123/texts/");
    File[] files = textDir.listFiles();

    int i = 1;
    int column = 0;
    for (File file : files) {
        String text = FileUtils.readFileToString(file, "UTF-8");
        float yLine = ct.getYLine();
        System.out.println("adding '" + file.getName() + "'");

        PdfPCell theText = new PdfPCell(new Phrase(text, new Font(Font.HELVETICA, 10)));
        theText.setBorder(Rectangle.NO_BORDER);
        theText.setPaddingBottom(10);
        PdfPCell runningNumber = new PdfPCell(new Phrase(new DecimalFormat("00").format(i++), new Font(
            Font.HELVETICA, 14, Font.BOLDITALIC,
            new Color(0.7f, 0.7f, 0.7f))));
        runningNumber.setBorder(Rectangle.NO_BORDER);
        runningNumber.setPaddingBottom(10);
        PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[] { 12, 100 });
        table.addCell(runningNumber);
        table.addCell(theText);
        ct.addElement(table);
        int status = ct.go(true);
        if (ColumnText.hasMoreText(status)) {
            column = Math.abs(column - 1);
            if (column == 0) {
                document.newPage();
                System.out.println("inserting new page with size :" + document.getPageSize());
            }
            ct.setSimpleColumn(
                COLUMNS_COORDS[column][0], COLUMNS_COORDS[column][1],
                COLUMNS_COORDS[column][2], COLUMNS_COORDS[column][3]);
            yLine = COLUMNS_COORDS[column][3];
            System.out.println("correcting yLine to: " + yLine);
        } else {
            ct.addElement(table);
        }
        ct.setYLine(yLine);
        System.out.println("before adding: " + ct.getYLine());
        status = ct.go(false);
        System.out.println("after adding: " + ct.getYLine());
        System.out.println("--------------------------------");
    }

    document.close();

Here you can see the result: http://d.pr/f/NEmx

Looking at the first page of the resulting PDF I assumed everything was working out fine.

But on second page you can see the problem(s):

  • text #31 is not displayed completely (first line + index is cut / not in visible area)
  • text #46 is not displayed completely (first three lines + index is cut / not in visible area)

On page 3 everything seems to be ok again. I am really lost here.

-- UPDATE (2013-03-14) --

I have analyzed the contents of the PDF now. The problem is not that content is show in non-visible areas but that the content is not present in the pdf at all. The missing part of the content is exactly the one which would have fit in the previous column / page. So it seems like ColumnText.go(true) is manipulating the object passed by addElement() before. Can someone confirm this? If so: what can I do about it?

-- end UPDATE (2013-03-14) --

Looking forward to your reply

regards, sven

回答1:

Solved! As soon as ColumnText indicates a table will not fit the current column I reinitialize ct with a new instance of ColumnText and add the table again.

In other words: Each instance of ColumnText is exactly dealing one column of my document.

if (ColumnText.hasMoreText(status) || mediaPoolSwitch) {
  column = Math.abs(column - 1);
  if (mediaPoolSwitch) {
      currentMediaPool = mediapool;
      column = 0;
  }
  if (column == 0) {
      document.newPage();
      writeTitle(writer.getDirectContent(), mediapool.getName());
  }
  ct = new ColumnText(writer.getDirectContent());
  ct.addElement(table);
  ct.setSimpleColumn(
      COLUMNS_COORDS[column][0], COLUMNS_COORDS[column][1],
      COLUMNS_COORDS[column][2], COLUMNS_COORDS[column][3]);
  yLine = COLUMNS_COORDS[column][3];
  LOG.debug("correcting yLine to: " + yLine);
} else {
  ct.addElement(table);
}
ct.setYLine(yLine);
ct.go();


标签: itext