Print Operation of the webPDF ToolboxWebService

Minimum Technical Requirements

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

Printing via Webservice with the webPDF wsclient Library

Important Note

The following code example is based on the use of the webPDF wsclient library. To understand and apply the example correctly, you should first read the relevant introductory blog post.

Here we would like to introduce the Print operation and its use with the webPDF wsclient library by means of code snippets.

For this purpose, you should already have created a REST or SOAP session and thus be able to create either a ToolboxWebService object for a SOAP session:

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

Or a ToolboxRestWebService object for a REST session:

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

After that, you must pass either a RestDocument or a SoapDocument object to that WebService object by calling the setDocument() method. Further background information can be found both in our webPDF documentation, for example on using the REST interface, and in our blog, for example on using the SOAP interface via webPDF wsclient.

All About the Webservice Parameter

Because you want to obtain write access to a document, you must pass the current open and/or permission password of the document to the webservice call on the created ToolboxWebService object:

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

If the document is not password-protected, you can skip this step.

What Is the ToolboxWebService? Example: Print

The ToolboxWebService is an endpoint of your webPDF server. It combines a range of operations that make it possible to modify your PDF document. One of these operations is the Print operation. With this operation, you can print selected pages of your PDF document in a targeted way. How does that work in practice?

Adding a Print Operation to the WebService Object

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

The following parameters can be set on the Print object.

pages (default value: "1")

Defines the page range to be printed. 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 with "*".

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

printerName (default value: "")

Here you can pass the name of the connected printer on which the document should be printed. If no name is passed, the system will attempt 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 configured paper orientation of the printer.

print.setAutoRotate(false);

centerInPage (default value: false)

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

print.setCenterInPage(true);

copies (default value: 1)

Defines the number of copies to print.

print.setCopies(2);

expandToMargins (default value: false)

If this value is set to true, pages that are smaller than the selected paper size are enlarged so that they fill the page.

print.setExpandToMargins(true);

jobName (default value: "")

This value lets you assign a specific name to the print job, which would then appear in the print queue.

print.setJobName("PrintJobName");

shrinkToMargins (default value: false)

If this value is set to true, pages that are larger than the selected paper size are reduced so that they fit on the page.

print.setShrinkToMargins(true);

Example with More Details: Webservice Call for the SOAP Interface

try (
// Set up a session with the webPDF server, here using SOAP.
SoapSession session = SessionFactory.createInstance(
WebServiceProtocol.SOAP,
new URL("https://localhost:8080/webPDF/")
);
// Provide the document to be processed and the target file.
SoapDocument soapDocument = new SoapDocument(
new File("Path of the source document").toURI(),
new File("Path of the target document")
)
) catch (ResultException | MalformedURLException ex)

More code examples for webservices that you can use with the wsclient library can be found here.