簡體   English   中英

Android 如何使用 itext 在表格上設置邊距頂部

[英]Android How to set margin top on table using itext

我創建了一個帶有導出 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();

    }

您可以使用以下方法將一張表格與其余內容分開:

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

使用 iText7 更新至 2021 年 2 月

您可以使用margin屬性:

myTable.setMarginTop(20);

在版本 5.5.13.3 中

myTable.SpacingBefore = (7f);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM