Android如何使用itext在表上设置保证金

时间:2018-03-25 22:14:27

标签: android itext

我创建了一个带有export pdf选项的android应用程序,并使用itext插件创建pdf文档。我想设置表margin-top属性,因为它显示在文档的顶部。我应该在徽标和表格之间添加段落还是有其他解决方案?

这是我的代码:

private static void createTable(Document document)
        throws BadElementException {
    try {
        PdfPTable table = new PdfPTable(5);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);


        PdfPCell c1 = new PdfPCell(new Phrase("Col2"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        c1.setFixedHeight(20);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Col2"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        c1.setFixedHeight(20);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Col3"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        c1.setFixedHeight(20);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Col4"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        c1.setFixedHeight(20);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Col5"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
        c1.setFixedHeight(20);
        table.addCell(c1);
        table.setHeaderRows(1);

        table.addCell("1.0");
        table.addCell("1.1");
        table.addCell("1.2");
        table.addCell("1.3");
        table.addCell("1.4");

        table.addCell("2.0");
        table.addCell("2.1");
        table.addCell("2.2");
        table.addCell("2.3");
        table.addCell("2.4");

        document.add(table);
    } catch (Exception e) {
        e.printStackTrace();

    }

2 个答案:

答案 0 :(得分:1)

使用 iText7 更新至 2021 年 2 月

您可以使用 margin 属性:

myTable.setMarginTop(20);

答案 1 :(得分:0)

您可以使用以下方法将一个表与其余内容分开:

    myTable.setSpacingBefore(10);
    myTable.setSpacingAfter(15);