Reputation: 11
I'am creating a 2*2 table on pdf file. I Just want to put outer border for the table, No need to show the inner cells border. I have tried like this
var back= new PdfPTable(2); //table for back
back.DefaultCell.Border = 1;
PdfPCell cell20 = new PdfPCell(new Phrase("cell1", body));
cell20.Border = 0;
back.AddCell(cell20);
PdfPCell cell21 = new PdfPCell(new Phrase("cell2", body));
cell21.Border = 0;
cell21.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
back.AddCell(cell21);
PdfPCell cell22 = new PdfPCell(new Phrase("cell3"));
cell22.Border = 0;
cell22.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
back.AddCell(cell22);
PdfPCell cell23 = new PdfPCell(new Phrase("cell4", body));
cell23.Border = 0;
cell23.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
back.AddCell(cell23);
but it results a table without any border, please help
Upvotes: 0
Views: 1369
Reputation: 77606
You need to create a table event, for instance:
public class OuterBorder implements PdfPTableEvent {
public void tableLayout(PdfPTable table, float[][] width, float[] height,
int headerRows, int rowStart, PdfContentByte[] canvas) {
float widths[] = width[0];
float x1 = widths[0];
float x2 = widths[widths.length - 1];
float y1 = height[0];
float y2 = height[height.length - 1];
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.rectangle(x1, y1, x2 - x1, y2 - y1);
cb.stroke();
}
}
As you can see, we use the width
and height
parameter passed to the tableLayout()
method to define the borders of a rectangle, and we draw that rectangle to the LINECANVAS
.
For this table event to work, you need to declare it to the table. In your case, that would be:
back.setTableEvent(new OuterBorder());
Note that my code is written in Java based on the PressPreviews example from my book. For the corresponding C# code, please consult the iTextSharp examples.
Upvotes: 2