itext - 什么是易于打印的“第一个权利,然后是向下” [英] itext -- what's an easy to print "first right, then down"

查看:85
本文介绍了itext - 什么是易于打印的“第一个权利,然后是向下”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个 PdfPTable ,其中包含的行和列多于PDF的一页。

I would like to print a PdfPTable containing many more rows and columns than fit in one page of the PDF.

我想打印第一个权利,然后向下,这类似于行主要顺序。打印第一页 - 完整的行,但是这些行的所有列。然后打印下一页 - 完整的行,再打印这些行的所有列。

I would like to print "first right, then down", which is similar to row-major order. Print the first page-full of rows, but all columns for those rows. Then print the next page-full of rows, and again all columns for those rows.

据我所知,iText可以处理 PdfPTable 包含的行数多于页面上适合的行数,但无法处理超出页面适合的列数。所以我自己拆分列。但这意味着我最终打印出先下来然后正确,这不是我想要的。

As I understand it, iText can handle a PdfPTable containing more rows than fit on a page, but can't handle more columns than fit on a page. So I split the columns myself. But that means that I end up printing "first down, then right", which is not what I want.

有没有一种简单的方法可以做我想要的?请注意,我不知道提前行高,因为我在 PdfPCell 对象上调用 setMinimumHeight()

Is there an easy way to do what I want? Note that I do not know the row heights ahead of time, because I call setMinimumHeight() on the PdfPCell objects.

我能想到的唯一方法是笨拙,就像先前一样首先拆分列。然后,对于适合一个页面的每个列序列,只要它们适合,就逐个添加行到 PdfPTable 。如果他们不这样做,那就是我们需要拆分行的地方。最后,我们以每组行和列的 PdfPTable 对象结束。我们需要将它们全部保存在内存中,然后按照右(主要)顺序将它们添加到PDF文档中。

The only way I can think of, which is clumsy, is to first split columns, as before. Then, for each sequence of columns that fit on one page, add rows one by one to a PdfPTable as long as they fit. When they don't, that's where we need to split the rows. At the end of this, we end up with PdfPTable objects for each set of rows and columns. We need to keep all of them in memory and add them to the PDF document in the right (row-major) order.

这看起来相当不优雅,所以我想要检查是否有更清洁的解决方案。谢谢。

This seems rather inelegant, so I wanted to check if there's a cleaner solution. Thanks.

推荐答案

如果你有非常大的表,那么维护概览并在不同页面上分发表的最优雅方式,是通过将表添加到一个非常大的 PdfTemplate 对象,然后将该 PdfTemplate 对象的剪切部分添加到不同的页面。

If you have really large tables, the most elegant way to maintain the overview and to distribute the table over different pages, is by adding the table to one really large PdfTemplate object and then afterwards add clipped parts of that PdfTemplate object to different pages.

这就是我在所做的工作 TableTemplate 示例。

That's what I've done in the TableTemplate example.

我创建一个包含15列,总宽度为1500pt的表。

I create a table with 15 columns a total width of 1500pt.

PdfPTable table = new PdfPTable(15);
table.setTotalWidth(1500);
PdfPCell cell;
for (int r = 'A'; r <= 'Z'; r++) {
    for (int c = 1; c <= 15; c++) {
        cell = new PdfPCell();
        cell.setFixedHeight(50);
        cell.addElement(new Paragraph(String.valueOf((char) r) + String.valueOf(c)));
        table.addCell(cell);
    }
}

现在我把这个表写成一个Form XObject来测量1500到1300.注意1300是表的总高度。您可以这样得到:table.getTotalHeight()或者您可以将26行乘以50 pt的固定高度。

Now I write this table to a Form XObject that measures 1500 by 1300. Note that 1300 is the total height of the table. You can get it like this: table.getTotalHeight() or you can multiply the 26 rows by the fixed height of 50 pt.

PdfContentByte canvas = writer.getDirectContent();
PdfTemplate tableTemplate = canvas.createTemplate(1500, 1300);
table.writeSelectedRows(0, -1, 0, 1300, tableTemplate);

现在您已经拥有Form XObject,您可以在每个页面上重复使用它,如下所示:

Now that you have the Form XObject, you can reuse it on every page like this:

PdfTemplate clip;
for (int j = 0; j < 1500; j += 500) {
    for (int i = 1300; i > 0; i -= 650) {
        clip = canvas.createTemplate(500, 650);
        clip.addTemplate(tableTemplate, -j, 650 - i);
        canvas.addTemplate(clip, 36, 156);
        document.newPage();
    }
}

结果是 table_template.pdf 我们在第1页有单元格A1到M5,在第2页有单元格N1到Z5,单元格A6到第3页的M10,第4页的单元格N6到Z10,第5页的单元格A11到M15和第6页的单元格N11到Z15。

The result is table_template.pdf where we have cells A1 to M5 on page 1, cells N1 to Z5 on page 2, cells A6 to M10 on page 3, cells N6 to Z10 on page 4, cells A11 to M15 on page 5 and cells N11 to Z15 on page 6.

这回答了所有Google的答案周五。

This answers all Google's answers posted on Friday.

这篇关于itext - 什么是易于打印的“第一个权利,然后是向下”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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