In the previous sessions of this Programmer's Guide Reference, we have seen that it is possible to produce a PDF document from a simple XML file. In that case, all the content stored in the XML file is converted to a PDF document. But sometimes, a programmer may require to import the content from an XML file and then control the XML content at runtime before converting into PDF document.
This feature can be achieved by combining the XML and Aspose.Pdf simple API.
Note: The input XML file must follow the Aspose.Pdf Schema .
Please follow these steps to create a PDF document using the combination of an XML file and Aspose.Pdf API:
Code Snippet
[Input XML File]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<Text ID="Text1"> </Text>
</Section>
</Pdf>
[C#]
//Create pdf document
Pdf pdf1 = new Pdf();
//Instantiate License class and call its SetLicense method to use the license
Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense("Aspose.Pdf.lic");
//Bind XML into the document
pdf1.BindXML("../../../Example-XML/HelloWorld.XML",null);
//Get the first Section from the PDF document
Section sec1 = pdf1.Sections[0];
//Get the Text paragraph (whose ID is Text1) from the section
Text text1 = sec1.Paragraphs["Text1"] as Text;
//Add a text Segment to the text paragraph
text1.Segments.Add("Hello World");
//Save the document
pdf1.Save("HelloWorld.pdf");
[VB.NET]
'Create pdf document
Dim pdf1 As Pdf = New Pdf()
'Instantiate License class and call its SetLicense method to use the license
Dim license As license = New license
License.SetLicense("Aspose.Pdf.lic")
'Bind XML into the document
pdf1.BindXML("../../Example-XML/HelloWorld.XML", Nothing)
'Get the first Section from the PDF document
Dim sec1 As Section = pdf1.Sections(0)
'Get the Text paragraph (whose ID is Text1) from the section
Dim text1 As Text = sec1.Paragraphs("Text1")
'Add a text Segment to the text paragraph
text1.Segments.Add("Hello World")
'Save the document
pdf1.Save("HelloWorld.pdf")