How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Generator Library
C#: How to add a table to PDF file using C#


C# sample code to add table content to PDF file in ASP.NET MVC, Windows applications.





In this C# tutorial, you will learn how to create a new PDF report file, and insert table contents to PDF using C# in ASP.NET MVC Web, Windows applications.

  • Create a table in PDF page
  • Apply table column settings
  • Align the table in the page
  • Insert table cell data

How to insert table into PDF report file using C#

  1. Download XDoc.PDF Report Generator C# library
  2. Install C# library to create tables in PDF document
  3. Step by Step Tutorial














Create an empty table in PDF using C#


Below are the steps and C# sample source code to create a new PDF file and print a text message on it using C#.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Open the document object, and start inserting contents
  3. Create a new PdfPTable object with 4 columns
  4. Insert 3 rows of table data
  5. Add the table to the PDF page
  6. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample41.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  initial a table with 4 columns
PdfPTable table = new PdfPTable(4);
//  3 rows
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table.AddCell(cell);
    //  finish remained columns of the current row
    table.CompleteRow();
}

//  add table to the document
document.Add(table);

//  close document and save to the output file
document.Close();




Create a table with relative column width


The following steps and C# sample code will instruct how to set the newly created table width on the PDF page using C#.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Open the document object, and start inserting contents
  3. Create a new PdfPTable object with 4 columns with width ratios set
  4. Lock the table columns' width
  5. Set the whole table width to 360 pixels (5 inches)
  6. Insert 3 rows of table data
  7. Add the table to the PDF page
  8. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample42.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  the width ratios of table columns -- 1 : 2 : 1 : 1
float[] columnRelativeWidths = new float[4] { 1, 2, 1, 1 };
PdfPTable table = new PdfPTable(columnRelativeWidths);
//  set lock column width flag to true
table.LockedWidth = true;
//  set total table width to 360 pt. (5 inches), by using above relative widths, the actural column widths are:
//  72 pt., 144 pt., 72 pt., 72 pt. in turn.
table.TotalWidth = 360;

//  3 rows
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table.AddCell(cell);
    //  finish remained columns of the current row
    table.CompleteRow();
}

//  add table to the document
document.Add(table);

//  close document and save to the output file
document.Close();




Create a table with absolute column width


String outputFilePath = Program.RootPath + "\\" + "Sample43.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  4 columuns with widths; 72 pt., 144 pt., 72 pt., 72 pt.
float[] columnWidthInPoints = new float[4] { 72F, 144F, 72F, 72F };
PdfPTable table = new PdfPTable(columnWidthInPoints.Length);
//  set lock column width flag to true
table.LockedWidth = true;
table.SetTotalWidth(columnWidthInPoints);

//  3 rows
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table.AddCell(cell);
    //  finish remained columns of the current row
    table.CompleteRow();
}

//  add table to the document
document.Add(table);

//  close document and save to the output file
document.Close();




Set table alignment in PDF page using C#


The following steps and C# sample code will show how to set the newly created table content alignment on the PDF page using C#.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Open the document object, and start inserting contents
  3. Create a new PdfPTable object with 4 columns
  4. Set table property Alignment to Alignment.ALIGN_LEFT



String outputFilePath = Program.RootPath + "\\" + "Sample44.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

PdfPTable table1 = new PdfPTable(4);
//  set table to left alignment
table1.Alignment = Alignment.ALIGN_LEFT;
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table1.AddCell(cell);
    //  finish remained columns of the current row
    table1.CompleteRow();
}
document.Add(table1);

PdfPTable table2 = new PdfPTable(4);
//  set table to right alignment
table2.Alignment = Alignment.ALIGN_RIGHT;
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table2.AddCell(cell);
    //  finish remained columns of the current row
    table2.CompleteRow();
}
document.Add(table2);

PdfPTable table3 = new PdfPTable(4);
//  set table to center alignment
table3.Alignment = Alignment.ALIGN_CENTER;
for (int i = 0; i < 3; i++)
{   //  set cell height to 20 pt. for each row
    PdfPCell cell = new PdfPCell();
    cell.Height = 20F;
    table3.AddCell(cell);
    //  finish remained columns of the current row
    table3.CompleteRow();
}
document.Add(table3);

//  close document and save to the output file
document.Close();




Add table cell to a table


String outputFilePath = Program.RootPath + "\\" + "Sample45.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  initial a table with 4 columns
PdfPTable table = new PdfPTable(4);

//  add cell to the 1st row
table.AddCell("Row 1, Cell 1");     //  1st cell
table.AddCell("Row 1, Cell 2");     //  2nd cell
table.AddCell("Row 1, Cell 3");     //  3rd cell
table.AddCell("Row 1, Cell 4");     //  4th cell
//  finish remained columns of the current row, do nothing if there is no remained cell in the row.
table.CompleteRow();

//  add cell to the 2nd row
table.AddCell("");                  //  1st cell is empty
table.AddCell("Row 2, Cell 2");     //  2nd cell
table.AddCell("");                  //  3rd cell is empty
//  finish remained columns of the current row
table.CompleteRow();

//  add cell to the 2nd row
table.AddCell("");                  //  1st cell is empty
table.AddCell("");                  //  2nd cell is empty
table.AddCell("Row 3, Cell 3");     //  3rd cell
//  finish remained columns of the current row
table.CompleteRow();

//  add table to the document
document.Add(table);

//  close document and save to the output file
document.Close();




Create a simple calendar


String outputFilePath = Program.RootPath + "\\" + "Sample46.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

float[] columnWidths = new float[7] { 72, 72, 72, 72, 72, 72, 72 };
PdfPTable table = new PdfPTable(columnWidths.Length);
table.LockedWidth = true;
table.SetTotalWidth(columnWidths);

Font titleFont = new Font(Font.FontFamily.Helvetica, 12F, Font.FontStyle.Bold, new Color(0, 0, 255));
String[] colNames = new String[7] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
foreach (String name in colNames)
    table.AddCell(createCell(name, titleFont, 16F));
//table.CompleteRow();

Font weekdayFont = new Font(Font.FontFamily.Helvetica, 36F, Font.FontStyle.Regular, new Color(0, 0, 0));
Font weekendFont = new Font(Font.FontFamily.Helvetica, 36F, Font.FontStyle.Bold, new Color(255, 0, 0));
int date = 1;
while (date < 31)
{
    for (int col = 0; col < 7; col++)
    {
        Font dateFont = (col == 0 || col == 6) ? weekendFont : weekdayFont;
        table.AddCell(createCell(date.ToString(), dateFont, 40F));

        if (++date > 31) break;
    }
    table.CompleteRow();
}

//  add content to the document
document.Add(table);

//  close document and save to the output file
document.Close();