How to Start Convert PDF Read PDF Build PDF 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

Using C# PDF SDK
C# PDF Builder: How to add a table to PDF file


C# Demo Code to add a table to adobe pdf file













Create an empty table


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


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


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