繁体   English   中英

iText Java-垂直和水平拆分表格并添加标题行

[英]iText java - Split table vertically and horizontally and add header row

我已引用Bruno Lowagie的代码在垂直和水平方向拆分表。 但是我还有其他要求,例如将标头设置为它。 请在下面找到代码:

public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable tbl = new PdfPTable(new float[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
//loop through 100 rows
for (int r = 1; r <= 100; r++) {
    for (int c = 1; c <= 24; c++) {
        tbl.addCell(String.format("r%sc%s", r, c));
    }
}
PdfContentByte canvas = writer.getDirectContent();
tbl.setTotalWidth(1500);//set table width
float top = document.top() - document.topMargin() - 30;
float yPos = top;
int start = 0;
int stop = 0;
for (int i = 0; i < 100; i++) {
    yPos -= tbl.getRowHeight(i);
    if (yPos < 0) {
        stop = --i;
        table = getHeaderRow(table);
        tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
        document.newPage();
        tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas);
        start = stop;
        document.newPage();
        yPos = top;
    }
}
tbl.writeSelectedRows(0, 12, stop, -1, document.leftMargin(), top, canvas);
document.newPage();
tbl.writeSelectedRows(12, -1, stop, -1, 5, top, canvas);
document.close();
}

//Method to create header row
private static PdfPTable getHeaderRow(PdfPTable table) {
    BaseColor myColor = WebColors.getRGBColor("#475a6d");
    table.setWidthPercentage(100);
    Font f = new Font();
    f.setFamily("sans-serif");
    f.setSize(10);
    f.setColor(BaseColor.WHITE);
    PdfPCell c1 = new PdfPCell(new Phrase("Id", f));
    c1.setHorizontalAlignment(Element.ALIGN_CENTER);
    c1.setNoWrap(true);
    c1.setBackgroundColor(myColor);
    table.addCell(c1);
    //... and so on
}

这导致在表列生成的末尾添加标题行。 谁能帮我吗?

我自己找到了答案。 而不是在编写文档时添加标题行,而是在表创建本身中添加了它,然后重新编写代码,如下所示:

public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(dest));
document.open();
PdfPTable tbl = new PdfPTable(new float[]
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1});
//loop through 100 rows
for (int r = 1; r <= 100; r++) {
for (int c = 1; c <= 24; c++) {
    tbl.addCell(String.format("r%sc%s", r, c));
}
}
PdfContentByte canvas = writer.getDirectContent();
tbl.setTotalWidth(1500);//set table width
float top = document.top() - document.topMargin() - 30;
float yPos = top;
int start = 0;
int stop = 0;
for (int i = 0; i < 100; i++) {
yPos -= tbl.getRowHeight(i);
if (yPos < 0) {
    stop = --i;
    table = getHeaderRow(table);
if(start==0){
//If it is first row, header is already added
        tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
} else {
    //Adding first row explicitly
tbl.writeSelectedRows(0, 12, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas);
tbl.writeSelectedRows(0, 12, start, stop, document.leftMargin(), top, canvas);
}
    document.newPage();
if(start==0){
//If it is first row, header is already added
    tbl.writeSelectedRows(12, -1, start, stop, 5, top, canvas);
} else {
//Adding first row explicitly
tbl.writeSelectedRows(12, -1, 0, 1, document.leftMargin(), top+table.getRowHeight(0), canvas);
tbl.writeSelectedRows(12, -1, start, stop, document.leftMargin(), top, canvas);
}
    start = stop;
    document.newPage();
    yPos = top;
    }
}
document.close();
}

暂无
暂无

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

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