2016-07-31 12 views
1

Ich benutze iText 7.0.0 (die Java-Flavor) und es scheint, dass die Tabellenzelle HorizontalAlignment ignoriert wird, da weder CENTER noch RECHTS funktioniert. Kannst du das reproduzieren?Tabellenzelle HorizontalAlignment ignoriert/gebrochen

see the pdf screenshoot

und den Code zu reproduzieren:

private static void brokenTableCellHorizontalAlignmentPdf(OutputStream output) throws IOException { 
    PdfWriter writer = new PdfWriter(output); 
    PdfDocument pdf = new PdfDocument(writer); 
    Document document = new Document(pdf); 
    PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA); 
    Table table = new Table(new float[] {15f, 16f, 4f}).setWidthPercent(100); 
    for (int y = 1; y <= 3; ++y) { 
     for (int x = 1; x <= 3; ++x) { 
      table.addCell(
        new Cell() 
          .setVerticalAlignment(VerticalAlignment.MIDDLE) 
          .setHorizontalAlignment(HorizontalAlignment.CENTER) 
          .add(new Paragraph(String.format("(%d, %d)%s", y, x, x == 1 ? "\n\ntest" : "")) 
            .setFont(font) 
            .setFontSize(8))); 
     } 
    } 
    document.add(table); 
    document.close(); 
} 
+0

Hallo, ich ermutige Sie, Brunos Antwort auf diese Frage zu akzeptieren, da ich es wirklich gründlich finde und es hilfreich sein könnte, leichter eine Antwort für andere Personen zu finden, die auf diese Frage stoßen. –

Antwort

2

Bitte nehmen Sie sich einen Blick auf das CellAlignment Beispiel:

public void createPdf(String dest) throws IOException { 
    //Initialize PDF document 
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 

    // Initialize document 
    Document document = new Document(pdf); 
    Table table = new Table(new float[]{2, 1, 1}); 
    table.setWidthPercent(80); 
    table.setHorizontalAlignment(HorizontalAlignment.CENTER); 
    table.setTextAlignment(TextAlignment.CENTER); 
    table.addCell(new Cell(1, 3).add("Cell with colspan 3")); 
    table.addCell(new Cell(2, 1).add("Cell with rowspan 2") 
     .setTextAlignment(TextAlignment.RIGHT)); 
    table.addCell("row 1; cell 1"); 
    table.addCell("row 1; cell 2"); 
    table.addCell("row 2; cell 1"); 
    table.addCell("row 2; cell 2"); 
    Cell cell = new Cell() 
     .add(new Paragraph("Left").setTextAlignment(TextAlignment.LEFT)) 
     .add(new Paragraph("Center")) 
     .add(new Paragraph("Right").setTextAlignment(TextAlignment.RIGHT)); 
    table.addCell(cell); 
    cell = new Cell().add("Middle") 
     .setVerticalAlignment(VerticalAlignment.MIDDLE); 
    table.addCell(cell); 
    cell = new Cell().add("Bottom") 
     .setVerticalAlignment(VerticalAlignment.BOTTOM); 
    table.addCell(cell); 
    document.add(table); 
    document.close(); 
} 

Die resultierende PDF, wenn Sie dieses Beispiel läuft wie folgt aussieht:

enter image description here

Es gibt kein Problem mit der Ausrichtung, weder vertikal, horizontal oder Textausrichtung.

Verwandte Themen