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.53k stars 581 forks source link

How to set a left aligned string and a center aligned string in a single line? #1215

Open playersun opened 1 month ago

playersun commented 1 month ago
StevenStreasick commented 1 month ago

You can use a PdfPTable to store two PdfPCells in the same row. One PDF can be set to left aligned, where the other string is set to center aligned. However, PdfPTable assigns a certain percentage of the width to each of its cells, meaning that the center aligned string will only be within the center of the assigned width. You can manually set the widths using a lot of math such that the second column, if left aligned, will appear to be globally centered in the page.

Alternatively, you can use the ColumnText class and set up two columns of text, with one left aligned and one middle aligned. The downside to this solution is that the columns have to be manually positioned.

playersun commented 1 month ago

You can use a PdfPTable to store two PdfPCells in the same row. One PDF can be set to left aligned, where the other string is set to center aligned. However, PdfPTable assigns a certain percentage of the width to each of its cells, meaning that the center aligned string will only be within the center of the assigned width. You can manually set the widths using a lot of math such that the second column, if left aligned, will appear to be globally centered in the page.

Alternatively, you can use the ColumnText class and set up two columns of text, with one left aligned and one middle aligned. The downside to this solution is that the columns have to be manually positioned.

OK, Got it. Thanks a lot!