This tutorial offers a .NET example on how XF Web Service can be accessed.
1. A new Console Application Project must be created in Visual Studio 2010. Any name can be set. For example, "ConsoleApplication1". .NET Framework 4 must be selected before continuing.
2. In Solution Explorer, "ConsoleApplication1" must be right-clicked and Add Reference selected.
3. In the Add Reference dialog-box, the .NET tab located at the left must be selected and the System.ServiceModel chosen
4. In Solution Explorer, "ConsoleApplication1" must be right-clicked and Add Service Reference selected.
5. In the Add Service Reference dialog-box, the address can be set to http://localhost/XFWS/ecrion/services/xfws.asmx or an url pointing to any other valid web service. The namespace must be set to “localhost”.
6. Now, the project will look like the following in Solution Explorer:
7. "Program.cs" must be opened and all the existing code lines must be replaced by the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ServiceModel;
using ConsoleApplication1.localhost;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String inputFilePath = @"test.fo";
String outputFilePath = @"output.pdf";
try
{
// Read the XSL-FO file content
string xslfoContent = null;
using (StreamReader reader = new StreamReader(inputFilePath))
xslfoContent = reader.ReadToEnd();
// Initialize data source
XmlDataSource inputDataSource = new XmlDataSource();
// Set input content; the value can be a string or a byte array
inputDataSource.Content = xslfoContent;
// Set input format
inputDataSource.Format = InputFormat.XSLFO;
// Create parameters for the rendering operation.
RenderingParameters renderingParameters = new RenderingParameters();
// Set output format
renderingParameters.OutputFormat = OutputFormat.PDF;
// Initialize web service client
EndpointAddress serviceAddress = new EndpointAddress("http://localhost/XFWS/ecrion/services/xfws.asmx");
xfwsSoapClient webServiceEndpoint = new xfwsSoapClient(new BasicHttpBinding(), serviceAddress);
// Creates print ready documents using Ecrion XF Web Service
byte[] outputBytes = webServiceEndpoint.Render(inputDataSource, renderingParameters);
// Write the output bytes
using (FileStream fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
fs.Write(outputBytes, 0, (int)outputBytes.Length);
}
Console.WriteLine("Rendering finished.");
Console.WriteLine(Path.GetFullPath(outputFilePath) + " has been created.");
}
catch (Exception e)
{
// Report errors
Console.Out.WriteLine(e.Message);
if (e.InnerException != null)
Console.Out.WriteLine(e.InnerException.ToString());
// Delete the (partial) output file if any
if (File.Exists(outputFilePath))
File.Delete(outputFilePath);
}
}
}
}
Example 1: Converting FO to PDF using XF Web Service
Notes:
· | In the above example, any FO file can be used. Below is the code for a sample fo, that must be copied into a .fo file. In this example, the file is named "test.fo". Before running the application, the .fo file must be copied into the ConsoleApplication1\ConsoleApplication1\bin\Debug folder. |
· | To specify a new address for the XF Web Service, the URL in EndpointAddress must be modified. |
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xf="http://www.ecrion.com/xf/1.0">
<fo:layout-master-set>
<fo:simple-page-master master-name="all-pages" page-width="11in" page-height="8.5in">
<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>
Example 2: Sample test.fo
Using XML Input
For XML/XFD input inputDataSource.Format = InputFormat.XSLFO; and parameters must be used. The following code snippet presents a sample template:
ServerDocumentTemplate template = new ServerDocumentTemplate();
template.ID = "Sample:Invoice.xfd";
parameters.Template = template;
inputDataSource.Format = InputFormat.XML;
Example 3: Converting XML to PDF using XF Web Service
Notes:
· | XF Web Service can use both server and local templates to convert XML to PDF. |
· | Local templates must be first installed in the Management Console under XF Rendering Server > Server Templates before using them. Also, the template's ID must be known. In the above example, "Sample:Invoice.xfd" was used, which can be found in XF Rendering Server's sample files after installing it. |