Writing a Simple Java Application

Top Previous Topic Next Topic  Print this topic

Using the Ultrascale Java API is very easy. Below, the steps that must be followed in order to create a simple "Hello World" application are described.

 

Step 1: Writing the Source Code

 

The code listed below must be copied into the a text file named HelloWorld.java

 

 

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());

               };

       }

}

 

 

Step 2: Compiling the Code File

 

First, the .jar file must be located. Under a default instalation it should be in C:\Program Files\Ecrion\XF Ultrascale 2013 vX.X.X\Client\Java if the operating system is Windows or in /opt/Ecrion/XFUltrascale/Client/Java if it is Linux.

For this example, the default folder's path will be used. If the .jar file is at a different location, the paths must be changed accordingly.

Also, the Java Development Kit must be installed.

 

A console must be opened and, using the cd command, the location where the HelloWorld.java file is saved must be enabled. Then, the following command must be typed in:

 

 

> javac -cp ".;C:\Program Files\Ecrion\XF Ultrascale 2013 vX.X.X\Client\Java\Ecrion.Ultrascale.Java.jar" HelloWorld.java

 

 

Step 3: Running the Program

 

Running the program is done using the java command.

 

 

> java -cp ".;C:\Program Files\Ecrion\XF Ultrascale 2013 vX.X.X\Client\Java\Ecrion.Ultrascale.Java.jar" HelloWorld

 

 

The last command should have no output. If the user has the proper writing permissions on the C:\ drive, a file named HelloWorld.pdf should appear at that location.

 

This concludes the "Hello World" example. More examples can be found in the Samples folder, that is present after installing XF Rendering Server 2013.

 

Below, a step by step explanation of the code is available.

 

Understanding the Code

 

The engine needs to be passed parameters via a RenderingParameters object.

Note:

·All members have default values and can be left unset, but some of them will be set in this example to show how it is done. To be more specific, the input and output format parameters will be set.

 

 

// create parameters for the rendering operation

RenderingParameters rp = new RenderingParameters();

 

// set input format

rp.InputFormat = Engine.InputFormat.IF_XSLFO;

 

// set output format

rp.OutputFormat = Engine.OutputFormat.OF_PDF;

 

 

The next step is creating the Engine object:

 

 

// create a new engine

Engine eng = new Engine();

 

 

Next, an input XSL-FO stream is needed. In this example, a StringBufferInputStream constructed from a hard-coded string will be used.

 

 

// 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);

 

 

Now, an output stream is created (a FileOutputStream is used in the example) to call the Render() method on the Engine object.

 

 

// create an output stream

OutputStream outputStream = new FileOutputStream("C:\\HelloWorld.pdf");

 

// render

eng.Render(inputStream, outputStream, rp);

 

 

Also, the streams must be closed after rendering:

 

 

// remember to close the streams

inputStream.close();

outputStream.close();