
Databases are almost everywhere to store and manage the data. It is a common practice of the programmers to retrieve the data from the databases and load it into the applications. When generating PDF files programmatically, one may need to populate the document with the data in database. To accomplish that in .NET applications, this article shows how to add data from database to PDF files in C#.
- .NET API to Add Data from Database to PDF
- Add Data from Database to PDF
- Add Data from Database to PDF in Entity Framework
C# .NET API to Add Data from Database to PDF
We will use Aspose.PDF for .NET to add data from database to PDF files. It is a popular PDF generation and manipulation API that 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
Add Data from Database to PDF in C#
In most cases, the data is fetched from a database table into a DataTable or DataView. For this demo we will create and populate a DataTable programmatically, without a database. Follow these steps to add data to a PDF file from a database in C#.
- Load the data into a DataTable from database.
- Create a new PDF or load an existing one using the Document class.
- Create an instance of the Table class and set its properties (column width, borders, etc.).
- Import data from the database to the PDF table using the Table.ImportDataTable() method.
- Add the table to the page with **Document.Pages[index].Paragraphs.Add(Table)**.
- Save the PDF file using the Document.Save(string) method.
The code sample below demonstrates how to import data from a database to a PDF in C#.
The following is the output of the above code sample.

Add Data from Database to PDF in Entity Framework
Entity Framework (EF) is widely used by developers today. Extending the Table class makes it easy to populate PDF documents with lists or grouped data in EF. The implementation below shows how to fill a PDF table using a list and grouped data. In both methods, the Table and data are passed as arguments.
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 learned how to add data from a database into PDF files using C#. You saw how to import a DataTable into a PDF table and how to use Entity Framework for the same task. Explore more about the C# PDF API in the documentation. For questions or support, visit our forum.