Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Created March 8, 2024 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conholdate-gists/535941cd78801c54a926c2e3ecf0b6bc to your computer and use it in GitHub Desktop.
Save conholdate-gists/535941cd78801c54a926c2e3ecf0b6bc to your computer and use it in GitHub Desktop.
Convert XML to PDF in C# .NET
// Instantiate Document object
Document doc = new Document();
// Bind source XML file
doc.BindXml(dataDir + "input.xml");
// Convert XML to PDF
doc.Save(dataDir + "XMLToPDF.pdf");
//Create pdf document
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
//Bind XML and XSLT files
try
{
pdf.BindXml("hello-world.xml", "hello-world.xslt");
}
catch (System.Exception)
{
throw;
}
//Save the document
pdf.Save("HelloWorldXml.pdf");
<?xml version="1.0" encoding="utf-8" ?>
<Contents>
<Content>Hello World!</Content>
</Contents>
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()"/>
<xsl:template match="/Contents">
<html>
<Document xmlns="Aspose.Pdf" IsAutoHyphenated="false">
<PageInfo>
<DefaultTextState
Font = "Helvetica" FontSize="8" LineSpacing="4"/>
<Margin Left="5cm" Right="5cm" Top="3cm" Bottom="15cm" />
</PageInfo>
<Page id="mainSection">
<TextFragment>
<TextSegment>
<xsl:value-of select="Content"/>
</TextSegment>
</TextFragment>
</Page>
</Document>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8" ?>
<Document xmlns="Aspose.Pdf">
<Page id="mainPage">
<TextFragment>
<TextSegment>Hello</TextSegment>
</TextFragment>
<TextFragment>
<TextSegment>World!</TextSegment>
</TextFragment>
</Page>
</Document>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment