Convert and merge multiple PDF files using SharePoint Designer workflows
The Muhimbi PDF Converter for SharePoint has been able to convert and merge multiple files (including web pages) into a single PDF file for some time, a facility used by many of our customers. Previously it was possible to use the SharePoint User Interface, our API and the Muhimbi Workflow Power Pack to control this merge process, but as of version 5.1 it is also possible to merge files using both SharePoint Designer and Nintex Workflow.
This post shows how to use SharePoint Designer to create a workflow that automatically adds a cover page to a document whenever it is created or updated. If the document is not already in PDF Format it will convert the file as part of the merging process.
The SharePoint Designer Workflow Activity is named Merge Documents into PDF. After adding it to your workflow you will see the following Workflow Sentence.
The following fields are available:
-
These Documents: This field specifies the list of documents / URLs to merge (and convert if needed). Each file must be specified on a new line and each line may contains three ‘;’ separated values
-
File Path: The path to the file to merge or URL of web page to convert. This may already be a PDF file, but if not – and the file format is supported by the converter – then it will be converted first. The path can be relative to the current site (e.g. Shared Documents/Some File.docx) or absolute (e.g. /sites/Finance/Shared Documents/Some Folder/Some file.docx). SharePoint Designer Workflow Lookups are fully supported, which can be used to dynamically generate the path for the current file, e.g. [%Current Item:URL Path%]. In SharePoint 2010 always use forward slashes in your file path, see KB2529610. When specifying a file please make sure the path does not include the host name, e.g. ‘https://your site/…’. If you wish to convert web pages then the path MUST start with https:// or https://.
-
Include Bookmarks: The source file may already include PDF Bookmarks, or may be able to generate such bookmarks as part of the conversion process, e.g. an MS-Word file. Specify true to copy these bookmarks to the merged PDF file, or false to strip out any bookmarks. This value is optional
-
Custom Bookmark value: The this field parameter (see below for details) can be overridden using the third parameter. Specify the name of the ‘top level bookmark’ for the file, specify “” to remove the bookmark for this document or don’t specify anything at all to use the value stored in the column referenced by the this field parameter. For an example see the exercise below.
-
This File: An optional filename (and path) to write the merged document to. When not specified, the same name as the document that triggered the workflow will be used with a ‘.pdf’ file extension. Please make sure the path does not include the host name, e.g. ‘https://your site/…’., see this post for details.
-
This Field: In a way similar to the User Interface for the merge facility, it is possible to specify a name of the column which contents will be used to populate the PDF Bookmark for the merged document. For example, specifying name or title makes it very easy to jump between the various sections of the merged PDF file.
-
Variable: List ID: If you wish to carry out further actions on the generated PDF file, e.g. perform a check-in, then you can optionally write the ID of the List the PDF was written to in a workflow variable.
-
Variable: List Item ID: Similarly to List ID, the Item ID of the generated PDF file can optionally be written to a workflow variable.
OK, so that is the theory. Let’s create a sample to automatically add a cover page to each document. In this example we will use SharePoint Designer 2010, but the steps for SharePoint Designer 2007 are nearly identical.
-
Download and install the PDF Converter for SharePoint. You will need version 5.1 or newer.
-
If not already installed, download and install the free SharePoint Designer for your environment (2007 or 2010).
-
Make sure you have the appropriate privileges to create workflows on a site collection.
-
Create, or navigate to, the Document Library that will be used for this workflow and add a simple MS-Word file named cover page.docx. This file can really be in any format and if its content is static it is recommend to use a PDF file for this purpose so it doesn’t need to be converted repeatedly for every merge operation.
-
Open SharePoint Designer, open the relevant Site, create a new workflow, associate it with the Document Library used in step #4 and enable all Start Options to make sure the workflow is triggered when an item is created and updated.
-
Add a condition to check that the type property of the current item does not equal pdf. Otherwise the workflow will recursively trigger whenever the merged file is written to the same document library.
-
Add the Merge Documents into PDF workflow Action and click the these documents parameter.
-
Add a line for the location of the cover page, note that our Document Library is named Automatic Merging. There is no need to specify the optional Include Bookmark and Custom Bookmark values, although you can if you wish to.
-
The second file to merge is the one that triggered the workflow. Position the cursor on the 2nd line of the String Builder and click the Add or Change Lookup button. Select Current Item as the Data Source and URL Path as the Field from source. Click OK to close the Lookup for String window. We want to include the source document’s bookmarks so enter ;true at the end of the second line.
Your String Builder should now look similar to the screenshot under step #8. Click OK to accept the changes.
-
In this example we want the (merged) output file to use the same name and path as the source file that triggered the workflow, so there is not need to change the this file parameter.
-
Click the this field parameter and select the field you wish to use for the content of the PDF Bookmarks. In this example we’ll select Name.
-
We are all done, publish the workflow and add an MS-Word file with the content to merge to the Document Library. After a few seconds a PDF file will be created consisting of the cover page and the content. Open the PDF file and use the Bookmark pane to quickly navigate to the start of the 2 documents.
Clavin is a Microsoft Business Applications MVP who supports 1,000+ high-level enterprise customers with challenges related to PDF conversion in combination with SharePoint on-premises Office 365, Azure, Nintex, K2, and Power Platform mostly no-code solutions.