【问题标题】:Hiding table border in iTextSharp在 iTextSharp 中隐藏表格边框
【发布时间】:2013-08-01 13:31:32
【问题描述】:

如何使用 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());

我是否需要为单个单元格指定无边框,或者我可以为表格本身指定无边框。

谢谢

【问题讨论】:

  • 也许是时候接受所提出的答案之一了。

标签: c# .net itextsharp


【解决方案1】:

这应该可以解决问题:

table.DefaultCell.Border = Rectangle.NO_BORDER; 

table.borderwidth= 0;

【讨论】:

  • 谢谢您的回复。我已经尝试了这两个选项,但它们都不起作用。我认为 Bruno Lowagie 给出的理由是有道理的
  • 表格对象没有borderwidth属性
【解决方案2】:

虽然我赞成 Martijn 的回答,但我想澄清一下。

iText 中只有单元格有边框;表格没有边框。 Martijn 建议将默认单元格的边框设置为NO_BORDER 是正确的:

table.DefaultCell.Border = Rectangle.NO_BORDER;

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

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

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

【讨论】:

  • 感谢您的额外解释!
  • 谢谢@Bruno,我昨天只是想弄清楚为什么这没有按预期工作!我今天要破解源代码。
  • 你能为我们提供这个工厂类的sn-p吗?
  • @Dr.MAF 多么奇怪的问题。每个人都以自己的方式编写这样的工厂类。如果你在官方网站上搜索CreateCell(),你会发现几个例子:itextpdf.com/search/node/createCell(这与 iText 无关;这是学习如何编写代码的人使用的标准做法)。
【解决方案3】:

首先,我们可以将所有单元格边框设置为 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);

在这里我们可以将表格添加到一个单元格并可以设置边框并再次将该单元格添加到另一个表格中,我们可以根据我们的要求使用。

【讨论】:

    【解决方案4】:
    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);
    

    检查this link

    【讨论】:

    • 请在您的代码中添加详细信息并提供您提供的链接的摘要。
    【解决方案5】:

    试试这个代码

     yourtable.DefaultCell.Border = 0;
    

    【讨论】:

      【解决方案6】:

      如果您的 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);
      

      我花了很长时间才弄明白。它现在对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2012-05-06
        • 2023-03-26
        • 2015-05-28
        • 2017-04-11
        • 2015-02-21
        • 1970-01-01
        相关资源
        最近更新 更多