Convert XSL-FO to PDF Samples
Here are two typical "Hello World" samples in Java, C# that will show how to convert XSL-FO to PDF using XF Rendering Server.
C# Code Sample
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ecrion.Ultrascale;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
try
{
//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();
//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 and output streams
Stream inputStream = new MemoryStream(stringBytes);
// Initialize data source
IDataSource input = new XmlDataSource(inputStream, Engine.InputFormat.XSLFO);
using (FileStream outputStream = new FileStream("C:\\HelloWorld.pdf", FileMode.Create))
{
// The 'using' statement will ensure that the output stream
// is closed even if an exception occurs
engine.Render(input, outputStream, param);
}
Console.WriteLine("Document rendered successfully!\n");
}
catch (Exception e)
{ // Report any errors that may occur
Console.WriteLine(e);
}
}
}
}
|
Java Code Sample
import java.io.*;
import ecrion.ultrascale.*;
public class HelloWorld
{
public static void main(String[] args)
throws IOException
{
String outputFile = "C:\\Temp\\HelloWorld.pdf";
OutputStream outputStream = null;
try
{
XSL-FO input
String xslFo = "<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>";
InputStream inputStream = new StringBufferInputStream(xslFo);
// Initialize data source
IDataSource input = new XmlDataSource(inputStream, Engine.InputFormat.XSLFO);
// create parameters for the rendering operation
RenderingParameters rp = new RenderingParameters();
// set output format
rp.OutputFormat = Engine.OutputFormat.PDF;
// create a new engine
Engine eng = new Engine();
// create an output stream
outputStream = new FileOutputStream(outputFile);
// render
eng.Render(input, outputStream, rp);
// remember to close the stream
outputStream.close();
// Do something here with the output stream
System.out.println("Document rendered successfully!\n");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
// remember to close the streams
if(inputStream != null)
inputStream.close();
if(outputStream != null)
outputStream.close();
}
}
}
|
Convert XML to PDF
The following samples in Java and C# show how to convert XML to PDF using XF Rendering Server. The layout of the document is placed in a template file (standard XSL templates or the more user friendly XFD files created using XF Designer).
C# Code Sample
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using System.Reflection;
using Ecrion.Ultrascale;
namespace XmlToPdf
{
class Program
{
static void Main(string[] args)
{
try
{
String xmlString =
"<person name='Joe Doe' age='65'/>";
String xslString =
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
" <xsl:template match='/'>" +
" <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>Name: <xsl:value-of select='person/@name'/></fo:block>" +
" <fo:block>Age: <xsl:value-of select='person/@age'/></fo:block>" +
" </fo:flow>" +
" </fo:page-sequence>" +
" </fo:root>" +
" </xsl:template>" +
"</xsl:stylesheet>";
//create template stream
Stream xsltString = new MemoryStream(Encoding.UTF8.GetBytes(xslString));
//create input stream
Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
using (Stream outputStream = new FileStream("C:\\XmlToPdf.pdf", FileMode.Create))
{
// The 'using' statement will ensure that the output stream
// is closed even if an exception occurs
// Prepare the input parameters for the rendering operation.
RenderingParameters param = new RenderingParameters();
// Initialize data source
IDataSource input = new XmlDataSource(xmlStream, Engine.InputFormat.XSLFO);
param.OutputFormat = Engine.OutputFormat.PDF;
param.Template = new LocalDocumentTemplate(xsltString, null, Engine.XsltEngine.DotNet20, null, null);
// Creates an instance of the engine and call render()
Engine engine = new Engine();
engine.Render(input, outputStream, param);
Console.WriteLine("Document rendered successfully!\n");
}
}
catch (Exception e)
{
Console.WriteLine(e); // Report any errors that may occur
}
}
}
}
|
Java Code Sample
import java.io.*;
import ecrion.ultrascale.*;
public class XmltoPdf
{
public static void main(String[] args)
{
try
{
String xmlString =
"<person name='Joe Doe' age='65'/>";
String xslString =
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
" <xsl:template match='/'>" +
" <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>Name: <xsl:value-of select='person/@name'/></fo:block>" +
" <fo:block>Age: <xsl:value-of select='person/@age'/></fo:block>" +
" </fo:flow>" +
" </fo:page-sequence>" +
" </fo:root>" +
" </xsl:template>" +
"</xsl:stylesheet>";
InputStream xmlStream = new StringBufferInputStream(xmlString);
InputStream xsltStream = new StringBufferInputStream(xslString);
OutputStream outputStream = new FileOutputStream("C:\\XmlToPdf.pdf");
// Initialize data source
IDataSource input = new XmlDataSource(xmlStream, Engine.InputFormat.XSLFO);
Engine eng = new Engine();
RenderingParameters rp = new RenderingParameters();
rp.OutputFormat = Engine.OutputFormat.PDF;
rp.Template = new LocalDocumentTemplate(xsltStream, null, Engine.XsltEngine.Java, null, null);
eng.Render(input, outputStream, rp);
// remember to close the streams
xmlStream.close();
xsltStream.close();
outputStream.close();
System.out.println("Document rendered successfully!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
|
Convert DOCX to PDF Samples
To convert DOCX/WordML to PDF simply change the input format accordingly. Below you will find two samples in Java, C# that will show how to convert DOCX to PDF using XF Rendering Server.
C# Code Sample
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ecrion.Ultrascale;
namespace DocXToPDF
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
System.Console.Error.WriteLine("USAGE: DocXToPDF.exe inputFile.docx.");
return;
}
String inputFile = Path.GetFullPath(args[0]);
String outputFile = Path.ChangeExtension(inputFile, ".pdf");
try
{
using (Stream inputStream = new FileStream(inputFile, FileMode.Open),
outputStream = new FileStream(outputFile, FileMode.Create))
{
//parameters for the rendering operation.
RenderingParameters param = new RenderingParameters();
// Initialize data source
IDataSource input = new XmlDataSource(inputFile, Engine.InputFormat.DocX);
//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 any errors that may occur
Console.WriteLine(e);
}
}
}
}
|
Java Code Sample
import java.io.*;
import ecrion.ultrascale.*;
public class DocXToPDF
{
public static void main(String[] args)
throws IOException
{
if (args.length != 1)
{
System.err.println("Usage: java DocXToPDF docxFile");
return;
}
OutputStream outputStream = null;
try
{
String inputFile = args[0];
String outputFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";
// Initialize data source
IDataSource input = new XmlDataSource(inputFile, Engine.InputFormat.DocX);
// create parameters for the rendering operation
RenderingParameters rp = new RenderingParameters();
// set output format
rp.OutputFormat = Engine.OutputFormat.PDF;
// create a new engine
Engine eng = new Engine();
// create an output stream
outputStream = new FileOutputStream(outputFile);
// render
eng.Render(input, outputStream, rp);
System.out.println("Document rendered successfully!");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
// remember to close the streams
if(outputStream != null)
outputStream.close();
}
}
}
|
Sample Code
The following samples are included when install the product. The location of the samples has a path similar to the following. Here, it is assumed that C: is the installation drive for XF:
| .NET | JAVA |
|
WatermarkSample - Shows how to combine a XF Designer Template (XFD) with XML and generate PDF. The template has a 45 degrees watermark in the background.
DocXToPDF - Render a DocX (Word 2007) document as PDF. No third party software is used.
HelloWorld - Render XSL-FO text into PDF.
AFPSample - Render XSL-FO text as AFP, and performs a Grayscale conversion on the fly.
MergeSample - Merge multiple input files (XSL-FO, DocX, PDF, etc) into one output PDF file.
PrintOutput - Print a document to the specified printer.
TIFFSample - Render a document into a multipage, LZW compressed TIFF with 150 DPI. CCIT3 and CCIT4 compressions are also supported.
XmlToPDFUsingLocalTemplates - Convert XML to PDF using LocalDocumentTemplate. LocalDocumentTemplate represents a XSL or XFD template available locally to the client application.
XmlToPDFUsingServerTemplates - Convert XML to PDF using ServerDocumentTemplate. ServerDocumentTemplate represents a XSL or XFD template installed in a remote instance of XF Rendering Server.
|
DocXToPDF - Render a DocX (Word 2007) document as PDF. No third party software is used.
HelloWorld - Render XSL-FO text into a PDF.
TIFFSample - Render a document into a multipage, LZW compressed TIFF with 150 DPI. CCIT3 and CCIT4 compressions are also supported.
XmlToPDFUsingLocalTemplates - Convert XML to PDF using LocalDocumentTemplate. LocalDocumentTemplate represents a XSL or XFD template available locally to the client application.
XmlToPDFUsingServerTemplates - Convert XML to PDF using ServerDocumentTemplate. ServerDocumentTemplate represents a XSL or XFD template installed in a remote instance of XF Rendering Server.
|
More Online Samples
- Converting DOCX to PDF: WordMLSample.zip
This is a sample Mail Merge application, which will combine a WordML template with records from an Access database. The conversion is performed on the server without any third party software (you don't need Microsoft Word to perform the rendering operation).
- Generating a PDF document directly into the Web Server's output stream: ASPXSample.zip.
- Converting AFP to PDF: AFPSample.zip
Sample console application which generates an AFP document from XML data combined with a XFD or XSL template.
- DALSample.zip - Sample console application which generates a complex PDF document from multiple inputs, including static PDF documents and dynamic XML combined with XFD templates.