How to use Java to Combine Multiple PDF Files

Related Products

PDF Converter

PDF Converter

Share

To facilitate the new PDF Merging facility in our PDF Converter for SharePoint we have added the ability to convert and merge multiple files to our core PDF Conversion engine, which our SharePoint product shares with our generic Java / .NET oriented PDF Converter Services.

In this post we’ll describe in detail how to invoke this new merging facility from your own code. This demo uses Java, but the web services based interface is identical when used from .NET ( See the .NET version of this same article).

This post is part of the following series related to manipulating PDF files using web services.

Key Features

The key features of the new merging facilities are as follows:

  1. Convert and merge any supported file format (inc. HTML, AutoCAD, MS-Office, InfoPath, TIFF, MSG) or merge existing PDF files.
  2. Apply different watermarks on each individual file as well as on the entire merged file (e.g. page numbering).
  3. Apply PDF Security settings and restrictions on the merged file.
  4. Optionally skip (and report) corrupt / unsupported files.
  5. Add PDF Bookmarks for each converted file.
  6. Apply any ConversionSetting supported by the regular conversion process.

Object Model

The object model is relatively straight forward. The classes related to PDF Merging are displayed below. A number of enumerations are used as well by the various classes, these can be found in our original post about Converting files using the Web Services interface. A detailed Developer Guide is available here.

ClassDiagram-Merging_thumb13

The Web Service method that controls merging of files is called ProcessBatch (highlighted in the screenshot above). It accepts a ProcessingOptions object that holds all information about the source files to convert and the MergeSettings to apply, which may optionally include security and watermarking related settings. A Results object is returned that, when it comes to merging of files, always contains a single file in element 0 that holds the byte array for the merged PDF file.

Sample code

The following sample merges all files specified on the command line into a single PDF. If the source files are not already in PDF format then it automatically converts them in the process. A PDF bookmark is automatically generated for each merged file as well.

The example described below assumes the following:

  1. The JDK has been installed and configured.
  2. The Conversion Service and all prerequisites have been installed in line with the Administration Guide.
  3. The Conversion Service is running in the default anonymous mode. This is not an absolute requirement, but it makes initial experimentation much easier.

The first step is to generate proxy classes for the web service by executing the following command:

Feel free to change the package name and destination directory to something more suitable for your organisation.

Wsimport automatically generates the Java class names. Unfortunately some of the generated names are rather long and ugly so you may want to consider renaming some, particularly the Exception classes, to something friendlier. This, however, means that if you ever run wsimport again you will need to re-apply those changes. For more information have a look at the high level overview of the Object Model exposed by the web service.

Once the proxy classes have been created add the following sample code to your project. Run the code and make sure the files to merge are specified on the command line.

As of version 5.2 this sample code is automatically installed alongside the product. The source code, including pre-generated proxy classes for the web service, can be downloaded here.

Labels: Articles, Java, Merging, News, pdf, PDF Converter, PDF Converter Services

Have a Question?
We’re Always Happy to Help.

© Muhimbi Ltd. 2008 - 2024
This website uses cookies to ensure you get the best experience. Learn more