Oksana Pochapska
asposepdf
Published in
6 min readAug 24, 2023

--

Generate Tables in PDF Files using C#

Tables provide a structured way to display data in rows and columns, enhancing data comprehension. When programmatically generating PDF files, the task of creating tables is common. This article presents an extensive tutorial on seamlessly creating tables within PDF documents using C# (.NET), enabling efficient integration of tabular data representation.

In this process, we will make use of Aspose.PDF for .NET, an API crafted specifically for PDF generation and manipulation within .NET applications. This versatile tool allows for the effortless creation of PDF layouts, both basic and intricate. You have the choice to either directly download the API’s binaries or conveniently install them via the NuGet package manager.

PM> Install-Package Aspose.PDF

With Aspose.PDF for .NET, you gain the ability to seamlessly generate tables within both newly created and existing PDF files. Below is a comprehensive guide detailing the steps required to create a table in a PDF using C#:

  • Load or Create PDF: Utilize the Document class to either load an existing PDF file or initiate the creation of a new one.
  • Table Initialization: Employ the Table class to initialize a table, allowing you to define the desired number of columns and rows.
  • Customize Table Settings: Adjust the table’s appearance and characteristics, such as specifying borders, according to your requirements.
  • Populate with Rows: Populate the table by adding rows through the Table.Rows.Add() method. This step is essential for filling the table with data.
  • Incorporate into Page: Integrate the generated table into a specific page of the PDF by utilizing the Document.Pages[index].Paragraphs.Add(Table) method.
  • Save the Modified PDF: After creating and customizing the table, save the modified PDF file using the Document.Save(string) method, ensuring your changes are preserved.

By following these steps, you can efficiently integrate dynamic tables into your PDF files using C# with the assistance of Aspose.PDF for .NET.

// Create PDF document (to load the existing file, initialize Document object with file's path)
Document document = new Document();
// Add page
Aspose.Pdf.Page page = document.Pages.Add();
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// Create a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++)
{
// Add row to table
Aspose.Pdf.Row row = table.Rows.Add();
// Add table cells
row.Cells.Add("Column (" + row_count + ", 1)");
row.Cells.Add("Column (" + row_count + ", 2)");
row.Cells.Add("Column (" + row_count + ", 3)");
}
// Add table to the page
page.Paragraphs.Add(table);
// Save the PDF document
document.Save("Generated-PDF.pdf");

The output presented below is a result of the provided code example.

In the realm of PDF table creation, customization is a key factor in delivering the desired visual impact. Aspose.PDF for .NET empowers developers with the ability to tailor table borders and margins according to specific requirements. Whether it’s adjusting border width, choosing a border style, or fine-tuning margins, this functionality provides a comprehensive way to optimize table layouts.

Here’s a step-by-step guide to achieve customized borders and margins for PDF tables using C#:

  • Load or create a PDF file using the Document class.
  • Initialize a table, setting columns and rows through the Table class.
  • Create a BorderInfo instance to define border styling parameters.
  • Establish MarginInfo settings to specify top, bottom, left, and right margins.
  • Populate the table by adding rows to the Table.Rows.Add() method.
  • Integrate the table into the page via the Document.Pages[index].Paragraphs.Add(Table) method.
  • Save the modified PDF file using the Document.Save(string) method.

For practical implementation, refer to the following code sample showcasing how to effectively apply customized borders and margins to tables within PDF documents using C#.

// Create PDF document (to load the existing file, initialize Document object with file's path)
Document document = new Document();

// Add page
Aspose.Pdf.Page page = document.Pages.Add();

// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();

// Set with column widths of the table
table.ColumnWidths = "50 50 50";

// Set default cell border using BorderInfo object
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

// Set table border using another customized BorderInfo object
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

// Create MarginInfo object and set its left, bottom, right and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;

// Set the default cell padding to the MarginInfo object
table.DefaultCellPadding = margin;

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = table.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add();
TextFragment mytext = new TextFragment("col3 with large text string");

// Row1.Cells.Add("col3 with large text string to be placed inside cell");
row1.Cells[2].Paragraphs.Add(mytext);
row1.Cells[2].IsWordWrapped = false;

// Row1.Cells[2].Paragraphs[0].FixedWidth= 80;
Aspose.Pdf.Row row2 = table.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");

// Add table to the page
page.Paragraphs.Add(table);

// Save the PDF document
document.Save("Generated-PDF.pdf");

Visual Output of the Provided Code Sample

Aspose.PDF for .NET provides the capability to customize the column adjustment settings for tables in PDF documents. This feature allows you to fine-tune how table columns behave, such as automatically fitting them to the window or content. Here’s a step-by-step guide on how to set the column adjustment for a table in a PDF using C#:

  • Load an existing PDF document or create a new one using the Document class.
  • Create a table structure by instantiating the Table class and defining the desired number of columns and rows.
  • Specify table borders, margins, and other formatting preferences as needed.
  • Set the Table.ColumnAdjustment property to your preferred value from the ColumnAdjustment enum, determining how columns adjust.
  • Populate the table with content by adding rows using the Table.Rows.Add() method.
  • Embed the table within a page using the Document.Pages[index].Paragraphs.Add(Table) method.
  • Save the modified PDF document using the Document.Save(string) method.

The provided code snippet serves as an example of implementing column adjustment settings for a table in a PDF using C#.

// Create PDF document (to load the existing file, initialize Document object with file's path)
Document document = new Document();

// Add page
Aspose.Pdf.Page page = document.Pages.Add();

// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();

// Set with column widths of the table
table.ColumnWidths = "50 50 50";

// Set column adjustment
table.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;

// Set default cell border using BorderInfo object
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

// Set table border using another customized BorderInfo object
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

// Create MarginInfo object and set its left, bottom, right and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;

// Set the default cell padding to the MarginInfo object
table.DefaultCellPadding = margin;

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = table.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add();
TextFragment mytext = new TextFragment("col3 with large text string");

// Row1.Cells.Add("col3 with large text string to be placed inside cell");
row1.Cells[2].Paragraphs.Add(mytext);
row1.Cells[2].IsWordWrapped = false;

// Row1.Cells[2].Paragraphs[0].FixedWidth= 80;
Aspose.Pdf.Row row2 = table.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");

// Add table to the page
page.Paragraphs.Add(table);

// Save the PDF document
document.Save("Generated-PDF.pdf");

In this article, you’ve gained insight into creating tables within PDF files using C#. Additionally, you’ve discovered how to personalize table attributes such as borders, margins, and column adjustment.

--

--