DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

How to Integrate Table with Database (DOM) & Populate Data in PDF using .NET

David Zondray user avatar by
David Zondray
·
Nov. 19, 14 · · Code Snippet
Like (0)
Save
Tweet
721 Views

Join the DZone community and get the full member experience.

Join For Free
This 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.

Popular on DZone

  • How to Use Geofences for Precise Audience Messaging
  • A Guide to Events in Vue
  • How Data and Analysis Can Power Agile Teams
  • The Most Popular Technologies for Java Microservices Right Now

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo