2016-07-31 5 views
1

J'utilise iText 7.0.0 (la saveur Java) et il semble que la cellule HorizontalAlignment soit ignorée car ni CENTER ni RIGHT ne fonctionnent. Pouvez-vous reproduire cela?La cellule de table HorizontalAlignment est ignorée/cassée

see the pdf screenshoot

et le code de reproduire:

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

Salut, je vous encourage à accepter la réponse de Bruno à cette question car je la trouve vraiment complète et il pourrait être utile de trouver plus facilement une réponse pour d'autres personnes qui rencontrent cette question. –

Répondre

2

S'il vous plaît jeter un oeil à l'CellAlignment exemple:

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(); 
} 

Le résultat PDF lorsque vous exécutez cet exemple ressemble à ceci:

enter image description here

Il n'y a pas de problème avec l'alignement, ni vertical, ni horizontal, ni alignement de texte.