Hi,
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.
[C#]
// 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";
dt.Rows.Add(dr);
}
//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");
else
// Background of Odd rows is Pink
currentrow.BackgroundColor = new Aspose.Pdf.Color("Pink");
}
//Save the Pdf
pdf1.Save(@"d:/pdftest/TableTemplate_XML.pdf");
[XML]
<?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>
<Row ID="Row2">
</Row>
</Table>
</Section>
</Pdf>
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.