Writing a Simple .NET Application

Top Previous Topic Next Topic  Print this topic

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

 

Step 1: Create a New Microsoft Visual Studio 2005 Project

 

Open Visual Studio 2005 and go to File > New > Project... and select Visual C# > Console Application.

 

Step 2: Add the Ecrion Ultrascale References

 

ultra_references

 

Go to Project > Add Reference... and select Ecrion.Ultrascale.NET in the list as shown in the screenshot above, then click OK.

 

Step 3: Type In The Code

 

Just copy the code listed below into the Program.cs file and hit F5 or Debug > Run and a PDF file named HelloWorld.pdf will be written on your C:\temp folder . Remember to first make sure that C:\temp exists and you have write access on it.

 

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 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. In our example, we will set the most important parameter: output format.

 

 

// create parameters for the rendering operation

RenderingParameters param = new RenderingParameters();

 

// set output format

param.OutputFormat = Engine.OutputFormat.PDF;

 

 

The next step is creating our Engine object:

 

 

// create a new engine

Engine engine = new Engine();

 

 

Next, we'll need an input XSL-FO stream. In our example, we will use a MemoryStream constructed from a hard-coded string. Then, use an XmlDataSource object to pass 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 we'll create an output stream (we'll use a FileStream in this example) and call the Render() method on our Engine object.

 

 

// create output stream from file

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

 

// render

engine.Render(input, outputStream, param);