Create Template and fill in data dynamically

Hi Aspose,

I am trying to create a template xml then fill in the data dynamically.How can I achieve this?
I found the below thread. However, couldn’t find sample.xml file. And how to generate my own xml template file. Please advise.
Working with XML using C#|Aspose.PDF for .NET


Regards,
Su

Hi Su,

Thanks for your inquiry. Please find sample XML (Aspose.Pdf XSD schema compliant) and sample code to fill it. Please note that this approach uses the old generator (Aspose.Pdf.Generator), which will be obsolete in the future. We are shifting to the new generator (Aspose.Pdf), which is more efficient and improved. It can be used for both creating a PDF document from scratch or manipulating an existing one.

We have created a feature request PDFNEWNET-39380 for the subject feature in the new generator. We will notify you as soon as it is implemented in the new generator, so you can migrate accordingly.

Pdf pdf = new Pdf();
pdf.BindXML("E:/Data/Template_new.xml", null);

DataTable getDT = creatDataTable();

// Get the section and then table from
// the obtained section of the Pdf that
// is built from the XML template
Section section1 = pdf.Sections["Section1"];

Aspose.Pdf.Generator.Table table1 = (Aspose.Pdf.Generator.Table)section1.Paragraphs["Table1"];
table1.ImportDataTable(getDT, true, 0, 0, 5, 2);

// Save the document
pdf.Save("E:/Data/XML_As_Template.pdf");
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));

    //Adding rows to the DataTable
    DataRow dr = dt.NewRow();
    dr[0] = 12;
    dr[1] = 11;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 22;
    dr[1] = 22;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 41;
    dr[1] = 41;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 47;
    dr[1] = 40;
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr[0] = 28;
    dr[1] = 20;
    dt.Rows.Add(dr);

    //Returning the instance of DataTable object
    return dt;
}

Please feel free to contact us for any further assistance.

Best Regards,