| Databases are specially built to store and manage data in a better manner. It's a very common practice for programmers to populate different kinds of objects with data from databases. |
If you want to populate Table object with data from any data source using Aspose.Pdf for .NET then it is possible too. And it's not only possible but its very easy too.
Aspose.Pdf for .NET allows developers to import data from:
- Object Array
- DataTable
- DataView
In our previous topics, we have discussed the use of ImportArray method of Table class that allows to fetch data from an array of string or objects. This topic would provide information about fetching data from DataTable or DataView.
All developers working under .NET platform must be familiar with basic ADO.NET concepts introduced by .NET Framework. We know that it is possible to connect to almost all kinds of data sources using ADO.NET. We can retrieve data from databases and can save them in DataSet, DataTable or DataView. So, Aspose.Pdf for .NET has provided the support for importing data from DataTable or DataView. It would provide more freedom to developers to populate their tables in PDF documents from any data source using Aspose.Pdf for .NET.
The ImportDataTable and ImportDataView methods of Table class are used to import data from databases.
In the example below, we have demonstrated the use of ImportDataTable. In this example, DataTable object is created from scratch and records are added programmatically instead of filling the DataTable with data from databases. Developers can populate DataTable from database too according to their desire.
Code Snippet
/* Create a DataTable object (Employee) and add columns to it (Employee_ID, * Employee_Name, Gender). */ DataTable dt = new DataTable("Employee"); dt.Columns.Add("Employee_ID",typeof(Int32)); dt.Columns.Add("Employee_Name",typeof(string)); dt.Columns.Add("Gender",typeof(string)); //Add 2 rows into the DataTable object programmatically DataRow dr = dt.NewRow(); dr[0] = 1; dr[1] = "John Smith"; dr[2] = "Male"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = 2; dr[1] = "Mary Miller"; dr[2] = "Female"; dt.Rows.Add(dr); //Instantiate a Pdf instance Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(); //Create a section in the Pdf instance Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add(); //Create a Table object Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table(); //Add the Table object in the paragraphs collection of the section sec1.Paragraphs.Add(tab1); //Set column widths of the table tab1.ColumnWidths = "40 100 100 100"; //Set default cell border of the table using BorderInfo object tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int) Aspose.Pdf.Generator.BorderSide.All,0.1F); //Import data into the Table object from the DataTable created above tab1.ImportDataTable(dt,true,0,1,3,3); //Get 1st row from the table Aspose.Pdf.Generator.Row row1 = tab1.Rows[0]; //Iterate through all cells in the row and set their background color to blue foreach(Aspose.Pdf.Generator.Cell curCell in row1.Cells) curCell.BackgroundColor = new Aspose.Pdf.Generator.Color("Blue"); //Save the Pdf pdf1.Save(...);
'************************************************************************** '* Create a DataTable object (Employee) and add columns to it (Employee_ID, '* Employee_Name, Gender). '************************************************************************** Dim dt As DataTable = New DataTable("Employee") dt.Columns.Add("Employee_ID", System.Type.GetType("System.Int32")) dt.Columns.Add("Employee_Name", System.Type.GetType("System.String")) dt.Columns.Add("Gender", System.Type.GetType("System.String")) 'Add 2 rows into the DataTable object programmatically Dim dr As DataRow = dt.NewRow() dr(0) = 1 dr(1) = "John Smith" dr(2) = "Male" dt.Rows.Add(dr) dr = dt.NewRow() dr(0) = 2 dr(1) = "Mary Miller" dr(2) = "Female" dt.Rows.Add(dr) 'Instantiate a Pdf instance Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf() 'Create a section in the Pdf instance Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add() 'Create a Table object Dim tab1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table() 'Add the Table object in the paragraphs collection of the section sec1.Paragraphs.Add(tab1) 'Set column widths of the table tab1.ColumnWidths = "40 100 100 100" 'Set default cell border of the table using BorderInfo object tab1.DefaultCellBorder = New Aspose.Pdf.Generator.BorderInfo(CType(Aspose.Pdf.Generator.BorderSide.All, Integer), 0.1F) 'Import data into the Table object from the DataTable created above tab1.ImportDataTable(dt, True, 0, 1, 3, 3) 'Get 1st row from the table Dim row1 As Aspose.Pdf.Generator.Row = tab1.Rows(0) 'Declare a Cell object Dim curCell As Aspose.Pdf.Generator.Cell 'Iterate through all cells in the row and set their background color to blue For Each curCell In row1.Cells curCell.BackgroundColor = New Aspose.Pdf.Generator.Color("Blue") Next 'Save the Pdf pdf1.Save(...)
