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 use C# and .NET, but the web services based interface is identical when used from Java ( See this generic PDF Conversion sample)
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.
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) 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.
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.
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 include security and watermarking related settings. A Results object is returned that, when it comes to merging of files, always contains a single file that holds the byte array for the merged PDF file.
Simple example code
The following sample describes the steps needed to convert all files in a directory, merge the results into a single file and apply page numbering to the merged file using the built in watermarking engine. We are using Visual Studio and C#, but any environment that can invoke web services should be able to access this functionality. Note that the WSDL can be found at http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl. A generic PDF Conversion Java based example is installed alongside the product and discussed in the User & Developer Guide.
- Start a new Visual Studio project and create the project type of your choice. In this example we are using a standard .net 3.0 project of type Windows Forms Application. Name it ‘Simple PDF Converter Sample’.
- Add a TextBox and Button control to the form. Accept the default names of textBox1 and button1.
- In the Solution Explorer window, right-click References and select Add Service Reference. (Do not use web references!)
- In the Address box enter the WSDL address listed in the introduction of this section. If the Conversion Service is located on a different machine then substitute localhost with the server’s name.
- Accept the default Namespace of ServiceReference1 and click the OK button to generate the proxy classes.
- Double click Button1 and replace the content of the entire code file with the following:
Providing the project and all controls are named as per the steps above, it should compile without errors. Run it, enter the full path to a folder that holds a couple of text files (PDF, Word, Excel, etc) and click the button to start the convert and merge process. The operation may take a while depending on the number and complexity of files in the folder.
Note that In this example we are programmatically configuring the WCF Bindings and End Points. If you wish you can use a declarative approach using the config file.
A more complex and full featured sample application is installed, with full source code, alongside the Conversion Service.
This new functionality is available as of version 5.0 of our software.