Aspose.Pdf

Use Xml as Template

Need of Template

 

Sometimes the PDF document you want to generate is a report with a series of objects with the same format. For example, the Invoice report in the Demos of Aspose.Pdf have 100 orders with the same format. When many documents have the same format then it's a better idea to define a format as a template that can be adopted by other documents.

 

XML as a Template

 

It is possible for Aspose.Pdf developers to define the format of one object in XML and use it as a template to generate the reports. Please follow the general steps given below to learn about how to use an XML as a template:

 

 

The following example shows how to use an XML as a template. In this example, a Section with one Table is defined in the XML as template. When the XML is loaded into Aspose.Pdf , a new Table is cloned and added into the Section . And then a new Section is cloned and added into the document.

 

Code Snippet

 

[C#]

 

 

//Create a Pdf instance and bind the XML template file to Pdf instance

Pdf pdf1 = new Pdf();

pdf1.BindXML("Template.xml",null);

 

 

//Get the section and then table from the obtained section of the Pdf that

//is built from the XML template

Section sec1 = pdf1.Sections["Section1"];

Table table1 = sec1.Paragraphs["Table1"] as Table;

 

 

//Clone a new table

Table table2 = table1.CompleteClone() as Table;

 

 

//Change the ID of table2 to "Table2" to make it different from table1

table2.ID = "Table2";

 

 

//Add table2 into the section

sec1.Paragraphs.Add(table2);

 

 

//Now there are 2 segments with ID "Item",

//We change the IDs to make sure they are different

Segment item = sec1.GetObjectByID("Item") as Segment;

item.ID = "Item1";        

item = sec1.GetObjectByID("Item") as Segment;

item.ID = "Item2";

 

 

//Change the content

item.Content = "item 2";

 

 

//Now clone section1

Section sec2 = sec1.CompleteClone() as Section;

 

 

//Add a cloned section to the Pdf and change the contents of the text segments

//in the section2 of the Pdf object       

pdf1.Sections.Add(sec2);

item = sec2.GetObjectByID("Item1") as Segment;

item.Content = "item1 sec2";

item = sec2.GetObjectByID("Item2") as Segment;

item.Content = "item2 sec2";

 

 

//Save the Pdf

pdf1.Save("XmlTemp.pdf");

 

 

[VB.NET]

 

 

'Create a Pdf instance and bind the XML template file to Pdf instance       

Dim pdf1 As Pdf = New Pdf()

pdf1.BindXML("Template.xml",Nothing)

 

 

'Get the section and then table from the obtained section of the Pdf that

'is built from the XML template

Dim sec1 As Section = pdf1.Sections("Section1")

Dim table1 As Table = sec1.Paragraphs("Table1")

 

 

'Clone a new table

Dim table2 As Table = table1.CompleteClone()

 

 

'Change the ID of table2 to make it different from table1

table2.ID = "Table2"

 

 

'Add table2 into the section

  sec1.Paragraphs.Add(table2)

 

 

'Now there are 2 segments with ID "Item",

'We change the IDs to make sure they are different

Dim item As Segment = sec1.GetObjectByID("Item")       

item.ID = "Item1"

item = sec1.GetObjectByID("Item")

item.ID = "Item2"

 

 

'Change the content

item.Content = "item 2"

 

 

'Now clone section1

Dim sec2 As Section = sec1.CompleteClone()

 

 

'Add a cloned section to the Pdf and change the contents of the text segments

'in the section2 of the Pdf object

pdf1.Sections.Add(sec2)

item = sec2.GetObjectByID("Item1")

item.Content = "item1 sec2"

item = sec2.GetObjectByID("Item2")

item.Content = "item2 sec2"

 

 

'Save the Pdf       

pdf1.Save("test.pdf")

 

[XML]

 

 

<?xml version="1.0" encoding="utf-8" ?>

  <Pdf xmlns="Aspose.Pdf">

   <Section ID="Section1">

        <Table ID="Table1" ColumnWidths="100 100">

         <DefaultCellBorder>

                 <All LineWidth="0.1" />

         </DefaultCellBorder>

         <Row>

          <Cell>

           <Text>

                   <Segment ID="Item">item 1</Segment>

           </Text>

          </Cell>

          <Cell>

           <Text>

                   <Segment ID="Item">some content</Segment>

           </Text>

          </Cell>

         </Row>

        </Table>

   </Section>

  </Pdf>