如何使用itext sharp在pdf中仅设置表格的垂直线?

Sem*_*ian 7 c# itextsharp

我有一张垂直和水平线条的桌子.但我不想要水平线.我只想要垂直线.我怎么设置它.我预期的o/p是

我的表格代码

PdfPTable table = new PdfPTable(5);
table.TotalWidth = 510f;//table size
table.LockedWidth = true;
table.HorizontalAlignment = 0;
table.SpacingBefore = 10f;//both are used to mention the space from heading


table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(new Phrase(new Phrase("    SL.NO", font1)));

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(new Phrase(new Phrase("   SUBJECTS", font1)));

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(new Phrase(new Phrase("   MARKS", font1)));

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(new Phrase(new Phrase("   MAX MARK", font1)));

table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.AddCell(new Phrase(new Phrase("   CLASS AVG", font1)));

Doc.Add(table);
Run Code Online (Sandbox Code Playgroud)

例如:

在此输入图像描述

有人请帮忙

Bru*_*gie 6

您可以更改单元格的边框,使它们只显示垂直线条.如何执行此操作取决于您将单元格添加到表中的方式.

这是两种方法:

1.您显式创建PdfPCell对象:

PdfPCell cell = new PdfPCell(); cell.AddElement(new Paragraph("my content")); cell.Border = PdfPCell.LEFT; table.AddCell(细胞);

在这种情况下,仅显示单元格的边框.对于行中的最后一个单元格,您还应该添加右边框:

cell.Border = PdfPCell.LEFT | PdfPCell.RIGHT;

2.隐式创建PdfPCell对象:

在这种情况下,您不PdfPCell自己创建对象,您可以让iTextSharp创建单元格.这些单元格将复制DefaultCell在该级别定义的属性PdfPTable,因此您需要更改此属性:

table.DefaultCell.Border = Rectangle.LEFT | Rectangle.RIGHT;
table.addCell("cell 1");
table.addCell("cell 2");
table.addCell("cell 3");
Run Code Online (Sandbox Code Playgroud)

现在所有单元格都没有顶部或底部边框,只有左边框和右边框.你会画出一些额外的线条,但没有人注意到线条重合.

另请参见在iTextSharp中隐藏表格边框

例如:

PdfPTable table = new PdfPTable(5);
table.TotalWidth = 510f;//table size
table.LockedWidth = true;
table.SpacingBefore = 10f;//both are used to mention the space from heading
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.DefaultCell.Border = PdfPCell.LEFT | PdfPCell.RIGHT;
table.AddCell(new Phrase("    SL.NO", font1));
table.AddCell(new Phrase("   SUBJECTS", font1));
table.AddCell(new Phrase("   MARKS", font1));
table.AddCell(new Phrase("   MAX MARK", font1));
table.AddCell(new Phrase("   CLASS AVG", font1));
Doc.Add(table);
Run Code Online (Sandbox Code Playgroud)

没有必要定义DefaultCell这么多次的属性.没有必要Phrase像这样嵌套构造函数:new Phrase(new Phrase("content")).