【问题标题】:Create borderless table in iText 7在 iText 7 中创建无边框表格
【发布时间】:2020-09-23 08:18:20
【问题描述】:

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

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

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;
}

我也尝试过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();
}

【问题讨论】:

    标签: c# itext7


    【解决方案1】:

    无边框表格或单元格,对于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));
    

    希望这有帮助。

    【讨论】:

      猜你喜欢
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多