Tables are used to represent data in the form of rows and columns. A table lets the readers go through the data quickly without reading a lot of text. When generating the PDF files programmatically, you may often come across the requirement of creating tables. In accordance with that, this article covers how to create tables in PDF files using C# .NET.
- .NET API to Create Tables in PDF
- Create a Table in a PDF File
- Set Borders and Margins in PDF Tables
- Apply Column Adjustment to PDF Tables
C# .NET API to Create Tables in PDF Files
We will use Aspose.PDF for .NET to create tables in PDF files. The API is designed to perform PDF generation and manipulation within the .NET applications. It allows you to create PDF files of simple and complex layouts seamlessly. You can either download the API’s binaries or install it using NuGet.
PM> Install-Package Aspose.PDF
Create a Table in a PDF File in C#
You can create tables in either a new or an existing PDF file using Aspose.PDF for .NET. The following are the steps to create a table in a PDF file in C#.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Set table’s settings (i.e. borders).
- Populate the table by creating rows using Table.Rows.Add() method.
- Add the table to the page using Document.Pages[index].Paragraphs.Add(Table) method.
- Save the PDF file using Document.Save(string) method.
The following code sample shows how to create a table in a PDF file using C#.
The following is the output of the above code sample.
Create PDF Table with Customized Borders and Margins
You can also customize the borders and margins of the tables in PDF as per your requirements. For example, you can set the border width, border style, and top, bottom, left, and right margins. The following are the steps to set borders and margins for the tables in PDF files in C#.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Create an instance of BorderInfo class to apply border styling.
- Create an instance of MarginInfo class to set margins for the table.
- Populate the table by creating rows using Table.Rows.Add() method.
- Add the table to the page using Document.Pages[index].Paragraphs.Add(Table) method.
- Save the PDF file using Document.Save(string) method.
The following code sample shows how to set borders and margins for tables in PDF using C#.
The following screenshot shows the output of the above code sample.
Apply Column Adjustment to PDF Tables in C#
Aspose.PDF for .NET also allows you to customize the column adjustment of the table in a PDF. For example, you can auto-fit the table to the window or the content. The following are the steps to set the column adjustment of a table in PDF using C#.
- Load the PDF file (or create a new one) using Document class.
- Initialize a table and set its columns and rows using Table class.
- Set borders and margins.
- Set Table.ColumnAdjustment property to the desired value of ColumnAdjustment enum.
- Populate the table by creating rows using Table.Rows.Add() method.
- Add the table to the page using Document.Pages[index].Paragraphs.Add(Table) method.
- Save the PDF file using Document.Save(string) method.
The following code sample shows how to set the column adjustment of the table in PDF using C#.
Get a Free License
You can get a free temporary license in order to use Aspose.PDF for .NET without evaluation limitations.
Conclusion
In this article, you have learned how to create tables in PDF files using C#. Furthermore, you have seen how to customize the borders. margins, and column adjustment of a table in PDF. Besides, you can explore more about the C# PDF API using the documentation. In case you would have any questions or queries, you can contact us via our forum.