2017-05-15 2 views
0

Ich ging entlang this Beispiel von iTextTableFooter.Footers von pdf-Dokument

aber ich bin nicht in der Lage, Fußzeilen richtig einzustellen.

Hier ist ein Bild von meinem Problem: enter image description here Hier ist mein Code, wo habe ich einen Fehler?

public class App { 

public static final String DEST = "c:/radek-folder/app.pdf"; 
protected int horizontalAlignmentLeft = Element.ALIGN_LEFT; 
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER; 
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE; 
protected int verticalAlignmentTop = Element.ALIGN_TOP; 
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf"; 
protected String fontTypeBold = "c:/radek-folder/font_sitebook_bold.ttf"; 
protected float fontSizeRegular = 10f; 
protected float fontSizeLarger = 14f; 
protected float fontSizeLarge = 16f; 

public static void main(String[] args) throws IOException, DocumentException { 
    File file = new File(DEST); 
    file.getParentFile().mkdirs(); 
    new App().createPdf(DEST); 
    System.out.println("done"); 
} 

public class FooterTable extends PdfPageEventHelper { 
    protected PdfPTable footer; 

    public FooterTable(PdfPTable footer) { 
     this.footer = footer; 
    } 

    public void onEndPage(PdfWriter writer, Document document) { 
     footer.writeSelectedRows(0, -1, 36, 64, writer.getDirectContent()); 
    } 
} 

public void createPdf(String dest) throws IOException, DocumentException { 
    float smallColumnWidth = 60; 
    float largeColumnWidth = 105; 
    float gigaColumnWidth = 150; 
    float[] row = { smallColumnWidth, smallColumnWidth, smallColumnWidth, 
      smallColumnWidth, smallColumnWidth, largeColumnWidth, largeColumnWidth, 
      gigaColumnWidth, largeColumnWidth, largeColumnWidth, largeColumnWidth, 
      largeColumnWidth, largeColumnWidth, largeColumnWidth, largeColumnWidth, 
      gigaColumnWidth, largeColumnWidth, largeColumnWidth, largeColumnWidth, 
      largeColumnWidth, largeColumnWidth, largeColumnWidth, largeColumnWidth }; 
    float totalWidth = sumOfrow(row); 
    Document document = new Document(PageSize.A1.rotate()); 
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 

    float x = document.bottomMargin(); 
    PdfPTable tableFooter = new PdfPTable(1); 
    tableFooter.setTotalWidth(523); 
    PdfPCell cell = new PdfPCell(new Phrase("This is a test document")); 
    cell.setBackgroundColor(BaseColor.ORANGE); 
    tableFooter.addCell(cell); 
    cell = new PdfPCell(new Phrase("This is a copyright notice")); 
    cell.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    tableFooter.addCell(cell); 
    FooterTable event = new FooterTable(tableFooter); 
    writer.setPageEvent(event); 

    document.open(); 
    // Header part 

    PdfPTable mainTable = new PdfPTable(1); 
    mainTable.getDefaultCell().setPadding(0); 
    mainTable.setTotalWidth(totalWidth); 
    mainTable.setLockedWidth(true); 

    PdfPTable subTable1 = new PdfPTable(23); 
    subTable1.setTotalWidth(row); 
    subTable1.setLockedWidth(true); 
    for (int i = 0; i < 200; i++) { 
     addCellToTableCzech(subTable1, horizontalAlignmentCenter, 
       verticalAlignmentMiddle, "Číslo", 23, 1, fontTypeBold, 
       fontSizeRegular); 
    } 

    mainTable.addCell(subTable1); 
    document.add(mainTable); 
    document.close(); 
} 

private float sumOfrow(float[] row) { 
    float result = 0; 
    for (float f : row) { 
     result += f; 
    } 
    return result; 
} 

private static void addCellToTableCzech(PdfPTable datatable, int horizontalAlignment, 
     int verticalAlignment, String value, int colspan, int rowspan, 
     String fontType, float fontSize) { 
    BaseFont base = null; 
    try { 
     base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    Font font = new Font(base, fontSize); 
    PdfPCell cell = new PdfPCell(new Phrase(value, font)); 
    cell.setColspan(colspan); 
    cell.setRowspan(rowspan); 
    cell.setHorizontalAlignment(horizontalAlignment); 
    cell.setVerticalAlignment(verticalAlignment); 
    cell.setBorder(PdfPCell.BOX); 
    datatable.addCell(cell); 
} 
} 
+0

richtig auf den Hyperlink aktualisiert. –

Antwort

0

Sie sollten marginBottom auf das Dokument gesetzt. Es zeigt den PDF-Inhalt an, wo er enden soll, und lässt die Fußzeile getrennt erscheinen.

Document document = new Document(PageSize.A1.rotate(), 36, 36, 36, 72); 

Ergebnis: Pdf with 72 marginBottom

+0

funktioniert immer noch nicht – user7968180

+0

@ user7968180 Bild hinzufügen. Hast du unterschiedliche Ausgaben? – Sergey

+0

Ich kopierte den gleichen Code, was ich hier in dieser Frage gepostet habe und änderte nur Dokument Deklaration nach Ihrem Rat. Meine Ausgabe ist immer noch gleich. Und ich habe nicht vergessen, mein Projekt zu säubern. Es ist ziemlich mysteriös ... – user7968180

Verwandte Themen