Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Load Data in XML Template & Convert it to PDF Document in C# & VB.NET

sheraz786sheraz786 Posts: 15Member
This technical tip explains how to load data in XML template and convert to PDF. We will discuss the approach on how to load the XML file and fill different XML nodes with data. To generate PDF documents here are the steps we are going to follow, Create XML, Bind XML & Save Pdf. IF you are feeling inconvenient of editing complex XML files with rich format settings. So, if there is a tool which could generate all these fixed contents such as Tables with Border Style, Margins and Padding, Headings or Floating Box etc. then what you need to do, is to just fill all data dynamically. This approach would not only be time saving but it will also make your applications more efficient. The following code snippet shows the main approach of filling data to get the resulting PDF document.

[code]

//C# Code

static void Main(){

//Creating new a Pdf object
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

//Binding the content from the named XML file
pdf.BindXML("Sample.xml", null);

//In a real scenario, data is usually input from Database. So, we can get data
//from a database. In this case, we are using a method that will provide us an
//instance of DataTable. The implementation of this method is also given below.
DataTable getDT = creatDataTable();

//Accessing a table through its ID
Aspose.Pdf.Generator.Table contenTable = (Aspose.Pdf.Generator.Table)pdf.GetObjectByID("Content");

//Importing data from a DataTable and filling the table in PDF document
contenTable.ImportDataTable(getDT,false,1,1,5,4);

//Saving the results
pdf.Save("Sample.pdf");
}

//The implementation of createDataTable method that is being called in the program
public static DataTable creatDataTable(){

//Creating a DataTable object
DataTable dt = new DataTable("Sample");

//Adding columns to the DataTable
dt.Columns.Add("Beginning of lease",typeof(Int32));
dt.Columns.Add("End of lease",typeof(Int32));
dt.Columns.Add("Landlord's end-of-lease assessment",typeof(string));
dt.Columns.Add("Comments",typeof(string));

//Adding rows to the DataTable
DataRow dr = dt.NewRow();
dr[0] = 12;
dr[1] = 11;
dr[2] = "$32.38";
dr[3] = "M";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 22;
dr[1] = 22;
dr[2] = "$148.45";
dr[3] = "G";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 41;
dr[1] = 41;
dr[2] = "$11.42";
dr[3] = "S,R";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 47;
dr[1] = 40;
dr[2] = "$48.52";
dr[3] = "D";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 28;
dr[1] = 20;
dr[2] = "$78.43";
dr[3] = "R";
dt.Rows.Add(dr);

//Returning the instance of DataTable object
return dt;
}
[/code]
[code]
'[VB.NET Code]

Shared Sub Main()

'Creating new a Pdf object
Dim pdf As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()

'Binding the content from the named XML file
pdf.BindXML("Sample.xml", Nothing)

'In a real scenario, data is usually input from Database. So, we can get data
'from a database. In this case, we are using a method that will provide us an
'instance of DataTable. The implementation of this method is also given below.
Dim getDT As DataTable = creatDataTable()

'Accessing a table through its ID
Dim contenTable As Aspose.Pdf.Generator.Table = CType(pdf.GetObjectByID("Content"), Aspose.Pdf.Generator.Table)

'Importing data from a DataTable and filling the table in PDF document
contenTable.ImportDataTable(getDT, False, 1, 1, 5, 4)

'Saving the results
pdf.Save("Sample.pdf")
End Sub

'The implementation of createDataTable method that is being called in the program
Public Shared Function creatDataTable() As DataTable

'Creating a DataTable object
Dim dt As DataTable = New DataTable("Sample")

'Adding columns to the DataTable
dt.Columns.Add("Beginning of lease", Type.GetType(Int32))
dt.Columns.Add("End of lease", Type.GetType(Int32))
dt.Columns.Add("Landlord's end-of-lease assessment", Type.GetType(Of String))
dt.Columns.Add("Comments", Type.GetType(Of String))

'Adding rows to the DataTable
Dim dr As DataRow = dt.NewRow()
dr(0) = 12
dr(1) = 11
dr(2) = "$32.38"
dr(3) = "M"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 22
dr(1) = 22
dr(2) = "$148.45"
dr(3) = "G"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 41
dr(1) = 41
dr(2) = "$11.42"
dr(3) = "S,R"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 47
dr(1) = 40
dr(2) = "$48.52"
dr(3) = "D"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 28
dr(1) = 20
dr(2) = "$78.43"
dr(3) = "R"
dt.Rows.Add(dr)

'Returning the instance of DataTable object
Return dt
End Function
[/code]
Read more: [link=http://www.aspose.com/.net/pdf-component.aspx]ttp://www.aspose.com/.net/pdf-component.aspx[/link]
Sign In or Register to comment.