Important alert: (current site time 7/16/2013 7:33:02 AM EDT)
 

article

Merge Xml Files in PDF Document & Reduce Code Duplication

Email
Submitted on: 3/15/2011 2:49:12 PM
By: aspose_seo 
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2537
 
     This technical tip shows how to merge Xml Files in PDF Document. Sometimes different PDF documents share some same content. For example, a number of PDF documents may have the same header or footer. In such cases, a lot of code duplication might be experienced by developers. 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.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

This technical tip shows how to merge Xml Files in PDF Document. Sometimes different PDF documents share some same content. For example, a number of PDF documents may have the same header or footer. In such cases, a lot of code duplication might be experienced by developers. 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.
An example code snippet is demonstrated below that uses an XSLT file using XslTransform class to combine the shared content from different XML files and produce a combined XML file that is then bound to 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 save method of the Pdf class.

Example: .Net Framework 2.0 and VS 2005

 [C#]

FileStream fs1 = new FileStream(@"D:\AsposeTest\Example.xml", FileMode.Open);
FileStream fs2 = new FileStream(@"D:\AsposeTest\Example.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

[C#]

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");

[VB.NET]

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 = 0
xmlDoc.Load(ms)
ms.Close()

Dim pdf As Pdf = New Pdf()
pdf.BindXML(xmlDoc, Nothing)

pdf.Save("e:/temp/test.pdf")

[test.xslt]

<?xml version="1.0" encoding="utf-8" ?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <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>

[header.xml]

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

[content.xml]

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

More about Aspose.Pdf for .NET

Contact Information

Aspose Pty Ltd
Suite 163, 79 Longueville Road
Lane Cove, NSW, 2066
Australia
Aspose – Your File Format Experts
sales@aspose.com

Phone: 888.277.6734
Fax: 866.810.94651


Other 16 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.