Load Data in XML Template and Save as PDF Document using C and VB.NET


This technical tip shares code in C and VB.NET for loading data in XML template, binding that data and then converting it into pdf format.Aspose.Pdf is a .NET Pdf component to write Pdf documents without using Adobe Acrobat. It supports form field creation, document, text & page properties, color space, text, heading and attachment settings.

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.

[C#]

 
static void Main(){

//Creating new a Pdf object
Aspose.Pdf.Generator.Pdfpdf = 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.
DataTablegetDT = creatDataTable();

//Accessing a table through its ID
Aspose.Pdf.Generator.TablecontenTable = (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 DataTablecreatDataTable(){

//Creating a DataTable object
DataTabledt = 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
DataRowdr = 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
returndt;
}

[VB]

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


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: