在 iText 7 中创建无边框 table

Create borderless table in iText 7

我正在将应用程序从 iTextSharp 5 移植到 iText 7。在原始应用程序中,我使用在页面顶部添加 table 的压模添加了页码。我几乎成功了,但我无法创建只有底部边框的 table(内部、左侧、右侧和顶部无边框。我尝试了 iText building blocks 书中的示例并查看了

但我仍然得到顶部、内部和侧面边框。这是我的代码:

private Table makepagetable(string doctitle, int curpage, int totalpage)
{
  PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);

  Text title = new Text(doctitle).SetFontSize(11f).SetFont(font);
  Text pages = new Text("page " + curpage.ToString() + " of " + totalpage.ToString()).SetFont(font);
  Paragraph p1 = new Paragraph(title).SetTextAlignment(TextAlignment.LEFT);
  Paragraph p2 = new Paragraph(pages).SetTextAlignment(TextAlignment.RIGHT);
  Table mytable = new Table(new float[] { 3, 1 })
      .SetBorderTop(Border.NO_BORDER)
      .SetBorderRight(Border.NO_BORDER)
      .SetBorderLeft(Border.NO_BORDER)
      .SetBorderBottom(new SolidBorder(2));
  float pval = 500;
  mytable.SetWidth(pval);
  mytable.AddCell(new Cell().Add(p1)).SetBorder(Border.NO_BORDER);
  mytable.AddCell(new Cell().Add(p2)).SetBorder(Border.NO_BORDER);
    // SetBorderTop(Border.NO_BORDER).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER);
  return mytable;
}

我也在 table 对象上尝试了 mytable.AddCell(new Cell().Add(p2)).SetBorderTop(Border.NO_BORDER).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER); 和各种类似的组合,但我仍然得到了边框。

为了完成这里是调用这个函数的例程

public Byte[] AddPageNumbers(Byte[] firstpass, string doctitle) {
  //firstpass = array of the original pdfdocument    
  // doctitle = text on left of table
  int i;
  float x = 30;            
  float y = 810;
  MemoryStream ms2 = new MemoryStream(firstpass);
  PdfReader reader = new PdfReader(ms2);
  MemoryStream ms3 = new MemoryStream();
  PdfDocument pdfDoc =  new PdfDocument(reader, new PdfWriter(ms3));
  Document document = new Document(pdfDoc, PageSize.A4, false);
  
  int n = pdfDoc.GetNumberOfPages();
  for (i = 3; i <= n; i++)
  {
    Paragraph header;
    //PdfPage page = pdfDoc.GetPage(i);
    //header = new Paragraph(String.Format(doctitle + " Page {0} of {1}", i, n));
    Table table;
    PdfPage page = pdfDoc.GetPage(i);
    table = makepagetable(doctitle, i, n);
    header = new Paragraph().Add(table);

    document.ShowTextAligned(header,x,y, i, TextAlignment.JUSTIFIED, VerticalAlignment.MIDDLE, 0);
  }
  document.Close();
  pdfDoc.Close();

  return ms3.ToArray();
}

Borderless Table 或 Cell,对于 c# 和 iText7,您可以使用:

//Table with no border
table.SetBorder(Border.NO_BORDER);

//Cell with no border
cell.SetBorder(Border.NO_BORDER);

我检查了你的代码:

//your source code
mytable.AddCell(new Cell().Add(p1)).SetBorder(Border.NO_BORDER);
//changed code
mytable.AddCell(new Cell().Add(p1).SetBorder(Border.NO_BORDER));

希望对您有所帮助。