How to use the webservices of webPDF 7 with options

This example shows how to pass options to the webservices of webPDF. In addition to the well-known “Converter” webservice, a second webPDF webservice called “Toolbox” is used in this example.  This example is based on our text “How to use the web services of webPDF 7“. If you don’t know this yet, it is recommended to start with this text. The software requirements don’t change compared to the first example.

Creating the project and generating the necessary proxy classes

As described in the first example text, create a Java project in IntelliJ with the following options.

Template Command Line App
Project name OptionExample
Project Location …/OptionExample
Base package de.webpdf

Open the project view in IntelliJ. Open the command prompt in the “src” folder and generate the proxy classes with the following commands.

wsimport -Xnocompile -s . -p de.webpdf.schema.converter  
http://localhost:8080/webPDF/soap/converter?wsdl   -extension
wsimport -Xnocompile -s . -p de.webpdf.schema.toolbox  
http://localhost:8080/webPDF/soap/toolbox?wsdl   -extension

Remark:

 The “-p” argument determines the target packet for the generated classes. This is necessary because the classes in the same package would conflict.

The project is created and the proxy classes are generated.

Conversion of webservice requests to webPDF webservices “Converter” and “Toolbox

As in the first text example, the entire code is inserted into the “main” method of the “Main” class again.

how-to-webservices-options-webpdf-7_1001

Now start creating the method content.

URL converterUrl;
URL toolboxUrl;

try {
    //URLs for converter and toolbox service wsdl
    converterUrl = new URL("http://localhost:8080/webPDF/soap/converter?wsdl");
    toolboxUrl = new URL("http://localhost:8080/webPDF/soap/toolbox?wsdl");
} catch (MalformedURLException ex) {
    System.err.println(ex.getMessage());
    return;
}

ConverterService converterService=new ConverterService(converterUrl);
Converter converter=converterService.getConverterPort();

de.webpdf.schema.converter.Operation converterOperation=new de.webpdf.schema.converter.Operation();
converterOperation.setConverter(new ConverterType());

//Page settings for conversion
converterOperation.getConverter().setPage(new PageType());
converterOperation.getConverter().getPage().setWidth(150);
//PDFA conversion settings
converterOperation.getConverter().setPdfa(new PdfaType());
converterOperation.getConverter().getPdfa().setConvert(new PdfaType.Convert());
converterOperation.getConverter().getPdfa().getConvert().setLevel("3b");

DataHandler inputHandler=new DataHandler(
        new FileDataSource(
                new File("src/de/webpdf/schema/toolbox/Toolbox.java")));
DataHandler resultHandler;

try {
    resultHandler = converter.execute(converterOperation, inputHandler, null);
    try (FileOutputStream fos = new FileOutputStream(new File("./ConverterResult.pdf"))) {
        resultHandler.writeTo(fos);
    }
} catch (de.webpdf.schema.converter.WebserviceException | IOException ex) {
    System.err.println(ex.getMessage());
    return;
}

Not much needs to be said about this code, as it largely matches the example from the last blog. Only the file to convert has been changed and the operation instance has been filled with more options.

Adding a PdfaType object to the operation( converterOperation.getConverter().setPdfa(new PdfaType());) causes the document to be converted to PDF/A during the conversion process. The page settings are adjusted by adding the PageType object.

All required parameters can be found in the webPDF user manual under “Programming”->”Webservice Parameters”. In addition an additional URL was created which will be used to generate the Toolbox Endpoint.

Remark:

There is also a PDF/A webservice for webPDF. However, the “Converter” webservice does not take the detour via this, but the conversion to PDF/A format is immediately initiated within webPDF.

Starting with the following section in the code, the “Toolbox” webservice is now also used.

resultHandler=null;

ToolboxService toolboxService=new ToolboxService(toolboxUrl);
Toolbox toolbox=toolboxService.getToolboxPort();

de.webpdf.schema.toolbox.Operation toolboxOperation=new de.webpdf.schema.toolbox.Operation();
//Rotation Settings for Toolbox Operation
RotateType rotateType=new RotateType();
rotateType.setDegrees(180);
//In this way, several more types could be added to the operation
toolboxOperation.getAnnotationOrAttachmentOrDelete().add(rotateType);

try {
    resultHandler = toolbox.execute(toolboxOperation, null,
            new File("./ConverterResult.pdf").toURI().toURL().toString());
    try (FileOutputStream fos = new FileOutputStream(new Fi-le("./ToolboxResult.pdf"))) {
        resultHandler.writeTo(fos);
    }
} catch (de.webpdf.schema.toolbox.WebserviceException | IOException ex) {
    System.err.println(ex.getMessage());
}

The already created URL initializes the service (here the ToolboxService). The service can be used to query the endpoint with a getter method (here “getToolboxPort()”). The endpoint is the final communication interface for you, just like the converter webservice.

The datahandler resultHandler is set to zero and reinitialized so that it no longer refers to the already generated result of the converter. The operation for Toolbox has as base element a list which you can access with “toolboxOperation.getAnnotationOrAttachmentOrDelete()”. You can insert all toolbox types described in the documentation (AnnotationType,AttachmentType,…).

In this example, this is done with a RotateType. In this type, a rotation angle of 180° is set before the transfer to the operation. However, several other type elements could have been added to the list (all of which again have various adjustment options). The execution is very similar to that of the converter service with the difference that the “execute” method is given a URL instead of a data handler.

After executing the file, there should be 2 PDF files in your project folder.

how-to-webservices-options-webpdf-7_3001

  1. the result of the conversion, 2. the result of the processing with the “Toolbox” web service

By using the webservice options, you have converted a JAVA file with the “Converter” webservice into a 150mm wide PDF document (which corresponds to the PDF/A standard) and then rotated it 180° with the “Toolbox” webservice. Congratulations!

attachment:
Required imports for the class:

import de.webpdf.schema.converter.Converter;
import de.webpdf.schema.converter.ConverterService;
import de.webpdf.schema.converter.ConverterType;
import de.webpdf.schema.converter.PageType;
import de.webpdf.schema.converter.PdfaType;
import de.webpdf.schema.toolbox.RotateType;
import de.webpdf.schema.toolbox.Toolbox;
import de.webpdf.schema.toolbox.ToolboxService;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

Continue with part 3 of our technology series Webservices here…