How to Integrate Table with Database (DOM) & Populate Data in PDF using .NET
Join the DZone community and get the full member experience.
Join For FreeThis technical tip shows how .NET developers can integrate table with database (DOM) and populate data from a database using Aspose.Pdf for .NET. 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 it’s very easy too. Aspose.Pdf for .NET allows developers to import data from, Object Array, DataTable and DataView. 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.
//[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);
// Create Document instance
Document doc = new Document();
doc.Pages.Add();
// Initializes a new instance of the Table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
//Set column widths of the table
table.ColumnWidths = "40 100 100 100";
// Set the table border color as LightGray
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
// set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
table.ImportDataTable(dt, true, 0, 1, 3, 3);
// Add table object to first page of input document
doc.Pages[1].Paragraphs.Add(table);
// Save updated document containing table object
doc.Save("c:/pdftest/DataIntegrated.pdf");
//[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)
' Create Document instance
Dim doc As Document = New Document()
doc.Pages.Add()
' Initializes a new instance of the Table
Dim table As Aspose.Pdf.Table = New Aspose.Pdf.Table()
'Set column widths of the table
table.ColumnWidths = "40 100 100 100"
' Set the table border color as LightGray
table.Border = New Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray))
' set the border for table cells
table.DefaultCellBorder = New Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.5F, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray))
table.ImportDataTable(dt, True, 0, 1, 3, 3)
' Add table object to first page of input document
doc.Pages(1).Paragraphs.Add(table)
' Save updated document containing table object
doc.Save("c:/pdftest/DataIntegrated.pdf")
Database
Data (computing)
PDF
Opinions expressed by DZone contributors are their own.
Comments