【问题标题】:itext table not fitting exactly even after locking width即使在锁定宽度后,itext 表也不完全适合
【发布时间】:2016-08-10 11:25:09
【问题描述】:

我正在尝试使用 itext 创建具有固定宽度的 pdf。 pdf 宽度固定为 85.0f。pdf 的边框为“边框”宽度 为此,我创建了一个宽度为 85.0f-(border*2) 的表格并向其中添加数据。然后我找到它的高度和宽度并创建一个宽度为 85.0f 和高度为 heightOftable+(border*2) 的矩形 然而,我得到的 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();
    }
}

任何人都可以建议边框和表格之间的细间距来自哪里以及如何删除它。谢谢。

【问题讨论】:

  • 您的示例中的代码过多。删除与您的问题无关的部分。愿意回答您问题的人可能会认为您要求他或她花费太多时间来区分相关和不相关的内容。

标签: java itext


【解决方案1】:

您的代码太复杂了。在你编写完代码后必须维护代码的人不会喜欢这样。

如果这是你想要的结果:

那么这就是你需要的代码:

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

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

【讨论】:

    【解决方案2】:

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

    pagesize.setUseVariableBorders(true);
    

    【讨论】:

    • 这不是答案,是吗?这是对原始问题的附加说明。您应该添加此类注释作为评论,或更新您的问题。
    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2011-10-15
    • 2013-11-16
    • 1970-01-01
    相关资源
    最近更新 更多