Java 页脚pdf文档

Java 页脚pdf文档,java,itext,Java,Itext,我使用了iTextTableFooter中的示例 但我无法正确设置页脚 以下是我的问题的图片: 这是我的密码,哪里有错 public class App { public static final String DEST = "c:/radek-folder/app.pdf"; protected int horizontalAlignmentLeft = Element.ALIGN_LEFT; protected int horizontalAlignmentCenter = Element

我使用了
iText
TableFooter
中的示例

但我无法正确设置页脚

以下是我的问题的图片: 这是我的密码,哪里有错

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);
}
}
公共类应用程序{
公共静态最终字符串DEST=“c:/radek folder/app.pdf”;
受保护的整数水平对齐左=Element.ALIGN_LEFT;
受保护的int horizontalAlignmentCenter=Element.ALIGN\u CENTER;
protected int verticalAlignmentMiddle=Element.ALIGN_MIDDLE;
protected int verticalAlignmentTop=Element.ALIGN_TOP;
受保护字符串fontTypeRegular=“c:/radek文件夹/font_sitebook.ttf”;
受保护字符串fontTypeBold=“c:/radek文件夹/font\u sitebook\u bold.ttf”;
受保护的浮点fontSizeRegular=10f;
受保护的float fontSizeLarger=14f;
受保护的浮动字体大小等于16f;
公共静态void main(字符串[]args)引发IOException、DocumentException{
文件文件=新文件(DEST);
文件.getParentFile().mkdirs();
新建应用().createPdf(DEST);
系统输出打印项次(“完成”);
}
公共类FooterTable扩展PdfPageEventHelper{
受保护的PDFP表尾;
公共页脚表(PdfPTable页脚){
this.footer=页脚;
}
公共作废onEndPage(PDF编写器、文档){
writeSelectedRows(0,-1,36,64,writer.getDirectContent());
}
}
public void createPdf(字符串dest)引发IOException、DocumentException{
浮动柱宽=60;
float largeColumnWidth=105;
浮动宽度=150;
float[]行={smallColumnWidth,smallColumnWidth,smallColumnWidth,
smallColumnWidth,smallColumnWidth,largeColumnWidth,largeColumnWidth,
gigaColumnWidth,largeColumnWidth,largeColumnWidth,largeColumnWidth,
largeColumnWidth,largeColumnWidth,largeColumnWidth,largeColumnWidth,
gigaColumnWidth,largeColumnWidth,largeColumnWidth,largeColumnWidth,
largeColumnWidth,largeColumnWidth,largeColumnWidth,largeColumnWidth};
浮动总宽度=sumOfrow(行);
文档=新文档(PageSize.A1.rotate());
PdfWriter writer=PdfWriter.getInstance(文档,新文件输出流(dest));
float x=document.bottomMargin();
PDFPTableFooter=新的PdfPTable(1);
tableFooter.setTotalWidth(523);
PdfPCell cell=新的PdfPCell(新短语(“这是测试文档”);
单元格.背景色(基色.橙色);
tableFooter.addCell(单元格);
cell=新的PdfPCell(新短语(“这是版权声明”);
单元格.背景色(基色.浅灰色);
tableFooter.addCell(单元格);
FooterTable事件=新的FooterTable(tableFooter);
writer.setPageEvent(事件);
document.open();
//标题部分
PdfPTable mainTable=新的PdfPTable(1);
mainTable.getDefaultCell().setPadding(0);
mainTable.setTotalWidth(总宽度);
mainTable.setLockedWidth(真);
PdfPTable SUBTABLE 1=新的PdfPTable(23);
子表1.setTotalWidth(行);
子表1.setLockedWidth(真);
对于(int i=0;i<200;i++){
AddCellToTable捷克语(子表1,水平对齐中心,
垂直排列中间,“Čslo”,23,1,fontTypeBold,
丰大小规则);
}
mainTable.addCell(子表1);
文件。添加(主表);
document.close();
}
专用浮动sumOfrow(浮动[]行){
浮动结果=0;
用于(浮动f:行){
结果+=f;
}
返回结果;
}
私有静态void addCellToTableCzech(PdfPTable datatable,int horizontalAlignment,
int垂直对齐,字符串值,int colspan,int rowspan,
字符串字体类型、浮点字体大小){
BaseFont base=null;
试一试{
base=BaseFont.createFont(fontType,BaseFont.CP1250,BaseFont.EMBEDDED);
}捕获(例外e){
e、 printStackTrace();
}
Font Font=新字体(基本字体、字体大小);
PdfPCell cell=新的PdfPCell(新短语(值、字体));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
单元格。设置水平对齐(水平对齐);
细胞垂直排列(垂直排列);
cell.setboorder(PdfPCell.BOX);
datatable.addCell(cell);
}
}

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

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

@user7968180添加图像。您有不同的输出吗?我复制了我在这个问题中发布的相同代码,并根据您的建议仅更改了文档声明。我的产量仍然相等。我没有忘记清理我的项目。这很神秘…@user7968180,很奇怪。你能试着增加
marginBottom
更多吗?例如,多达200个。也许还有其他人。应该有区别。无论我如何改变这个利润率-输出仍然是相等的。我不知道该怎么想。你更改了应用程序的Pdf4form,你忘了说:-)这就是我代码中的问题。现在工作正常,谢谢。正确更新了超链接。