Quantcast
Channel: Programmers Heaven Forums RSS Feed
Viewing all articles
Browse latest Browse all 2703

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

$
0
0
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# 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("Con
tent");        

 //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;
 }

'[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

Read more: ttp://www.aspose.com/.net/pdf-component.aspx

Viewing all articles
Browse latest Browse all 2703

Trending Articles