Search code examples
c#itext7

Create borderless table in iText 7


I am porting an application from iTextSharp 5 to iText 7. In the original application I added page numbers using a stamper that added a table to the top of the page. I have almost succeeded but I am unable to create a table with only a bottom border (internal, left, right and top is borderless. I tried the example from the iText building blocks book and have looked at itext 7 table borderless

But I still get top, internal and side borders. Here is my code:

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

I have also tried mytable.AddCell(new Cell().Add(p2)).SetBorderTop(Border.NO_BORDER).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER); and various similar combinations on the table object, but I still get the borders.

For completion here is the routine that calls this function

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

Solution

  • Borderless Table or Cell, for c# and iText7, you can use:

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

    I review your code:

    //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));
    

    Hope this is helpful.