We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Using an XML Template

I am trying to learn how to use an XML document as a template with Aspose.PDF for .NET

Unfortunately the example provided in the documentation area of the website (under Programmer's Guide / Advance features / Use XML as template) fails with an error message "Object reference not set to an instance of an object" and appears to fail at the line

item.Content = "item2 sec2";

Please can you tell me how the corrected code should look so that I can figure out how to use xml as a template.

Many thanks

Hello Graham,

Thanks for considering Aspose.

I have tested the scenario and I am able to notice the issue with latest version 4.3.0. For the sake of correction, I have logged it in our issue tracking system as PDFNET-16938. We will look into the details of this problem and will update you with the status of correction.

Meanwhile, please visit the following links for information on

In case you still face any problem or you have any further query, please feel free to contact.

We apologize for your inconvenience.


While waiting for the issue to be fixed, can you suggest a different way to achieve the same result as below codes?

item.content="item2 sec2";

Do you know how long it will take to get this issue fix?

Thank you.

Hello Graham,

The issue has just been logged and before we have analyzed this problem in details, I am afraid its pretty hard to share the ETA regarding its resolution. As soon as we have some updates, we would be pleased to update you with the status of correction.

Can you please share some more details regarding your exact requirement, so that I can share the appropriate code snippet.

We apologize for your inconvenience.

The requirement is basically to be able to use an XML file as a document template. The code snippet shown in the Aspose documentation does not work, so I need a code snippet that does work.

The exact requirement is described in the Aspose documentation. If the code snippet will do what your own documentation says that's fine as I will be able to copy this for my own requirements.


Hello Graham,

We are creating a very simple example for you that can help you to understand this XML template concept. Please spare us little time and we will get back to you, shortly.

We apologize for the delay and inconvenience.


Thanks for your patience.

Please take a look over the following code snippet which explains the concept of using XML document as a template while generating the PDF document.

In following code, first the DataTable is created which contains the data information. An XML template containing table structure is loaded and finally a specific background color is applied over alternate rows (except header which has a specific header). If the table exceeds the first page, the Header Row will be repeated over subsequent pages. The resultant PDF that I have generated is also in attachment. Please take a look.


// Create a DataTable object (Employee) and add columns to it (Employee_ID, Employee_Name, Gender).
DataTable dt = new DataTable("Employee");
dt.Columns.Add("Employee_ID", typeof(Int32));
dt.Columns.Add("Employee_Name", typeof(string));
dt.Columns.Add("Gender", typeof(string));
dt.Columns.Add("Address", typeof(string));

//Add some rows into the DataTable object programmatically
int rowcount = 1;
for (rowcount = 1; rowcount <= 58; rowcount++)
DataRow dr = dt.NewRow();
dr[0] = rowcount;
dr[1] = "Nayyer Shahbaz";
dr[2] = "Male";
dr[3] = "House 336";

//Create a Pdf instance and bind the XML template file to Pdf instance
Pdf pdf1 = new Pdf();
pdf1.BindXML("D:/pdftest/PDFConversiontest.xml", null);

//Get the section and then table from the obtained section of the Pdf that
//is built from the XML template
Aspose.Pdf.Section section1 = pdf1.Sections["Section1"];
//Create a Table object
Aspose.Pdf.Table table1 = section1.Paragraphs["Table1"] as Aspose.Pdf.Table;

//Get 1st row from the table
Aspose.Pdf.Row row1 = table1.Rows["Row1"] as Aspose.Pdf.Row;
// Specify the background color of the Header Row
row1.BackgroundColor = new Aspose.Pdf.Color((byte)8, (byte)(9 * 10), (byte)(9 * 20));

//Import data into the Table object from the DataTable created above
table1.ImportDataTable(dt, true, 0, 0, rowcount, 4);

for (int i = 1; i < rowcount; i++)
Aspose.Pdf.Row currentrow = table1.Rows[i];
if (i % 2 == 0)
// Background of Even rows is Silver
currentrow.BackgroundColor = new Aspose.Pdf.Color("Silver");
// Background of Odd rows is Pink
currentrow.BackgroundColor = new Aspose.Pdf.Color("Pink");

//Save the Pdf


<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section ID="Section1">
<Table ID="Table1" ColumnWidths="100 100 100 100" IsFirstRowRepeated="true">

<All LineWidth="0.1" />

<Row ID="Row1">
<Row ID="Row2">

You may also visit the following links for information on topics covered in this example.

In case it does not satisfy your requirements or you have any further query, please feel free to contact.

Thank you for this, although it isn't really what I was expecting. My idea of a remplate is a fixed layout into which I can insert different values of data.

I have created an XML file containing a page layout with text and image paragraphs. Some text is fixed whilst other text comes from a database. I want to be able to produce a multi-page document from this where every page has the same layout, but the text and images on the different pages are filled from the database.

In summary, the XML file provides a single layout, but the database allows me to create multiple pages with the different content.

How can I achieve this?


Sorry for replying you late.

Can you please share the sample XML document that you have generated, so that we can try generating the desired output at our end. We are sorry for the delay and inconvenience.

The simple template is attached. For each page that is used I would like to specify a page title in the field TextPageTitle, text in the field TextRecommend and images in Image1 and Image2



Hi Graham,

Thanks for sharing the resource file.

We are working over this query and will reply to you soon. Thanks for your patience and understanding.

The issues you have found earlier (filed as 16938) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.