pdf文档

时间:2017-05-15 09:58:48

标签: java itext

我从iText TableFooter开始了this示例。

但我无法正确设置页脚。

这是我的问题图片: 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);
}
}

1 个答案:

答案 0 :(得分:0)

您应该将marginBottom设置为文档。它将指示pdf内容,它应该在哪里结束,并让你的页脚分开。

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

结果: Pdf with 72 marginBottom