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 then it is possible too. And it's not only possible but its very easy too.
Aspose.Pdf allows developers to import data from:
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 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 .
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
[C#]
/* 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
Pdf pdf1 = new Pdf();
//Create a section in the Pdf instance
Section sec1 = pdf1.Sections.Add();
//Create a Table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.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 BorderInfo((int)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
Row row1 = tab1.Rows[0];
//Iterate through all cells in the row and set their background color to blue
foreach(Cell curCell in row1.Cells)
curCell.BackgroundColor = new Aspose.Pdf.Color("Blue");
//Save the Pdf
pdf1.Save(...);
[VB.NET]
'**************************************************************************
'* 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 Pdf = New Pdf()
'Create a section in the Pdf instance
Dim sec1 As Section = pdf1.Sections.Add()
'Create a Table object
Dim tab1 As Aspose.Pdf.Table = New Aspose.Pdf.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 BorderInfo(CType(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 Row = tab1.Rows(0)
'Declare a Cell object
Dim curCell As 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.Color("Blue")
Next
'Save the Pdf
pdf1.Save(...)