Writing a Simple Java Application

Top Previous Topic Next Topic  Print this topic

Using the Ultrascale Java API is very easy. Below we will describe the steps needed to create a simple "Hello World" application.

 

Step 1: Write the Source Code

 

Copy the code listed below 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: Compile the Code File

 

First locate our .jar file. It should be in C:\Program Files\Ecrion\XF Ultrascale 2010\Java under a default installation. We'll assume that this is the case and provide the console commands under this assumption. If you have the .jar under a different path you can adapt the commands to use that location. Also make sure that you have the Java Development Kit installed. To check, search for a directory under C:\Program Files\Java that starts with the string "jdk".

 

Open a console, navigate using the cd command to the location where you saved your HelloWorld.java file and type the following:

 

 

> javac -cp ".;C:\Program Files\Ecrion\XF Ultrascale 2010\Java\Ecrion.Ultrascale.Java.jar" HelloWorld.java

 

 

Step 3: Run the Program

 

Running the program is done using the java command.

 

 

> java -cp ".;C:\Program Files\Ecrion\XF Ultrascale 2010\Java\Ecrion.Ultrascale.Java.jar" HelloWorld

 

 

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

 

This concludes our "Hello World" example. More examples can be found in your Samples folder. Read on below if you want a step by step explanation of the code.

 

Understanding the Code

 

Our engine needs to be passed some parameters via a RenderingParameters object. Note that all members have default values and can be left unset, but we'll be setting a few of them here to show you how it's done, namely the two most important parameters: the input and output format.

 

 

// 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 our Engine object:

 

 

// create a new engine

Engine eng = new Engine();

 

 

Next, we'll need an input XSL-FO stream. In our example, we will use a StringBufferInputStream constructed from a hard-coded string.

 

 

// 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 we'll create an output stream (we'll use a FileOutputStream in this example) and call the Render() method on our Engine object.

 

 

// create an output stream

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

 

// render

eng.Render(inputStream, outputStream, rp);

 

 

Also, remember to close the streams after rendering:

 

 

// remember to close the streams

inputStream.close();

outputStream.close();