【问题标题】:Android How to set margin top on table using itextAndroid如何使用itext在表格上设置边距
【发布时间】:2018-09-04 00:18:02
【问题描述】:

我创建了一个带有导出 pdf 选项的 android 应用程序,并使用 itext 插件创建 pdf 文档。我想设置 table 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();

    }

【问题讨论】:

    标签: android itext


    【解决方案1】:

    使用 iText7 更新至 2021 年 2 月

    您可以使用margin 属性:

    myTable.setMarginTop(20);
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2019-06-24
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多