PdfpTable vs. Table (vs. SimpleTable?)

后端 未结 1 1781
南笙
南笙 2021-01-20 11:27

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

相关标签:
1条回答
  • 2021-01-20 11:43

    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.

    0 讨论(0)
提交回复
热议问题