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.
- Converting Office files to PDF Format using a Web Services based interface (C# / .NET).
- Converting Office files to PDF Format using a Web Services based interface (Java).
- Invoking the PDF Converter Web Service from Visual Studio 2005 using VB.net
- Using Windows Azure to convert documents to PDF format.
- Using the awesome new watermarking features of the Muhimbi PDF Converter Services (C# / .NET).
- Using the PDF Watermarking features from Java based environments.
Key Features
The key features of the new merging facilities are as follows:
- Convert and merge any supported file format (inc. HTML, AutoCAD, MS-Office, InfoPath, TIFF, MSG) or merge existing PDF files.
- Apply different watermarks on each individual file as well as on the entire merged file (e.g. page numbering).
- Apply PDF Security settings and restrictions on the merged file.
- Optionally skip (and report) corrupt / unsupported files.
- Add PDF Bookmarks for each converted file.
- 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.
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:
- The JDK has been installed and configured.
- The Conversion Service and all prerequisites have been installed in line with the Administration Guide.
- 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