Print Operation of the webPDF ToolboxWebService

Coding Example: Webservice Printing using 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

We would like to introduce the print operation and its use with the webPDF wsclient library by means of code snippets. You should already have created a REST or SOAP session for this purpose and thus either a ToolboxWebService object (for a SOAP session) or a SOAP object by calling the WebserviceFactory:

SOAP:

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

REST:

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

Then you have to pass either a RestDocument or a SoapDocument object to that WebService object by calling the method setDocument(). Further background information on this can be found in our webPDF documentation (e.g. on using the REST interface) or in our blog: e.g. on using the SOAP interface with webPDF wsclient.

All about Webservice Parameter

Since you want to get changing access to a document, you have to give the current open and/or permission password of the document to the webservice call (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.

What is the ToolboxWebService? Example Print/Printing

The ToolboxWebService is an endpoint of your webPDF server. It summarizes a number of operations. With these it is possible to modify your PDF document. One of these operations is the Print Operation. With this operation you can print specific pages of your PDF document. How does it work in concrete terms?

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

PrintType print = new PrintType();
toolboxWebService.getOperation().add(print);

The following parameters can be set for the Print object:

pages (default value: “1”)

Specifies the page area to print. 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 items (“1,3-5,6”). All pages of the document can be selected using “*”.

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

printerName (default value: “”)

Here you can transfer the name of the connected printer on which the document is to be printed. If no name is specified here, the system tries to use the default printer instead.

print.setPrinterName("PrinterName");

autoRotate (default value: true)

If this boolean value is set to true, the orientation of the PDF document is automatically adjusted to the paper orientation of the printer.

print.setAutoRotate(false);

centerInPage (default value: false)

If this boolean value is set to true, PDF pages are centered on the print result.

print.setCenterInPage(true);

copies (default value: 1)

Specifies the number of copies to print.

print.setCopies(2);

expandToMargins (default value: false)

If this value is set to true, pages smaller than the paper size will be enlarged to fill the page.

print.setExpandToMargins(true);

jobName (default value: “”)

This value allows you to set a specific name for the print job that would appear in the printer queue.

print.setJobName("PrintJobName");

shrinkToMargins (default value: false)

If this value is set to true, pages larger than the paper size used will be reduced accordingly to fit on the page.

print.setJobName("PrintJobName");

Example with more details – 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("https://localhost:8080/webPDF/")
    );
    // Make available the document that is 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:
    PrintType print = new PrintType();
    toolboxWebService.getOperation().add(print);

    print.setPages("1,3-5,6");
    print.setPrinterName("PrinterName");
    print.setAutoRotate(false);
    print.setCenterInPage(true);
    print.setCopies(2);
    print.setExpandToMargins(true);
    print.setJobName("PrintJobName");
    print.setShrinkToMargins(true);

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

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