在iTextSharp中隐藏表格边框

43

如何使用iTextSharp隐藏表格边框。我正在使用以下代码生成文件:

var document = new Document(PageSize.A4, 50, 50, 25, 25);

// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);

document.Open();
PdfPTable table = new PdfPTable(3);
var bodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
Font arial = FontFactory.GetFont("Arial", 6, BaseColor.BLUE);
cell = new PdfPCell(new Phrase("Font test is here ", arial));
cell.PaddingLeft = 5f;
cell.Colspan = 1;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("XYX"));
cell.Colspan = 2;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("Hello World"));
cell.PaddingLeft = 5f;
cell.Colspan = 1;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("XYX"));
cell.Colspan = 2;
table.AddCell(cell);



table.SpacingBefore = 5f;
document.Add(table);
document.Close();

Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Receipt-test.pdf");
Response.BinaryWrite(output.ToArray());

我需要为单元格指定无边框,还是可以为整个表格指定无边框。

谢谢


1
也许现在是时候接受其中一个给出的答案了。 - Bruno Lowagie
6个回答

77
尽管我点赞了 Martijn 的回答,但我想加一点澄清。
在 iText 中,只有单元格有边框;表格没有边框。Martijn 建议将默认单元格的边框设置为 NO_BORDER 是正确的:
table.DefaultCell.Border = Rectangle.NO_BORDER;

但这并不适用于问题中提供的代码片段。默认单元格的属性仅在iText需要隐式创建PdfPCell实例时使用(例如:如果您使用addCell()方法传递Phrase作为参数)。

在@Brown_Dynamite提供的代码片段中,PdfPCell对象是显式创建的。这意味着您需要将这些单元格的边框设置为NO_BORDER

通常,我编写一个工厂类来创建单元格。这样,我可以显著减少创建表格的类中的代码量。


感谢进一步的解释! - Martijn van Put
1
谢谢@Bruno,昨天我正试图弄清楚为什么这不像我预期的那样工作!我今天打算破解源代码。 - Chris Haas
你能为这个工厂类提供一小段代码吗? - Dr. MAF
@Dr.MAF 这是一个奇怪的问题。每个人都以自己的方式编写这样的工厂类。如果您在官方网站上搜索“CreateCell()”,您将会找到一些示例:http://itextpdf.com/search/node/createCell(这与iText无关;这是已经学会编写代码的人使用的标准实践)。 - Bruno Lowagie

11

这应该能起作用:

table.DefaultCell.Border = Rectangle.NO_BORDER; 
或者
table.borderwidth= 0;

1
谢谢回复。我已经尝试了这两个选项,但它们都没有起作用。我认为Bruno Lowagie提出的原因是有道理的。 - SharpCoder
表对象没有borderwidth属性。 - Matthew Lock

10

首先,我们可以将所有单元格边框设置为0。在将所有单元格分配给表之后,我们可以使用以下代码仅为pdfptable外边框设置边框。

        PdfPCell cell = new PdfPCell();
        cell.AddElement(t);
        cell.BorderWidthBottom=1f;
        cell.BorderWidthLeft=1f;
        cell.BorderWidthTop=1f;
        cell.BorderWidthRight = 1f;
        PdfPTable t1 = new PdfPTable(1);
        t1.AddCell(cell);

我们可以将表格添加到一个单元格中,并设置边框,然后将该单元格再添加到另一个表格中,根据需要使用。


5

如果您的PdfPTable嵌套在另一个PdfPTable中,则嵌套表将显示表边框。 摆脱表边框的唯一方法是将嵌套的PdfPTable放入主PdfPTable的PdfPCell中,并将该单元格的边框宽度设置为0。

iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(1); //<-- Main table
table.TotalWidth = 540f;
table.LockedWidth = true;
float[] widths = new float[] { 540f };
table.SetWidths(widths);
table.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.SpacingAfter = 10;

PdfPTable bodyTable = new PdfPTable(1); //<--Nested Table
bodyTable.TotalWidth = 540f;
bodyTable.LockedWidth = true;
float[] bodyWidths = new float[] { 540f };
bodyTable.SetWidths(bodyWidths);
bodyTable.HorizontalAlignment = 0;
bodyTable.SpacingAfter = 10;
bodyTable.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;

var para1 = new Paragraph("This is a long paragraph", blackNormal);
para1.SetLeading(3f, 1f);
PdfPCell bodyCell1 = new PdfPCell();
bodyCell1.AddElement(para1);
bodyCell1.Border = 0;
bodyTable.AddCell(bodyCell1);

iTextSharp.text.pdf.PdfPCell cellBody = new iTextSharp.text.pdf.PdfPCell(bodyTable);
cellBody.BorderWidth = 0; //<--- This is what sets the border for the nested table
table.AddCell(cellBody);

花了我很长时间才弄清楚这个。现在它对我有用。


3
PdfPTable table = new PdfPTable(4);
table.TotalWidth = 400f;
table.LockedWidth = true;
PdfPCell header = new PdfPCell(new Phrase("Header"));
header.Colspan = 4;
table.AddCell(header);
table.AddCell("Cell 1");
table.AddCell("Cell 2");
table.AddCell("Cell 3");
table.AddCell("Cell 4");
PdfPTable nested = new PdfPTable(1);
nested.AddCell("Nested Row 1");
nested.AddCell("Nested Row 2");
nested.AddCell("Nested Row 3");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
table.AddCell(nesthousing);
PdfPCell bottom = new PdfPCell(new Phrase("bottom"));
bottom.Colspan = 3;
table.AddCell(bottom);
doc.Add(table);

PdfPTable table = new PdfPTable(3);
table.TotalWidth = 144f;
table.LockedWidth = true;
table.HorizontalAlignment = 0;
PdfPCell left = new PdfPCell(new Paragraph("Rotated"));
left.Rotation = 90;
table.AddCell(left);
PdfPCell middle = new PdfPCell(new Paragraph("Rotated"));
middle.Rotation = -90;
table.AddCell(middle);
table.AddCell("Not Rotated");
doc.Add(table);

请查看此链接

请在您的代码中添加细节,并提供您提供的链接的摘要。 - merours

1
尝试这段代码。
 yourtable.DefaultCell.Border = 0;

此选项无法工作。 - Schroet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接