phax/ph-pdf-layout

Issue with Centering Text in Single Line

Closed this issue · 2 comments

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

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

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.