Zeljo
Zeljo

Reputation: 25

Android How to set margin top on table using itext

I created an android application with export pdf option and use itext plugin for creating pdf documents. I want to set table margin-top attribute because it shows on top of the document. Should I add paragraph between logo and table or is there other solution?

Here is my code:

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

    }

Upvotes: 2

Views: 4320

Answers (3)

sultan s. alfaifi
sultan s. alfaifi

Reputation: 324

In Version 5.5.13.3

myTable.SpacingBefore = (7f);

Upvotes: 0

Bogdan Android
Bogdan Android

Reputation: 1395

Updated to Feb - 2021 with iText7

You can use the margin property:

myTable.setMarginTop(20);

Upvotes: 1

marsop
marsop

Reputation: 323

You can use the following methods to separate one table from the rest of the content:

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

Upvotes: 5

Related Questions