Rotate pages with the wsclient library

Minimum technical requirements

  • Java version: 7
  • webPDF version: 7
  • wsclient version: 1

In this article, we introduce the Rotate operation of the Toolbox web service from webPDF and show 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.

Before you start...

To call the web service in the way shown here, it is assumed that you have already created a REST or SOAP session. Via WebServiceFactory, you can then create either a ToolboxWebService object for SOAP or a ToolboxRestWebService object for REST.

SOAP:

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

REST:

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

In addition, a RestDocument or SoapDocument must be passed to the web service object via setDocument().

Further documentation:

Webservice parameters

For write access to a document, it may be necessary to provide the current open and/or permission password of the document to the web service call. This can be done directly on 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 web service is an endpoint of your webPDF server that provides operations for directly modifying PDF documents. One of these operations is Rotate.

The Rotate operation allows you to rotate pages in your PDF document.

You add a Rotate operation to your web service object as follows:

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

The following parameters can be set on the rotate object:

pages (default value: "1")

Specifies the page range to rotate. You can specify 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")

Defines the desired page rotation in degrees. Pages are always rotated in multiples of 90 degrees. Values that do not match a 90-degree step are normalized accordingly.

rotate.setDegrees(180);

pageGroup (default value: "all")

Restricts the selected pages according to their page number. The following values are available:

  • 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 selected pages according to their orientation. The following values are available:

  • 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

Below is a more detailed example of the full web service call for the SOAP interface:

try (
SoapSession session = SessionFactory.createInstance(
WebServiceProtocol.SOAP,
new URL("https://localhost:8080/webPDF/")
);
SoapDocument soapDocument = new SoapDocument(
new File("Path of the source document").toURI(),
new File("Path of the target document")
)
) {
ToolboxWebService toolboxWebService =
WebServiceFactory.createInstance(session, WebServiceType.TOOLBOX);

toolboxWebService.setDocument(soapDocument);

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

toolboxWebService.getOperation().add(rotate);
} catch (ResultException | MalformedURLException ex) {
// error handling
}

The parameters are also documented in the user manual, although without examples for use with the wsclient library: Rotate parameter structure

A documentation of error codes and possible errors can be found here.

Please also note: many parameters are already preset with default values. If a default value matches your intended behavior, the parameter does not have to be set explicitly.

More coding examples for web services that you can use with the wsclient library can be found here.