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

PdfPCell.setHorizontalAlignment not working at all #1220

Closed sciurid closed 2 months ago

sciurid commented 2 months ago

PLEASE FILL THIS TEMPLATE AS MUCH AS POSSIBLE. REMOVE IRRELEVANT PARTS.

Describe the bug

PdfPCell.setHorizontalAlignment() does not work. No matter what aligment parameter is specified, elements added are still left aligned.

To Reproduce

Code to reproduce the issue

        File pdf = File.createTempFile("test-", ".pdf");
        Document doc = new Document();
        PdfWriter.getInstance(doc, new FileOutputStream(pdf));
        doc.open();

        PdfPTable table = new PdfPTable(3);
        table.setWidths(new float[]{3, 3, 3});

        PdfPCell a1 = new PdfPCell();
        a1.setHorizontalAlignment(Element.ALIGN_LEFT);
        a1.addElement(new Chunk("A1"));
        table.addCell(a1);

        PdfPCell a2 = new PdfPCell();
        a2.setHorizontalAlignment(Element.ALIGN_CENTER);
        a2.addElement(new Phrase("A2: Phrase"));
        table.addCell(a2);

        PdfPCell a3 = new PdfPCell();
        a3.setHorizontalAlignment(Element.ALIGN_RIGHT);
        a3.addElement(new Paragraph("A3: Paragraph"));
        table.addCell(a3);

        doc.add(table);
        doc.close();

Expected behavior

It is expected that in the pdf file, the text in cell A1, A2, A3 should be aligned respectively to left, center and right.

Screenshots

image

System

(please complete the following information)

Your real name

Chen Qiang (chenqiang@tsinghua.edu.cn)