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

I am new to iText and I am trying to create a table. But for some reason table.getDefaultCell().setBorder(PdfPCell.NO_BORDER)

has no effect, my table still has 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?

+3


source to share


4 answers


You are mixing two different concepts.

Concept 1: you define each one PdfPCell

manually, for example:

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 in the cell itself.

Concept 2: you allow iText to be created PdfPCell

implicitly, e.g .:

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

      



In this case, you can define default cell-level properties. These properties will be used internally if iText builds PdfPCell

in your place.

Output:

Either you define a border for all instances PdfPCell

separately, or let iText instantiate PdfPCell

, in which case you can define a cell-level border by default.

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 on a project based on experience: it offers the most flexible handling of cells and properties.

+6


source


cell.setBorder (Rectangle.NO_BORDER);

you need to install NO_BORDER

to cell.

eg.



cell = new PdfPCell (new phrase ("Menge", tfont))

cell.setBorder (Rectangle.NO_BORDER);
table.addCell (cell);

refers to itext here

+1


source


For the entire cell in the table:

 table.AddCell(new Phrase(DepInfo.ElementAt(0), font));
 table.AddCell(iText.Image.GetInstance(t));
 table.AddCell(third);
 foreach (var c in table.Rows.SelectMany(r => r.GetCells()))
   c.Border = PdfPCell.NO_BORDER;

      

0


source


If you want to create a new PdfPCell that inherits all the default table cell settings, you should use 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);
PdfPCell cell = new PdfPCell(table.getDefaultCell());
cell.setPhrase(new PdfPCell(new Phrase("Menge", tfont)));
table.addCell(cell);
document.add(table);

      

0


source







All Articles