phax / ph-pdf-layout

Java library for creating fluid page layouts with Apache PDFBox. Supporting multi-page tables, different page layouts etc.
Apache License 2.0
61 stars 11 forks source link

Issue with Centering Text in Single Line #31

Closed maxflu21 closed 1 year ago

maxflu21 commented 1 year ago

Hello,

I've been using your PH-PDF Layout library for a project and have run into an issue I was hoping you could provide some insight on.

Specifically, I've been having trouble with centering text within a single line. I've been using the .setHorzAlign(EHorzAlignment.CENTER) method on a PLText object, but it seems to only center the text if there's enough text to fill the line, like a long string or multiple strings separated by newline characters.

PLText textField = new PLText(
    "Some Text\n" +
    "-----------------------------------------------------------------------------------------------",
    new FontSpec (PreloadFont.REGULAR, 10)
).setHorzAlign(EHorzAlignment.CENTER);

In this case, the text is properly centered. But when I have a single line of text without the long string of dashes, the text is not centered.

Is there a workaround or method that I may have overlooked to get single line text to center properly within the bounding box?

Thank you in advance for your help and for the effort you've put into developing this library. It's been very useful for my project outside of this issue.

Best regards, Max

phax commented 1 year ago

Hi Max, that is a "classical issue". The PLText element is an "inline" element, as such it uses only the space it needs. So when it is multiline, it uses the width of the widest line, limited to the boundaries of the outer element. As such, to fullfiil your goal, you need to have an outer PLBox that encapsulates the text, and the PLBox as a "block element" needs the horicontal alignment. See the test issue in the above link. And here is the output of the test: center-issue31.pdf

maxflu21 commented 1 year ago

Thank you very much for your quick and helpful response! Your suggestion solved my problem and I greatly appreciate your support. It's wonderful that you've developed such a useful framework and share it with the community.