Java Example: Accessing the XF Web Service

Top Previous Topic Next Topic  Print this topic

To import a web service in Eclipse the next steps must be followed:

 

1. A new JAVA project must be created in Eclipse

 

new_java_project

 

2. The source must be right clicked and the New/Other option chosen. Then the Web Service Client must be selected.

 

select_a_wizard

 

3. The service definition (WSDL) url must be set.

For example: http://localhost/XFWS/ecrion/services/xfws.asmx?wsdl

 

Note:

It does not matter if the XF Web Service is hosted in IIS or Tomcat.

 

4. Eclipse will generate proxy classes for the web service. The web service is ready to be used.

 

Here is a sample web service program that will convert a DOCX file to a PDF using the XF Rendering Server Web Service:

 

 

import com.ecrion.www.xfws._2_0.*;

import javax.xml.bind.*;

import java.io.*;

 

public class Program

{

      public static void main(String[] args)

      {

             try

             {

                    // Read input file

                    DataInputStream inStm = new DataInputStream(new FileInputStream("c:\\temp\\test.docx"));

                    byte[] documentBytes = new byte[inStm.available()];

                   

                    inStm.readFully(documentBytes);

                   

                    XfwsLocator locator = new XfwsLocator();

                    XfwsSoap engine = (XfwsSoap)locator.getPort(XfwsSoap.class);

                    XmlDataSource ds = new XmlDataSource();

                   

                    ds.setContent(DatatypeConverter.printBase64Binary(documentBytes));

                    ds.setFormat(InputFormat.DocX);

 

                    RenderingParameters parameters = new RenderingParameters();

                                

                    parameters.setOutputFormat(OutputFormat.PDF);

                   

                    byte[] result = engine.render(ds, parameters);

                   

                    DataOutputStream outStm = new DataOutputStream(new FileOutputStream("c:\\temp\\test.pdf"));

                   

                    outStm.write(result);

                   

                    System.out.println("Finished");

             }

             catch (Exception e)

             {

                    e.printStackTrace();

             }

      }

}

 

Example 1: Converting DOCX to PDF using XF Web Service

 

Notes:

ds.setFormat(InputFormat.XML) - is used for XML/XFD input
parameters.setTemplate() - is used for specifying the template

 

 

ServerDocumentTemplate template = new ServerDocumentTemplate();

                   

template.setID("Sample:Invoice.xfd");

parameters.setTemplate(template);

 

Example 2: 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.