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.IO;
using Ecrion.Ultrascale;
namespace HelloWorld
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
//parameters for the rendering operation.
RenderingParameters param = new RenderingParameters();
//set input format
param.InputFormat = Engine.InputFormat.XSLFO;
//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);
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(inputStream, 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)
{
try
{
// create parameters for the rendering operation
RenderingParameters rp = new RenderingParameters();
// set input format
rp.InputFormat = Engine.InputFormat.XSLFO;
// set output format
rp.OutputFormat = Engine.OutputFormat.PDF;
// create a new engine
Engine eng = new Engine();
// 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);
// create an output stream
OutputStream outputStream = new FileOutputStream("C:\\HelloWorld.pdf");
// render
eng.Render(inputStream, outputStream, rp);
// remember to close the streams
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
};
}
} |
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 XSLTransformation
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
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>";
XPathDocument xml = new XPathDocument(new StringReader(xmlString));
XPathDocument xsl = new XPathDocument(new StringReader(xslString));
//Create a new XslTransform object.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsl, null, null);
MemoryStream xslFo = new MemoryStream();
//Transform the data and send the output in the memory stream.
xslt.Transform(xml, null, new XmlTextWriter(xslFo, Encoding.Unicode));
using (Stream outputStream = new FileStream("C:\\XSLTransformation.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();
param.InputFormat = Engine.InputFormat.XSLFO;
param.OutputFormat = Engine.OutputFormat.PDF;
// Creates an instance of the engine and call render()
Engine engine = new Engine();
engine.Render(xslFo, outputStream, param);
}
}
catch (Exception e)
{
Console.WriteLine(e); // Report any errors that may occur
}
}
}
|
Java Code Sample
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import ecrion.ultrascale.*;
public class XSLTranformation
{
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 xml = new StringBufferInputStream(xmlString);
InputStream xslt = new StringBufferInputStream(xslString);
OutputStream outputStream = new FileOutputStream("C:\\XSLTransformation.pdf");
// JAXP reads data using the Source interface
Source xmlSource = new StreamSource(xml);
Source xsltSource = new StreamSource(xslt);
// the factory pattern supports different XSLT processors
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
ByteArrayOutputStream xmlResult = new ByteArrayOutputStream();
trans.transform(xmlSource, new StreamResult(xmlResult));
InputStream inputStream = new StringBufferInputStream(xmlResult.toString());
Engine eng = new Engine();
RenderingParameters rp = new RenderingParameters();
rp.InputFormat = Engine.InputFormat.XSLFO;
rp.OutputFormat = Engine.OutputFormat.PDF;
eng.Render(inputStream, outputStream, rp);
// remember to close the streams
inputStream.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
{
[STAThread]
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
{
Stream inputStream = new FileStream(inputFile, FileMode.Open);
using (Stream outputStream = new FileStream(outputFile, FileMode.Create))
{
//parameters for the rendering operation.
RenderingParameters param = new RenderingParameters();
//set input format to DocX
param.InputFormat = 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(inputStream, outputStream, param);
inputStream.Close();
}
}
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)
{
try
{
if (args.length != 1)
{
System.err.println(
"Usage: java DocXToPDF docxFile");
return;
}
String inputFile = args[0];
String outputFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";
// create parameters for the rendering operation
RenderingParameters rp = new RenderingParameters();
// set input format
rp.InputFormat = Engine.InputFormat.DocX;
// set output format
rp.OutputFormat = Engine.OutputFormat.PDF;
// create a new engine
Engine eng = new Engine();
InputStream inputStream = new FileInputStream(inputFile);
// create an output stream
OutputStream outputStream = new FileOutputStream(outputFile);
// render
eng.Render(inputStream, outputStream, rp);
// remember to close the streams
inputStream.close();
outputStream.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
};
}
} |
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:
C:\Program Files\Ecrion\XF Ultrascale 2010\Samples\
|
.NET
|
JAVA
|
|
XSLTransformation - Convert XML to PDF.
An XSL transformation is performed using a template designed in
XF Designer then the result is
rendered as PDF.
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.
|
XSLTransformation - Convert XML to PDF.
An XSL transformation is performed using a template designed in
XF Designer then the result is
rendered as PDF.
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.
|
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.