即使锁定宽度后,itext表也不完全适合

时间:2016-08-10 11:25:09

标签: java itext

我正在尝试使用itext创建一个固定宽度的pdf。 pdf宽度固定为85.0f.pdf边框为“边界”和“边界”。宽度 为此我创建了一个宽度为85.0f-(border * 2)的表并向其添加数据。然后我找到它的高度和宽度并创建一个宽度为85.0f且高度为heightOftable +(border * 2)的Rectangle 然而,我得到的pdf边框和表之间的间距很小。 我想删除那个间距。 我已经尝试将填充设置为零等。但是,薄间距仍然存在。 我的代码是:

public class Template2100_2 {

     public static final String DEST = "D:\\uploads\\abc.pdf";
     public static void main(String[] args) throws DocumentException, IOException {
         Template2100_2 t=new Template2100_2();
         Font font = new Font(Font.FontFamily.TIMES_ROMAN, 7f, Font.NORMAL);

         t.createPdfOrdinary("The Commerce Ministry has received several references from various stakeholders seeking clarification", "E-mail ID\n"
                + " 1854213265\n"
                + " Ph: 12547869",1,font);
    }
    public void createPdfOrdinary(String chunk1Text, String chunk2Text,Integer border,Font font) throws DocumentException, IOException
    {


        Document document = new Document(PageSize.A4, 0, 0, 0, 0);
        PdfPTable table = new PdfPTable(1);
        table.setTotalWidth(85f-(border*2));
        table.setLockedWidth(true);
        //table.setWidthPercentage(100);

        table.setSpacingBefore(0f);
        table.setSpacingAfter(0f);


        PdfPCell cell;
        cell = new PdfPCell(new Phrase(chunk1Text));
        cell.setBorder(0);
        cell.setPadding(0);
        cell.setColspan(1);
        cell.isUseBorderPadding();
        cell.setPaddingBottom(1f);
        cell.setPaddingRight(-1f);
        cell.setBackgroundColor(BaseColor.PINK);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(chunk2Text));
        cell.setBorder(0);
        cell.setPadding(0);
        cell.setPaddingBottom(1f);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setRowspan(1);
        cell.setBackgroundColor(BaseColor.PINK);
        cell.isUseBorderPadding();



        table.addCell(cell);
        FileOutputStream fos=new FileOutputStream(DEST);
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(table);
        Float height=table.getTotalHeight();
        System.out.println(table.getTotalHeight()+"::"+table.getTotalWidth());
        document.close();
        fos.close();
        writer.close();
        Rectangle pagesize = new Rectangle(85f, height+(border*2));
        pagesize.setBorder(Rectangle.BOX);
        pagesize.setBorderWidth(border);
        document = new Document(pagesize, border, border,border, border);
        fos=new FileOutputStream(DEST);
        writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(table);

        document.close();
        fos.close();
        writer.close();
    }
}

任何人都可以建议边框和桌子之间的薄间距来自何处以及如何将其移除。谢谢。

2 个答案:

答案 0 :(得分:1)

您的代码太复杂了。在你编写代码之后必须维护代码的人不会那样。

如果这是您想要的结果:

enter image description here

然后这是您需要的代码:

public void createPdf(String dest) throws IOException, DocumentException {
    float width = 85;
    float border = 1;
    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(width - 2 * border);
    table.setLockedWidth(true);
    table.addCell("Some text in some cell.");
    table.addCell("In some very narrow table");
    Rectangle rect = new Rectangle(width, table.getTotalHeight() + 2 * border);
    Document document = new Document(rect, border, border, border, border);
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    document.add(table);
    document.close();
}

无需两次创建同一文档。只需从头开始使用正确的尺寸创建文档。

答案 1 :(得分:-1)

现在我可以使用以下方法删除该间距:

pagesize.setUseVariableBorders(true);