2017-05-15 5 views
0

に沿って行った例はiTextTableFooterです。PDFファイルのフッター

私はフッターを正しく設定できません。

ここに私の問題の写真があります: enter image description here ここに私のコードはありますが、私は間違いがありますか?

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

が正しくハイパーリンクを更新しました。 –

答えて

0

marginBottomを文書に設定する必要があります。それはpdfの内容を示し、終了する必要があります。また、フッターを分けることができます。

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

結果: Pdf with 72 marginBottom

+0

まだ動作しません – user7968180

+0

@ user7968180 Imageを追加してください。別の出力がありますか? – Sergey

+0

私はこの質問にここに投稿したものと同じコードをコピーし、あなたのアドバイスに従って文書の宣言だけを変更しました。 私の出力は同じです。そして私は自分のプロジェクトをきれいにすることを忘れなかった。 それはかなり謎です... – user7968180

関連する問題