how to create table in pdf document last page bott

2019-07-23 16:51发布

问题:

I have created table in pdf document using itext in java. Working fine in functionality wise. But I need to display table as a below of document.

this is my code

PdfPTable datatablebottom = new PdfPTable(8);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setColspan(8);
            cell.setBackgroundColor(BaseColor.GRAY);
            cell.setBorderWidthTop(5.0f);
            cell.setBorderColorTop(BaseColor.DARK_GRAY);

            if(msgfb.equals("1")){
                //document.add(new Paragraph(""));
                cell.addElement(new Paragraph(""));
            }else if(msgfb.equals("2")){
                //document.add(new Paragraph("Thank you for your business"));
                Paragraph pf = new Paragraph("Thank you for your business Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness Thanks for your bussiness",BT_NORMAL);
                pf.setAlignment(Element.ALIGN_CENTER);
                cell.addElement(pf);
            }else{
                //document.add(new Paragraph(msgfb));
                Paragraph pf = new Paragraph(msgfb,BT_NORMAL);
                pf.setAlignment(Element.ALIGN_CENTER);
                cell.addElement(pf);
                //cell.addElement(new Paragraph(msgfb,BT_NORMAL));
            }
            cell.setPaddingBottom(10.0f);
            datatablebottom.addCell(cell);
            datatablebottom.setTotalWidth(PageSize.A4.getWidth()-70);
            datatablebottom.setLockedWidth(true);
            document.add(datatablebottom);

回答1:

You need to define the absolute width of your table:

datatable.setTotalWidth(document.right(document.rightMargin()) - document.left(document.leftMargin()));

Then you need to replace the line:

document.add(datatablebottom);

with this one:

datatable.writeSelectedRows(0, -1, document.left(document.leftMargin()), datatable.getTotalHeight() + document.bottom(document.bottomMargin()), writer.getDirectContent());

The writeSelectedRows() method draws the table at an absolute position. We calculate that position by asking the document for its left margin (the x value) and by adding the height of the table to the bottom margin of the document (the Y coordinate). We draw all rows (0 to -1).



标签: java itext