Merge XML files into a PDF Document in .NET

Merge XML files into a PDF Document in .NET

Sometimes different PDF documents have some content in common. For example, a number of PDF documents may have the same header or footer. In such cases, .NET developers who are responsible for document creation might go through a lot of code duplication. To reduce this code duplication, it’s a good practice to save the content (shared by multiple PDF documents) into different XML files and then merge the XML files together before generating PDF documents.

The example code snippets below use an XSLT file using the XslTransform class to combine the shared content from different XML files and produce a combined XML file. The combined XML file is then bound to a PDF object by calling its BindXML method. After the final XML file is bound, then it can be saved as a PDF document by calling the save method of the PDF class.

Example: .NET Framework 2.0 and VS 2005


FileStream fs1 = new FileStream(@"D:AsposeTestExample.xml", FileMode.Open);FileStream fs2 = new FileStream(@"D:AsposeTestExample.xslt", FileMode.Open); Pdf pdf1 = new Pdf(); pdf1.BindXML(fs1, fs2); pdf1.Save("D:/Asposetest/XMlXSLTMERGE.pdf"); fs1.Close();fs2.Close();

Example: .NET Framework 1.1 and VS 2003


XmlDocument xmlDoc = new XmlDocument();MemoryStream ms = new MemoryStream();  XslTransform xsl = new XslTransform(); xsl.Load("test.xslt"); xsl.Transform(xmlDoc,null,ms);  ms.Position = 0; xmlDoc.Load(ms); ms.Close();  Pdf pdf = new Pdf(); pdf.BindXML(xmlDoc,null);  pdf.Save("e:/temp/test.pdf");


Dim xmlDoc As XmlDocument = New XmlDocument()Dim ms As MemoryStream = New MemoryStream() Dim xsl As XslTransform = New XslTransform()xsl.Load("test.xslt")xsl.Transform(xmlDoc, Nothing, ms) ms.Position = 0xmlDoc.Load(ms)ms.Close() Dim pdf As Pdf = New Pdf()pdf.BindXML(xmlDoc, Nothing) pdf.Save("e:/temp/test.pdf")


<?xml version="1.0" encoding="utf-8" ?>   <xsl:stylesheet version="1.0" xmlns:xsl="">    <xsl:template match="/">        <Pdf xmlns="Aspose.Pdf">          <Section>                  <Header>                          <xsl:copy-of select="document('header.xml')"/>                  </Header>                  <xsl:copy-of select="document('content.xml')"/>          </Section>        </Pdf>    </xsl:template> </xsl:stylesheet>


<Text>         <Segment>header</Segment> </Text>


<Text>          <Segment>Hello world</Segment> </Text>

Find out more about Aspose.Pdf for .NET at their website.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved