gpt4 book ai didi

c# - 如何使用 itextsharp 在 pdf 中仅设置表格的垂直线?

转载 作者:太空狗 更新时间:2023-10-29 20:23:36 24 4
gpt4 key购买 nike

我有一张带有垂直线和水平线的表格。但我不想要水平线。我只想要垂直线。我该如何设置它。我预期的 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);

例如:

enter image description here

请大家帮忙

最佳答案

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

这是两种方法:

<强>1。您显式创建 PdfPCell 对象:

PdfPCell 单元格 = new PdfPCell();cell.AddElement(new Paragraph("我的内容"));cell.Border = PdfPCell.LEFT;table.AddCell(单元格);

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

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

<强>2。您隐式创建 PdfPCell 对象:

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

table.DefaultCell.Border = Rectangle.LEFT | Rectangle.RIGHT;
table.addCell("cell 1");
table.addCell("cell 2");
table.addCell("cell 3");

现在所有单元格都没有顶部或底部边框,只有左右边框。您会绘制一些额外的线条,但没有人会注意到线条重合。

另见 Hiding table border in 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);

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

关于c# - 如何使用 itextsharp 在 pdf 中仅设置表格的垂直线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com