Half_Duplex
Half_Duplex

Reputation: 5234

iText 7 table ignoring my table border settings

I want a table with no borders. I've tried to set the border property, individual border properties, set the border manually, set the cells border to no border, etc. None remove the border. What is the proper way to have an iText 7 table with no border?

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputStream));
    Document doc = new Document(pdfDoc);
    PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);

    Table table = new Table(new float[] { 1, 1 });
    table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
    table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
    table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
    table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
    table.setProperty(Property.BORDER, Border.NO_BORDER);
    table.setBorder(Border.NO_BORDER);

    table.setWidthPercent(100);

    // Header
    File file = new ClassPathResource("logo.png").getFile();
    Image logo = new Image(ImageDataFactory.create(file.getPath()));

    Paragraph headerParagraph = new Paragraph();
    Text headerTitle = new Text("Title of PDF")
            .setFont(font)
            .setFontSize(20)
            .setFontColor(new DeviceRgb(0, 128, 128));
    Text headerDescription = new Text("Description")
            .setFont(font)
            .setFontSize(11);

    headerParagraph.add(headerTitle);
    headerParagraph.add(NEW_LINE);
    headerParagraph.add(headerDescription);

    table.addCell(logo);
    table.addCell(headerParagraph).setTextAlignment(TextAlignment.RIGHT);

None of these settings seem to work.Using iText 7.0.2

Upvotes: 0

Views: 3496

Answers (1)

Uladzimir Asipchuk
Uladzimir Asipchuk

Reputation: 2458

First of all, run the next snippet and see that iText7 can create tables without borders.

    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFileName));
    Document doc = new Document(pdfDoc);

    Table table = new Table(new float[] {50, 50 });

    Paragraph headerParagraph = new Paragraph();
    Text headerTitle = new Text("Title of PDF")
            .setFontSize(20)
            .setFontColor(new DeviceRgb(0, 128, 128));
    Text headerDescription = new Text("Description")
            .setFontSize(11);

    headerParagraph.add(headerTitle);
    headerParagraph.add(headerDescription);

    table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));
    table.addCell(new Cell().add(headerParagraph).setBorder(Border.NO_BORDER).setTextAlignment(TextAlignment.RIGHT));

    doc.add(table);

This is the line responsible for such "magic" :

table.addCell(new Cell().add("logo").setBorder(Border.NO_BORDER));

However there is no magic at all. By default, cells have borders in iText7 (0.5px solid black). So if you want to add a cell without border you should specify it by setting NO_BORDER as a cell border.

On the other hand, tables don't have borders by default (I mean bounding borders). So there is no need in these lines:

table.setProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
    table.setProperty(Property.BORDER_LEFT, Border.NO_BORDER);
    table.setProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
    table.setProperty(Property.BORDER_TOP, Border.NO_BORDER);
    table.setProperty(Property.BORDER, Border.NO_BORDER);
    table.setBorder(Border.NO_BORDER);

Also you should understand that table.setBorder(border) stands for table.setProperty(Property.BORDER, border). The same for table.setBorderLeft(border), etc.

Upvotes: 2

Related Questions