DEV Community

loading...
Aspose.PDF

Export data to PDF using Aspose.PDF for .NET Core 2.0

andruhovski profile image Andriy Updated on ・4 min read

There are a number of tasks when for some reason it is more convenient to export data from databases to a PDF document without using the recently popular HTML to PDF conversion scheme. This article will show you how to generate a PDF document using the Aspose.PDF for .NET Core 2.0 library.

Using this library has some advantages:

  • It's a single cross-platform API for .NET/.NET Standard 2.0, Java, and Android platforms with native core, so you don't need additional dependencies;

  • Large set of data converters from/to PDF;

  • Wide range of functions for editing PDF and additional functions like signing, encryption, text extraction etc;

  • To demonstrate the solution, the standard template "ASP.NET Core Web Application / Web Application (Model-View-Controller)" was used.

Basics of generation PDF with Aspose.PDF

One of the most important classes in Aspose.PDF is a Document class. This class is a PDF rendering engine. To present a PDF structure, the Aspose.PDF library uses the Document-Page model, where:

  • Document - contains the properties of the PDF document including page collection;
  • Page - contains the properties of a specific page and various collections of elements associated with this page.

Therefore, to create a PDF document with Aspose.PDF, you should follow these steps:

  1. Create the Document object;
  2. Add the page (the Page object) for the Document object;
  3. Create objects that are placed on the page (e.g. text fragment, table, etc.)
  4. Add created items to the corresponding collection on the page (in our case it will be a paragraph collection);
  5. Save the document as PDF file.

The most common problem is the output of data in a table format. The Table class is used to process tables. This class gives us the ability to create tables and place them in the document, using Rows and Cells. So, to create the table, you need to add the required number of rows and fill them with the appropriate number of cells.

The following example creates the table 4x10.

When initializing the Table object, the minimal skin settings were used:

As a result, we get the table 4x10 with equal-width columns.

Table 4x10

Exporting Data from ADO.NET Objects

The Table class provides methods for interacting with ADO.NET data sources - ImportDataTable and ImportDataView. The first method imports data from the DataTable, the second from the DataView.
Premising that these objects are not very convenient for working in the MVC template, we will limit ourselves to a brief example. In this example (line 50), the ImportDataTable method is called and receives as parameters a DataTable instance and additional settings like the header flag and the initial position (rows/cols) for the data output.

Exporting Data from the Entity Framework

More relevant for .NET Core 2.0 is the import of data from ORM frameworks. In this case, it's a good idea to extend the Table class with extension methods for importing data from a simple list or from the grouped data. Let's give an example for one of the most popular ORMs - Entity Framework.

The Data Annotations attributes are often used to describe models and help us to create the table. Therefore, the following table generation algorithm was chosen for ImportEntityList:

  • lines 12-18: build a header row and add header cells according to the rule "If the DisplayAttribute is present, then take its value otherwise take the property name"
  • lines 50-53: build the data rows and add row cells according to the rule "If the attribute DataTypeAttribute is defined, then we check whether we need to make additional design settings for it, and otherwise just convert data to string and add to the cell;"

In this example, additional customizations were made for DataType.Currency (lines 32-34) and DataType.Date (lines 35-43), but you can add others if necessary.
The algorithm of the ImportGroupedData method is almost the same as the previous one. An additional GroupViewModel class is used, to store the grouped data.

Since we process groups, first we generate a line for the key value (lines 66-71), and after it - the lines of this group.

Saving result

The Document class has a Save method that allows you to save the result. In our case, the result is a file that will be downloaded by the client application (browser).

To sum up: this article shows you how to import data into tables in a new document. Adding a table to an existing document is also possible, but it is somewhat more complex and will be treated separately.

Discussion

pic
Editor guide