Rotate pages with the wsclient library

In this article we would like to introduce the rotate operation of the toolbox webserices from webPDF and show you how you can use it with the webPDF wsclient library.

Important note:

The following coding example is based on the use of the webPDF wsclient library. In order to understand and apply the example, the following blog post should be considered first:

webPDF and Java: very easy with the “wsclient” library

Before you start…

In order to be able to call the webservice in the way we would like to present here, it is assumed that you have already created a REST or SOAP session and can therefore either create a toolboxWebservice object (for a SOAP session) by calling the WebServiceFactory:

ToolboxRestWebService toolboxWebService =
    WebServiceFactory.createInstance(
        session, WebServiceType.TOOLBOX
    );

Or create a ToolboxRestWebservice object (for a REST session):

ToolboxRestWebService toolboxWebService =
    WebServiceFactory.createInstance(
        session, WebServiceType.TOOLBOX
    );

And passed either a RestDocument or a SoapDocument object to this WebService object by calling the setDocument() method.

An article explaining how to use the REST-interface can be found here:

Use of the REST interface via webPDF wsclient

An article explaining how to use the SOAP-interface can be found here:

Use of the SOAP interface via webPDF wsclient

Webservice Parameters

Especially to get changing access to a document, it is often necessary to give the current open and/or permission password of the document to the webservice call. You can do this directly at the created toolboxWebService object:

toolboxWebService.getPassword().setOpen("password");
toolboxWebService.getPassword().setPermission("password");

If the document does not have an appropriate password protection, you can skip this point.

The Toolbox Webservice…

The Toolbox Webservice is an endpoint of your webPDF server that summarizes a number of operations with which you can directly manipulate your PDF document. One of these operations is the rotate operation, which we would like to discuss here.

The rotate operation allows you to rotate pages of your PDF document.

You add a rotate operation to your WebService object as follows:

RotateType rotate = new RotateType();
toolboxWebService.getOperation().add(rotate);

The following parameters can be set for the rotate object:

pages (default value: „1“)

Specifies the page range to rotate. You can specify either a single page („1“), a list of pages („1,3,5“), a page range („1-5“), or a combination of these elements („1,3-5,6“). All pages of the document can be selected using „*“.

rotate.setPages("1,3-5,6");

degrees (default value: „90“)

The desired rotation of the pages in degrees. Sides always have a rotation corresponding to a multiple of 90 degrees. Values that do not correspond to a 90 degree step are normalized to such a step.

rotate.setDegrees(180);

pageGroup (default value: „all“)

Restricts the pages in the selected selection according to their page number. The following values can be set here:

  • all = All selected pages are rotated.
  • even = Only selected pages with an even page number are rotated.
  • odd = Only selected pages with an odd page number are rotated.
rotate.setPageGroup(PageGroupType.ODD);

pageOrientation (default value: „any“)

Restricts the pages in the selected selection according to their orientation. The following values can be set here:

  • all = All selected pages are rotated.
  • portrait = Only selected pages in portrait format are rotated.
  • landscape = Only selected pages in landscape format are rotated.
rotate.setPageOrientation(PageOrientationType.PORTRAIT);

More detailed example

Finally a somewhat more detailed example for our entire webservice call (for addressing the SOAP interface):

try (
    // Setup of a session with the webPDF server (here SOAP):
    SoapSession session = SessionFactory.createInstance(
        WebServiceProtocol.SOAP,
        new URL("http://localhost:8080/webPDF/")
    );
    // Make available the document to be processed
    // and the file in which the result is to be written:
    SoapDocument soapDocument = new SoapDocument(
        new File("Path of the source document").toURI(),
        new File("Path of the target document")
    )
) {
    // Selection of the webservice via a factory:
    ToolboxWebService toolboxWebService =
        WebServiceFactory.createInstance(
            session, WebServiceType.TOOLBOX
        );
    toolboxWebService.setDocument(soapDocument);
    toolboxWebService.getPassword().setOpen("password");
    toolboxWebService.getPassword().setPermission("password");

    // Object oriented parameterization of the call:
    RotateType rotate = new RotateType();
    toolboxWebService.getOperation().add(rotate);

    rotate.setPages("1,3-5,6");
    rotate.setDegrees(180);
    rotate.setPageGroup(PageGroupType.ODD);
    rotate.setPageOrientation(PageOrientationType.PORTRAIT);

    // Execution.
    toolboxWebService.process();
} catch (ResultException | MalformedURLException ex) {
    // To evaluate possible errors that may have occurred, the
    // wsclient library appropriate methods are available:
}

The parameters themselves can also be found documented in our user manual, but of course there without examples for use with the wsclient library: rotate parameter structure

A documentation of the error-codes and possibly occurring errors can be found here.

Please also note: All parameters are preset with certain default values. If a default value is specified and does not deviate from your desired value, it is not absolutely necessary to set this parameter.

More coding examples for webservices that you can use with the ws-client library can be found here.