PdfpTable vs. Table (vs. SimpleTable?)

2019-02-26 15:39发布

问题:

I am writing code that generates both PDF and RTF documents, depending on the user's selection. The information in both documents is the same.

Until now, we were using classes like Table, Cell, HeaderFooter, etc., which are rendered into the correct form by the documentwriter (either PdfWriter or RtfWriter2).

However, the further I get into trying to make changes (mostly because the upgrade to iText 2.1 left the spacing a little quirky), the more I'm being told to "use PdfpTable", or "Use pageEvents to set headers". It seems like a lot of the generic classes are being deprecated.

I am thinking of revamping the code to separate out the table creations for PDF and RTF. However, all the iText tutorial stuff seems to actually recommend using Table in an instance where both PDF and RTF documents are needed.

Additionally, it appears that while there is a PdfpTable class, there is nothing similar for RTF, so I would be using Table anyway, which would leave me vulnerable to all the quirks that come along with the now unsupported Table class.

Basically, what are the benefits/downsides of separating the code into two sections -- one to create PDF documents, and one to create the same documents in an RTF format, if that was selected? And where does SimpleTable come in to all of this? Will it give me the flexibility that I need?

Thanks!

回答1:

From the book iText In Action, end of chapter 6:

If you look at the iText API, you’ll also find some other table classes. com.lowagie.text.Table is the original table class; it dates from the early iText days. It uses class com.lowagie.text.pdf.PdfTable internally to render a table to PDF (don’t confuse this class with PdfPTable).
There’s also the newer SimpleTable class, which tries to form a link between PdfPTable and Table. It’s able to translate itself to a PdfPTable if you add it to a document that writes PDF or to a Table if you’re producing HTML or RTF. [...]
The major disadvantage of the Table class is that it’s no longer supported. Different people have fixed most of the known issues, but today not a single person understands if and how all the Table-methods work. If you decide to use this class, you’re more or less on your own, and you’ll encounter lots of quirky layout issues based on historical design decisions. However, this doesn’t mean you can’t make good use of the Table class.

Advantages of the Table class

With the Table class, you can generate a table structure that can be rendered in PDF, RTF, and HTML. If you compare the results, you’ll see there are small differences in the way the table is rendered. This is normal; not every table feature is supported in every document format.

  1. You can generate a table in PDF, HTML, or RTF using the same code.
  2. You can set padding and spacing the way it’s done in HTML.
  3. You can use the row span without having to resort to nested table.
  4. You can change the number of columns even after you’ve added cells.
  5. You can add cells at specific positions (the number of rows is augmented dynamically).
  6. You can delete a column before adding the table to the document.
  7. You can let iText add the Table as if it was a PdfPTable.
  8. You get a PdfPTable object based on the Table object.

As opposed to PdfPTable, you can add cells to a Table in a random order, and add or delete columns if needed. You can even translate a Table to a PdfPTable if you didn’t use setRowspan().

There’s also the SimpleTable, class, which is a simplified version of (PdfP)-Table. When adding a SimpleTable to a PDF document, iText first attempts to add the table as a PdfPTable; if this fails, it’s added as a Table. When adding a SimpleTable to an RTF or HTML document, it’s added as a Table. SimpleTable differs from the Table and PdfPTable in the sense that it reintroduces the concept of rows. This can be handy if you’re parsing an XML file that has a table-row-cell structure. If the tag corresponding with the rows has attributes, you don’t have to define this property for each cell in the row separately; you can set the property for the entire row at once.

Summary

PdfPTable should be your first choice; but depending on the requirements defined for your project, there can be good reasons to opt for Table or SimpleTable.



标签: itext