Writing a Simple .NET Application

Top Previous Topic Next Topic  Print this topic

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

 

Step 1: Creating a New Microsoft Visual Studio 2005 Project

 

First, Visual Studio 2005 must be opened. Then, the user must access File > New > Project... and select Visual C# > Console Application.

 

Step 2: The Ecrion Ultrascale References must be added

 

ultra_references

 

To add a reference, the user must acccess Project > Add Reference... and select Ecrion.Ultrascale.NET from the list (as shown in the screenshot above).

 

Step 3: Type In The Code

 

The code listed below must be copied into the Program.cs file. Then, the F5 key must be pressed or the Debug > Run option selected. As a result, a PDF file named "HelloWorld.pdf" will be written in the user's C:\temp folder.

Note:

·The user must make sure that the C:\temp folder exists and writing access is granted for the mentioned folder.

 

 

using System;

using System.IO;

using Ecrion.Ultrascale;

 

namespace HelloWorld

{  

  class Program

   {

       [STAThread]

      static void Main(string[] args)

       {

          String outFilePath = @"C:\temp\HelloWorld.pdf";

          FileStream outputStream = null;

 

          try

           {

              //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 stream

              Stream inputStream = new MemoryStream(stringBytes);

 

              // Initialize data source

              IDataSource input = new XmlDataSource(inputStream, Engine.InputFormat.XSLFO);

 

 

              // Create output stream

               outputStream = new FileStream(outFilePath, FileMode.Create);

             

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

 

               engine.Render(input, outputStream, param);

 

              Console.WriteLine("Document rendered successfully!\n");

           }

          catch (Exception e)

           {

              // Report errors

              Console.Out.WriteLine(e.Message);

              if (e.InnerException != null)

                  Console.Out.WriteLine(e.InnerException.ToString());

 

              // Close and delete the (partial) output file if any

              if (outputStream != null)

               {

                   outputStream.Close();

                   outputStream = null;

               }

              if (File.Exists(outFilePath))

                  File.Delete(outFilePath);

           }

       }

   }

}

 

 

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. In this example, the most important parameter, which is the output format, is set.

 

 

// create parameters for the rendering operation

RenderingParameters param = new RenderingParameters();

 

// set output format

param.OutputFormat = Engine.OutputFormat.PDF;

 

 

The next step is creating the Engine object:

 

 

// create a new engine

Engine engine = new Engine();

 

 

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

Then, an XmlDataSource object will be utilized to pass it to the 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 string bytes

byte[] stringBytes = System.Text.Encoding.UTF8.GetBytes(xml);

 

// create input stream from string bytes

Stream inputStream = new MemoryStream(stringBytes);

// Initialize data source

IDataSource input = new XmlDataSource(inputStream, Engine.InputFormat.XSLFO);

 

 

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

 

 

// create output stream from file

Stream outputStream = new FileStream("C:\\temp\\HelloWorld.pdf", FileMode.Create);

 

// render

engine.Render(input, outputStream, param);