LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.
Other
3.61k stars 597 forks source link

Prevent leading from affecting the first cell #1189

Closed StevenStreasick closed 4 months ago

StevenStreasick commented 4 months ago

The Problem I've been using OpenPDF for several months to create professional documents. While it's been a valuable tool overall, there's one feature, leading, that I find problematic and currently avoid using. Leading in OpenPDF leads to undesired behaviors, particularly with vertical alignment being ignored in tightly packed PDFs. You can find a more detailed discussion and my initial thoughts at https://github.com/LibrePDF/OpenPDF/discussions/1175.

The Solution As discussed, my suggestion is to prevent leading from affecting the first line, which aligns better with the typical definition of leading. This can be achieved by adding a simple boolean within the go(boolean) method of the columnText class which would act as a flag for whether the first cell is being iterated over or not.

Alternatives To work around the leading issue, I found that you could create your own ColumnText, but this becomes a lot of work to fix this one little bug. Additionally, I found that one could add additional padding to the bottom of the cell that could match the amount that leading is adding to the top, however, this often times creates a larger cell than what I would desire. The last alternative that I found is to simply not use leading, which is the practice I've adopted within my PDFs.

Please feel free to reach out to me or reply to this forum if you would like to further discuss these ideas.

Best Regards, Steven

andreasrosdal commented 4 months ago

Thank you for reporting. Pull requests welcome, with the solution you proposed.

StevenStreasick commented 4 months ago

As I was looking into implementing this, I realized that ColumnTexts and PdfPCells have a variable called 'useAscender' which handles this already (Can be set with the setUseAscender method). If useAscender is true, then it ignores the leading in favor of the Ascender value for the first row. For all of my purposes, Ascender will be practically, if not zero on the first row, simulating my proposed solution.