Introduction
In this topic, we will discuss that how can you make use of Aspose.Word to create an XML template that can be converted to PDF document after modifying its contents at runtime.
Need of Template
When you are going to generate PDF documents, you would be familiar with the following steps:
And you must be 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.
Aspose.Word as an XML Generating Tool
Conventions from Word to Pdf are well known to Aspose community but there is a very few number of users who know to utilize Aspose.Word as an XML generator. To avoid making up complicated XML, you can firstly use Aspose.Word to edit the appearance and then save it as XML. After a litter modification, you can finally save this XML file as a PDF document.
Example
1. Edit the appearance with Aspose.Word
First of all, write all details of layout with Aspose.Word. The following word document is an elaborating demonstration:
http://www.aspose.com/Products/Aspose.Pdf/Sample.doc
2. Get Ready
Before generating PDF document, we should appoint a place which will be filled with data. Please take a look at the image below:
|
We will use Pdf.GetObjectByID Method to locate where to add data.
Here is the created XML file:
http://www.aspose.com/Products/Aspose.Pdf/Sample.xml
3. Fill Data and Save PDF
The following code snippet show the main approach of filling data to get the resulting PDF document. Here is the PDF file:
http://www.aspose.com/Products/Aspose.Pdf/Sample.pdf
Code Snippet
[C#]
static void Main(){
//Creating new a Pdf object
Aspose.Pdf.Pdf pdf = new Aspose.Pdf.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.Table contenTable = (Aspose.Pdf.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;
}
[VB.NET]
Shared Sub Main()
'Creating new a Pdf object
Dim pdf As Aspose.Pdf.Pdf = New Aspose.Pdf.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.Table = CType(pdf.GetObjectByID("Content"), Aspose.Pdf.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(String))
dt.Columns.Add("Comments",Type.GetType(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