The following code exemplifies how to convert XSL-FO to PDF:
using System;
using System.IO;
using Ecrion.Ultrascale;
namespace HelloWorld
{
class Program
{
[STAThread]
static void Main(string[] args)
{
String outFilePath = @"C:\temp\HelloWorld.pdf";
FileStream outputStream = null;
try
{
//XSL-FO input
String xml = "<fo:root xmlns:fo='http://www.w3.org/1999/XSL/Format'>" +
"<fo:layout-master-set>" +
"<fo:simple-page-master master-name='all-pages'>" +
"<fo:region-body region-name='xsl-region-body' margin='0.7in'/>" +
"</fo:simple-page-master>" +
"</fo:layout-master-set>" +
"<fo:page-sequence master-reference='all-pages'>" +
"<fo:flow flow-name='xsl-region-body'>" +
"<fo:block>Hello World!</fo:block>" +
"</fo:flow>" +
"</fo:page-sequence>" +
"</fo:root>";
// Get bytes of string
byte[] stringBytes = System.Text.Encoding.UTF8.GetBytes(xml);
// Create input stream
Stream inputStream = new MemoryStream(stringBytes);
// Initialize data source
IDataSource input = new XmlDataSource(inputStream, Engine.InputFormat.XSLFO);
// Create output stream
outputStream = new FileStream(outFilePath, FileMode.Create);
// Create parameters for the rendering operation.
RenderingParameters param = new RenderingParameters();
// Set output format
param.OutputFormat = Engine.OutputFormat.PDF;
// Creates print ready documents using Ecrion XF Ultrascale engine
Engine engine = new Engine();
engine.Render(input, outputStream, param);
Console.WriteLine("Document rendered successfully!\n");
}
catch (Exception e)
{
// Report errors
Console.Out.WriteLine(e.Message);
if (e.InnerException != null)
Console.Out.WriteLine(e.InnerException.ToString());
// Close and delete the (partial) output file if any
if (outputStream != null)
{
outputStream.Close();
outputStream = null;
}
if (File.Exists(outFilePath))
File.Delete(outFilePath);
}
}
}
}