3. Converting an XML document to RTF

This second sample consists in three steps:

  1. Compile the XSLT style sheet for all subsequent uses.

  2. Invoke the XSLT engine to convert the input XML document to XSL-FO.

  3. Invoke XMLmind XSL-FO Converter to convert the temporary XSL-FO file generated by second step to RTF.

Excerpts of samples/dotnet/Sample2.cs:

using System.Xml.Xsl;
using XmlMind.FoConverter;

...
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(ToUri(xslPath));1

            string xmlUri = ToUri(xmlPath);
            foPath = Path.GetTempFileName();
            transform.Transform(xmlUri, foPath);2

            Converter converter = new Converter();3
            converter.OutputFormat = OutputFormat.Rtf;
            converter.OutputEncoding = "windows-1252";
            converter.ImageResolution = 72;
            converter.BaseUrl = xmlUri;4

            converter.SetInput(ToUri(foPath));
            converter.SetOutput(rtfPath);
            converter.Convert();5
...

1

Compile the XSLT style sheet.

[Important]About the thread safety of XMLmind XSL-FO Converter

A Converter instance must not be shared between different threads.

2

Transform the XML input file to a temporary output file created in the system-dependant temporary file directory.

3

Create and parameterize a Converter object as explained in Section 2, “Converting an XSL-FO file to RTF”.

4

Setting the BaseUrl property to the URL of the XML input file is really needed in our case:

If the XML input file references graphics files using relative URLs (example: images/screenshot1.png), then the generated XSL-FO file is likely to contain fo:external-graphic objects referencing the same graphics files using the same relative URLs. The problem is that, in our case, the XSL-FO file is not generated in the same directory as the XML input file. Therefore, without the BaseUrl property, these relative URLs would be resolved incorrectly by XMLmind XSL-FO Converter.

An advanced alternative to specifying a BaseUrl property, is to specify an IUriResolver object using Converter.SetUriResolver.

5

Perform the conversion by invoking Converter.Convert.