iText - 使用ColumnText进行奇怪的列/页面更改行为 [英] iText - Strange column- / page changing behaviour with ColumnText

查看:207
本文介绍了iText - 使用ColumnText进行奇怪的列/页面更改行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iText的新手并试图完成以下任务:

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


  • 从本地hd读取文本文件列表

  • 以2列布局pdf文件排列文件的文本

  • 在每个文本之前添加连续编号的索引

我开始使用MovieColumns1示例( http://itextpdf.com/examples/iia.php?id=64 )并最终得到以下代码:

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

在这里你可以看到结果:
http://d.pr/f/NEmx

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

查看第一页结果PDF我假设一切正常。

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


  • 文本#31未完全显示(第一行+索引被剪切/不在可见区域)

  • 文本#46未完全显示(第一行三行+索引被切断/不在可见区域内)

在第3页,一切似乎都没问题。我真的迷失了。

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

- 更新(2013-03-14) -

我现在已经分析了PDF的内容。问题不在于内容是在非可见区域中显示的,而是内容在pdf中根本不存在。内容的缺失部分恰好是适合前一列/页面的部分。所以似乎ColumnText.go(true)正在操纵addElement()之前传递的对象。有人可以证实吗?如果是这样的话:我该怎么办呢?

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?

- 结束更新(2013-03-14) -

期待您的回复

问候,
sven

regards, sven

推荐答案

解决了!只要ColumnText指示表不适合当前列,我就使用ColumnText的新实例重新初始化ct并再次添加表。

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.

换句话说:
ColumnText的每个实例都正好处理我文档的一列。

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 - 使用ColumnText进行奇怪的列/页面更改行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆