简体   繁体   English

如何使用itext 7将表格添加到带有页眉和页脚的页面

[英]how to add a table to a page with header and footer using itext 7

i need to add a table in a specific position and be repeated in all pdf pages without interference with header and footer this is my code我需要在特定位置添加一个表格,并在所有 pdf 页面中重复,而不会干扰页眉和页脚 这是我的代码

PdfEventHandler handler = new PdfEventHandler(header, x,y);
pdf.addEventHandler(PdfDocumentEvent.START_PAGE, handler);
Table table = new Table(getcolumnsWidth(pdfColumns, tableWidth));
String line = br.readLine();
process(table, line, true);
while ((line = br.readLine()) != null) {
    process(table, line, false);
}
br.close();
document.add(table);

with

public class PdfEventHandler implements IEventHandler {

    private String header;
    private int x,y;

    public PdfEventHandler(String header,int x , int y) {
     this.header =header;
     this.x = x; this.y =y;
    }

    @Override
    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        PdfPage page = docEvent.getPage();
        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
        Rectangle area = page.getPageSize();
        int pageHeight = (int) area.getHeight();
        Canvas canvas = new Canvas(pdfCanvas, pdfDoc, area);

        canvas.showTextAligned(header, x, y, TextAlignment.CENTER);
    }
}

this is the result pdf file这是结果pdf文件在此处输入图片说明

That's very unclear what you try to achieve, so I've made several assumptions. 尚不清楚您要实现什么目标,因此我做了几个假设。

First: you create your pdf from scratch (there is no source file). 第一:您从头创建pdf(没有源文件)。 Second: you want to place a table on each page (onto specific area) and the table should not interfere with the other content. 第二:您想在每个页面上放置一个表格(到特定区域),并且该表格不应干扰其他内容。

So how to place a table (or another element) on each page? 那么如何在每个页面上放置一个表(或另一个元素)呢? One can use an event handler for it. 可以为其使用事件处理程序。 Please look at the next example: https://github.com/itext/i7js-examples/blob/develop/src/test/java/com/itextpdf/samples/sandbox/events/TableFooter.java There a table is added as a footer once a new page is created. 请查看下一个示例: https : //github.com/itext/i7js-examples/blob/develop/src/test/java/com/itextpdf/samples/sandbox/events/TableFooter.java在此添加了一个表创建新页面后的页脚。

As for you code I believe that you should have used the showTextAligned which requires a page number: http://itextsupport.com/apidocs/itext7/latest/com/itextpdf/layout/RootElement.html#showTextAligned-com.itextpdf.layout.element.Paragraph-float-float-int-com.itextpdf.layout.property.TextAlignment-com.itextpdf.layout.property.VerticalAlignment-float- 至于您的代码,我相信您应该使用需要页码的showTextAligned: http ://itextsupport.com/apidocs/itext7/latest/com/itextpdf/layout/RootElement.html#showTextAligned-com.itextpdf.layout .element.Paragraph浮子浮子-INT-com.itextpdf.layout.property.TextAlignment-com.itextpdf.layout.property.VerticalAlignment-浮子

使用 table.setRelativePosition(0, 0, 0, 0) 将表格内容放在 pdf 上重复的标题之后

   for (int i = 0; i < 2; i++) {
     table.addCell("title 1");
    table.addCell("title 2");
    //header row and footer
    }
    table.setHeaderRows(2);  //two header rows ... 
    table.setFooterRows(1); // Of which one is a footer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM