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
我正在pdf文件上创建一个2 * 2表。我只是想把桌子的外边框,不需要显示内部细胞边界。我试过这样的
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
但它导致没有任何边框的表,请帮助
1 个解决方案
#1
2
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
.
如您所见,我们使用传递给tableLayout()方法的width和height参数来定义矩形的边框,然后将该矩形绘制到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.
请注意,我的代码是基于我书中的PressPreviews示例用Java编写的。有关相应的C#代码,请参阅iTextSharp示例。
#1
2
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
.
如您所见,我们使用传递给tableLayout()方法的width和height参数来定义矩形的边框,然后将该矩形绘制到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.
请注意,我的代码是基于我书中的PressPreviews示例用Java编写的。有关相应的C#代码,请参阅iTextSharp示例。