iText 5 getDefaultCell().setBorder(PdfPCell.NO_BORDER) has no effect

12,439

Solution 1

You are mixing two different concepts.

Concept 1: you define every PdfPCell manually, for instance:

PdfPCell cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);

In this case, you define every aspect, every property of the cell on the cell itself.

Concept 2: you allow iText to create the PdfPCell implicitly, for instance:

table.addCell("Adding a String");
table.addCell(new Phrase("Adding a phrase"));

In this case, you can define properties at the level of the default cell. These properties will be used internally when iText creates a PdfPCell in your place.

Conclusion:

Either you define the border for all the PdfPCell instances separately, or you let iText create the PdfPCell instances in which case you can define the border at the level of the default cell.

If you choose the second option, you can adapt your code like this:

PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
table.setWidthPercentage(100);
table.addCell(new Phrase("Menge", tfont));
table.addCell(new Phrase("Beschreibung", tfont));
table.addCell(new Phrase("Einzelpreis", tfont));
table.addCell(new Phrase("Gesamtpreis", tfont));
table.addCell(new Phrase("MwSt", tfont));
document.add(table);

This decision was made by design, based on experience: it offers the most flexible to work with cells and properties.

Solution 2

cell.setBorder(Rectangle.NO_BORDER);

you have to set NO_BORDER on cell.

e.g.

cell = new PdfPCell(new Phrase("Menge", tfont));
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);

refer itext here

Share:
12,439
Mathias Mahlknecht
Author by

Mathias Mahlknecht

Updated on June 04, 2022

Comments

  • Mathias Mahlknecht
    Mathias Mahlknecht about 2 years

    I'm new with iText and I'm trying to build a table. But for some reason table.getDefaultCell().setBorder(PdfPCell.NO_BORDER) has no effect, my table has still borders.

    Here is my code:

    PdfPTable table = new PdfPTable(new float[] { 1, 1, 1, 1, 1 });
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    Font tfont = new Font(Font.FontFamily.UNDEFINED, 10, Font.BOLD);
    table.setWidthPercentage(100);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Menge", tfont));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Beschreibung", tfont));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Einzelpreis", tfont));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Gesamtpreis", tfont));
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("MwSt", tfont));
    table.addCell(cell);
    document.add(table);
    

    Do you have any idea what I am doing wrong?