如何在Java中使用itext在pdf文档的最后一页底部创建表 [英] how to create table in pdf document last page bottom using itext in java

查看:1216
本文介绍了如何在Java中使用itext在pdf文档的最后一页底部创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Java中使用itext在pdf文档中创建了表格.在功能方面做得很好. 但是我需要将表格显示为文档的下方.

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.

这是我的代码

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);

推荐答案

您需要定义表格的绝对宽度:

You need to define the absolute width of your table:

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

然后您需要替换该行:

document.add(datatablebottom);

与此:

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

writeSelectedRows()方法在绝对位置绘制表格.我们通过询问document的左边缘(x值)并将表格的高度添加到文档的底部边缘(Y坐标)来计算该位置.我们绘制所有行(0到-1).

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在pdf文档的最后一页底部创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆