Export data to PDF using Aspose.PDF for .NET Core 2.0
Tags: csharp, pdf
Published: 2018-02-19 16:25:36

---
title: Export data to PDF using Aspose.PDF for .NET Core 2.0
published: true
description: Export data to PDF documents using Aspose.PDF for .NET Core 2.0
tags: #csharp, #pdf
---

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](https://apireference.aspose.com/net/pdf/aspose.pdf/document). 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;
1. Add the page (the Page object) for the Document object;
1. Create objects that are placed on the page (e.g. text fragment, table, etc.)
1. Add created items to the corresponding collection on the page (in our case it will be a paragraph collection);
1. Save the document as PDF file.

{% gist https://gist.github.com/andruhovski/10df703353b15e994561d9e7e1bbcea4 %}

The most common problem is the output of data in a table format. The [Table class](https://apireference.aspose.com/net/pdf/aspose.pdf/table) is used to process tables. This class gives us the ability to create tables and place them in the document, using [Rows](https://apireference.aspose.com/net/pdf/aspose.pdf/rows) and [Cells](https://apireference.aspose.com/net/pdf/aspose.pdf/cell). 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.


{% gist https://gist.github.com/andruhovski/b03d232683eb816f04d67061abe7ab8e %}

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

* [ColumnWidths](https://apireference.aspose.com/net/pdf/aspose.pdf/table/properties/columnwidths) - width of columns (by default);
* [DefaultCellPadding](https://apireference.aspose.com/net/pdf/aspose.pdf/table/properties/defaultcellpadding) - the default fields for the table cell;
* [Border](https://apireference.aspose.com/net/pdf/aspose.pdf/table/properties/border) - table frame attributes (style, thickness, color);
* [DefaultCellBorder](https://apireference.aspose.com/net/pdf/aspose.pdf/table/properties/defaultcellborder) - attributes of the cell frame (style, thickness, color).

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

![Table 4x10](http://aspose-html-doc.azurewebsites.net/docs/images/img001.jpg)

## Exporting Data from ADO.NET Objects

The Table class provides methods for interacting with ADO.NET data sources - [ImportDataTable](https://apireference.aspose.com/net/pdf/aspose.pdf.table/importdatatable/methods/1) and [ImportDataView](https://apireference.aspose.com/net/pdf/aspose.pdf/table/methods/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.

{% gist https://gist.github.com/andruhovski/2f7723bc55fe5d4751010f43bc63798d %}

## 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.

{% gist https://gist.github.com/andruhovski/36a57aa9a8738c96e557589048c22f09 %}

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.

{% gist https://gist.github.com/andruhovski/57dca66df8419ae87b5fb47b68f8711f %}

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).

{% gist https://gist.github.com/andruhovski/c6520e80f737db0ca7aae151462907c9 %}

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.