Aspose.Pdf

Converting Xsl-Fo to PDF (Beta Version)

Note that this is the Beta version of Xsl-Fo to PDF. Some complex Xsl-Fo may not be supported well and we may be  not able to fix it in short time.

What is XSL-FO?

 

XSL-FO stands for eXtensible Stylesheet Language Formatting Objects. It's a language for describing a styled document completely. The XSL-FO document contains the medium and appearance specific Formatting Objects that make up the page (or for audio output, the speech). For the print medium, formatting objects can include characters, blocks of text, images, tables, borders, master pages and so on.

 

XSL-FO is not a page description language. It can specify various layout rules (e.g. where page breaks can occur) and requirements (e.g. footnotes go at the bottom of the page) but it doesn't determine the actual placement of each element. That is determined by the XSL-FO Pagination Engine called Formatter. The output from a formatter need not actually drive a printer. Rather, the output might be a PostScript or PDF document, which would need additional rendering software. There are several XSL-FO formatters available that are commonly used to generate PDF on the fly.

 

XSL-FO & Aspose.Pdf

 

With Aspose.Pdf , developers can perform many operations related with XSL-FO as listed below:

 

 

In simple words, developers can provide many kinds of input sources to Aspose.Pdf for having an output in the form of a PDF document or a PDF file stream.

 

Moreover, developers can also customize the Meta Information of the output generated PDF document. There are six types of meta information for the PDF documents that can be customized by developers as follows:

 

 

Currently, XSL-FO feature of Aspose.Pdf doesn't support TIFF and SVG images. But, in future version, Aspose.Pdf will offer many new exciting features besides supporting other image types too.

 

The example given below provides complete demonstration of the almost all possible ways to convert XSL-FO files in any form to PDF document. The example also describes that how can developers set meta information of the PDF documents. In the early lines of code, it is also practiced about how to set license for Aspose.Pdf . These lines are commented for the developers using evaluation versions of Aspose.Pdf . If you have bought a license of Aspose.Pdf then you should un-comment those lines.

 

Code Snippet

 

[C#]

 

using System;

using Aspose.Pdf;

using System.Xml;

using System.IO;

using System.Xml.XPath;

 

 

class HelloWorld

{

   [STAThread]

   public static void  Main(System.String[] args)

   {

     //Set up your product license.

     //If you just want to evaluate Aspose.Pdf, you can annotate these two lines.

     //License license = new License();

     //license.SetLicense("Aspose.Pdf.lic");

 

 

     //Create a Convertor object.

     Pdf app;

 

 

     System.IO.Stream pdf;

     string name = "HelloWorld";

     string fo = name + ".fo";

     string xml = name + ".xml";

     string xsl = name + ".xsl";

 

 

     // Create the XmlDocument.

     XmlDocument doc_fo = new XmlDocument();

     doc_fo.Load(new StreamReader(fo));

     XPathDocument doc_xml = new XPathDocument(xml);

 

 

     //1. fo:string2string

     app = new Pdf();

     app.BindFO(fo);

     app.Save(name+"_fo_sring2string.pdf");

 

 

     //2. fo:string2stream

     app = new Pdf();

     app.BindFO(fo);

     pdf = new System.IO.FileStream

                (name+"_fo_string2stream.pdf",System.IO.FileMode.Create);

     app.Save(pdf);

 

 

     //3. fo:stream2string

     app = new Pdf();

     app.BindFO(new System.IO.FileStream(fo, System.IO.FileMode.Open,

                                        System.IO.FileAccess.Read));

     app.Save(name+"_fo_stream2string.pdf");

 

 

     //4. fo:stream2stream

     app = new Pdf();

     app.BindFO(new System.IO.FileStream(fo, System.IO.FileMode.Open,

                                        System.IO.FileAccess.Read));

     pdf = new System.IO.FileStream

                (name+"_fo_stream2stream.pdf",System.IO.FileMode.Create);

     app.Save(pdf);

 

 

     //5. fo:doc2string

     app = new Pdf();

     app.BindFO(doc_fo);

     app.Save(name+"_fo_doc2string.pdf");

 

 

     //6. fo:doc2stream

     app = new Pdf();

     app.BindFO(doc_fo);

     pdf = new System.IO.FileStream

                (name+"_fo_doc2stream.pdf",System.IO.FileMode.Create);

     app.Save(pdf);

 

 

     //7. xml:string2string

     app = new Pdf();

     app.BindFO(xml,xsl);

     app.Save(name+"_xml_string2string.pdf");

 

 

     //8. xml:string2stream

     app = new Pdf();

     app.BindFO(xml,xsl);

     pdf = new System.IO.FileStream

                (name+"_xml_string2stream.pdf",System.IO.FileMode.Create);

     app.Save(pdf);

 

 

     //9. xml:doc2string

     app = new Pdf();

     app.BindFO(doc_xml,xsl);

     app.Save(name+"_xml_doc2string.pdf");

 

 

     //10. xml:doc2stream

     app = new Pdf();

     app.BindFO(doc_xml,xsl);

     pdf = new System.IO.FileStream

                (name+"_xml_doc2stream.pdf",System.IO.FileMode.Create);

     app.Save(pdf);

 

 

     //0. An example of how to set the metadata of your generated pdf document

     //If you do not want to set these tedious metadata of pdf, you can set the

     //second parameter of Save method to be "null" simply, as shown in above.

 

 

     //At first create a IDictionary to contain your metadata.

     System.Collections.IDictionary metadata = new System.Collections.Hashtable();

 

 

     //Then set the various items.

     //If you don't set them, Aspose.Pdf.Fo will set default value.

     //a. title of your pdf doc:

     metadata.Add("Title","New Title");

     //b. author of your pdf doc:

     metadata.Add("Author","New Author");

     //c. subject of your pdf doc:

     metadata.Add("Subject","New Subject");

     //d. keywords of your pdf doc:

     metadata.Add("Keywords","New Key Words");

     //e. creator of your pdf doc:

     metadata.Add("Creator","New Creator");

     //f. producer of your pdf doc:

     metadata.Add("Producer","New Producer");

 

 

     //Make this IDictionary be the parameter of Save method, and start converting.

     app = new Pdf();

     app.FoMetaData = metadata;

     app.BindFO(fo);

 

 

     app.Save("HelloWorld.pdf");

   }

}

 

[VB.Net]

 

Imports System

Imports System.IO

Imports System.Xml

Imports System.Xml.XPath

Imports Aspose.Pdf

 

 

Public Class HelloWorld

    Shared Sub Main()

        'Set up your product license.

        'If you just want to evaluate Aspose.Pdf.Fo, you can annotate these two lines.

        'License license = new License();

        'license.SetLicense("Aspose.Pdf.lic");

 

 

        'Create a Convertor object.

        Dim app As Pdf

 

 

        Dim pdf As System.IO.Stream

        Dim name As String = "HelloWorld"

        Dim fo As String = name + ".fo"

        Dim xml As String = name + ".xml"

        Dim xsl As String = name + ".xsl"

 

 

        ' Create the XmlDocument.

        Dim doc_fo As XmlDocument = New XmlDocument

        doc_fo.Load(New StreamReader(fo))

        Dim doc_xml As XPathDocument = New XPathDocument(xml)

 

 

        '1. fo:string2string

        app = New Pdf

        app.BindFO(fo)

        app.Save(name + "_fo_sring2string.pdf")

 

 

        '2. fo:string2stream

        app = New Pdf

        app.BindFO(fo)

        pdf = New System.IO.FileStream(name + "_fo_string2stream.pdf",

                                        System.IO.FileMode.Create)

        app.Save(pdf)

 

 

        '3. fo:stream2string

        app = New Pdf

        app.BindFO(New System.IO.FileStream(fo, System.IO.FileMode.Open,

                                        System.IO.FileAccess.Read))

        app.Save(name + "_fo_stream2string.pdf")

 

 

        '4. fo:stream2stream

        app = New Pdf

        app.BindFO(New System.IO.FileStream(fo, System.IO.FileMode.Open,

                                        System.IO.FileAccess.Read))

        pdf = New System.IO.FileStream(name + "_fo_stream2stream.pdf",

                                        System.IO.FileMode.Create)

        app.Save(pdf)

 

 

        '5. fo:doc2string

        app = New Pdf

        app.BindFO(doc_fo)

        app.Save(name + "_fo_doc2string.pdf")

 

 

        '6. fo:doc2stream

        app = New Pdf

        app.BindFO(doc_fo)

        pdf = New System.IO.FileStream(name + "_fo_doc2stream.pdf",

                                         System.IO.FileMode.Create)

        app.Save(pdf)

 

 

        '7. xml:string2string

        app = New Pdf

        app.BindFO(xml, xsl)

        app.Save(name + "_xml_string2string.pdf")

 

 

        '8. xml:string2stream

        app = New Pdf

        app.BindFO(xml, xsl)

        pdf = New System.IO.FileStream(name + "_xml_string2stream.pdf",

                                        System.IO.FileMode.Create)

        app.Save(pdf)

 

 

        '9. xml:doc2string

        app = New Pdf

        app.BindFO(doc_xml, xsl)

        app.Save(name + "_xml_doc2string.pdf")

 

 

        '10. xml:doc2stream

        app = New Pdf

        app.BindFO(doc_xml, xsl)

        pdf = New System.IO.FileStream(name + "_xml_doc2stream.pdf",

                                        System.IO.FileMode.Create)

        app.Save(pdf)

 

 

        '0. An example of how to set the metadata of your generated pdf document

        'If you do not want to set these tedious metadata of pdf, you can set the

        'second parameter of Save method to be "null" simply, as shown in above.

 

 

        'At first create a IDictionary to contain your metadata.

        Dim metadata As System.Collections.IDictionary = New

                                        System.Collections.Hashtable

 

 

        'Then set the various items.

        'If you don't set them, Aspose.Pdf.Fo will set default value.

        'a. title of your pdf doc:

        metadata.Add("Title", "New Title")

        'b. author of your pdf doc:

        metadata.Add("Author", "New Author")

        'c. subject of your pdf doc:

        metadata.Add("Subject", "New Subject")

        'd. keywords of your pdf doc:

        metadata.Add("Keywords", "New Key Words")

        'e. creator of your pdf doc:

        metadata.Add("Creator", "New Creator")

        'f. producer of your pdf doc:

        metadata.Add("Producer", "New Producer")

 

 

        'Make this IDictionary be the parameter of Save method and

        'start converting.

        app = New Pdf

        app.FoMetaData = metadata

        app.BindFO(fo)

 

 

        app.Save("HelloWorld.pdf")

    End Sub

End Class

 

[Java]

 

Pdf app = new Pdf();

app.bindFo(new FileInputStream(new File("helloworld.fo")));

app.save(new FileOutputStream(new File("Helloworld.pdf")));

                                         

app = new Pdf();

app.bindFo(new FileInputStream(new File("helloworld.xml")),

                            new FileInputStream(new File("helloworld.xsl")));

app.save(new FileOutputStream(new File("Helloworld.pdf")));