Skip to content

setCharacterSpacing affecting centre alignment #1280

@gdhnz

Description

@gdhnz

Not sure if a bug or I'm doing something wrong but when I try and set the character spacing for some text added to a ColumnText, it's affecting the centre alignment I'm trying to achieve.

If I don't set setCharacterSpacing, the paragraph is correctly aligned in the centre.

How can I centre text on a page using ColumnText and setCharacterSpacing?

public class SimplePdf {
    public static void main(String[] args) throws IOException {
        String text = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Morbi ac nisl cursus, feugiat mauris nec, tempor ipsum.
Vivamus sodales bibendum urna in iaculis.
Vivamus porta at nisi a interdum.
Quisque vehicula ipsum sed leo tempus semper.
In ultrices mattis odio, non.""";

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("simple-output.pdf"));

        Rectangle pageSize = document.getPageSize();

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        cb.beginText();
        cb.setCharacterSpacing(3.04f);
        cb.endText();

        Paragraph paragraph = new Paragraph(text, new Font(BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED), 10f));
        paragraph.setAlignment(Element.ALIGN_CENTER);

        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(paragraph, 0, pageSize.getHeight() - 300f, pageSize.getWidth(), pageSize.getHeight() - 170f, 16, Element.ALIGN_CENTER);
        ct.go();

        // Close everything
        document.close();
        writer.close();
    }
}

Version: openpdf 2.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions