Editing XMP Metadata: our coding example

Minimum technical requirements

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

How to edit XMP metadata 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

To call the webservice, you should first have created a REST or SOAP session and then create a ToolboxWebService object (for a SOAP session) by calling the WebserviceFactory or create a ToolboxRestWebService object (for a REST session):

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

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

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

More about this Webservice parameter

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. You can do this directly at the created ToolboxWebService object. If the document does not have an appropriate password protection, you can skip this point.

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

XMP Operation of the ToolboxWebService

Now we want to introduce the Xmp operation of the webPDF ToolboxWebservices and how you can use it with the webPDF wsclient library.

The ToolboxWebservice 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 Xmp operation. The Xmp operation allows you to add XMP metadata to a document.

To add an Xmp operation to your WebService object:

XmpType xmp = new XmpType();
toolboxWebService.getOperation().add(xmp);

The following parameters can be set for the xmp object:

dataFormat (default value: “json”)

Format of the XMP metadata to be added. The following values can be set here:

  • json = json
xmp.setDataFormat(XmpDataFormatType.JSON);

The “data” object

To import the XMP data and configure its import, add an XmpFileDataType object to the Xmp object.

XmpFileDataType data = new XmpFileDataType();
xmp.setData(data);

You can read about the form and structure of the XMP metadata in this section of the User’s Guide.

The following parameters can be set for the Data object:

source

Specifies the source from which the XMP data is to be obtained. The following values can be set here:

  • value = The data is passed directly as byte array.
  • uri = A URI reference to the data is passed.
data.setSource(FileDataSourceType.VALUE);

value

Passes the XMP data in the form of a byte array. (source should be set to value.)

data.setValue(FileUtils.readFileToByteArray(new File("…")));

uri

Passes a URI reference to the XMP data to be used. (source should be set to uri.)

data.setUri(new File("...").toURI().toString());

The object “namespace”

To restrict the namespace of the XMP data, you can add an XmpNamespaceType object to the Xmp object.

XmpNamespaceType namespace = new XmpNamespaceType();
xmp.setNamespace(namespace);

The following parameters can be set for the Namespace object:

namespaceName (default value: “”)

Sets a namespace for the XMP data.

namespace.setNamespaceName("webPDF XMP Properties");

namespacePrefix (default value: “”)

Sets a prefix for the namespace of the XMP data.

namespace.setNamespacePrefix("webPDF");

namespacePrefix (default value: “”)

Sets the URI for the namespace of the XMP data.

namespace.setNamespaceURI("http://ns.webpdf.de/webpdf/7.0/");

More detailed example here:

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 webservices via a factory:
    ToolboxWebService toolboxWebService =
        WebServiceFactory.createInstance(
            session, WebServiceType.TOOLBOX
        );
    toolboxWebService.setDocument(soapDocument);
    toolboxWebService.getPassword().setOpen("password");
    toolboxWebService.getPassword().setPermission("password");

    XmpType xmp = new XmpType();
    toolboxWebService.getOperation().add(xmp);

    xmp.setDataFormat(XmpDataFormatType.JSON);

    XmpNamespaceType namespace = new XmpNamespaceType();
    xmp.setNamespace(namespace);

    namespace.setNamespaceName("webPDF XMP Properties");
    namespace.setNamespacePrefix("webPDF");
    namespace.setNamespaceURI("http://ns.webpdf.de/webpdf/7.0/");

    XmpFileDataType data = new XmpFileDataType();
    xmp.setData(data);

    data.setSource(FileDataSourceType.VALUE);
    data.setValue(FileUtils.readFileToByteArray(new File("…")));

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

Further link tips:

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