Subscribe to News feed

Combining multiple PDF files using a SharePoint Designer Workflow

Posted at: 11:23 PM on 26 May 2010 by Nycklander

workflow Hi, I’m Nicolas and today I will show how to merge multiple PDF files using a SharePoint Designer workflow using the PDF Converter for SharePoint and the Workflow Power Pack.

Update: Please note that as of version 5.0 of the PDF Converter it is also possible to merge PDF Files using the SharePoint User Interface as well as via direct Web Service calls.

A quick introduction for those not familiar with the product: The Muhimbi Workflow Power Pack for SharePoint allows custom C# or VB.NET code to be embedded in SharePoint Designer Workflows without the need to resort to complex Visual Studio based workflows, the development of bespoke Workflow Activities or long development cycles.

The solution presented below shows how to create a SharePoint Designer workflow and attach it to a document library. This workflow is triggered when adding or modifying an item in the library, for example after converting a document using the PDF Converter. This example is particularly useful when you want to automatically add a cover page or Appendix to each and every PDF file in the system.

The workflow checks if the file extension for the current item is “pdf”. If this is the case then it uses the Workflow Power Pack to execute some C# code that carries out the actual appending of PDF files. The solution provided in this post works in SharePoint 2007 as well as 2010.

Append PDF content from one file to another

Create the workflow as follows:

  1. Download and install the Muhimbi Workflow Power Pack for SharePoint
     
  2. Download and install the Muhimbi PDF Converter for SharePoint 
     
  3. We need to be able to access functionality in the Muhimbi.SharePoint.DocumentConverter.PDF assembly. Add this reference to the relevant Web Application using the Workflow Power Pack Central Administration screens as described in the Administration Guide.
      
  4. Make sure you have the appropriate privileges to create workflows on a site collection.
     
  5. Create a new workflow using SharePoint Designer.
     
  6. On the Workflow definition screen associate the workflow with the Shared Documents library, tick the boxes next to both “Automatically start….” options and proceed to the next screen.
     
  7. We only want to act on files of type PDF. Although we could have put this validation in the code, in this example we use a workflow condition for it so add a Compare Any Data Source condition and:
     
    a. Click on the first value followed by the display data binding (fx) button.
    b. Select Current Item as the Source and select File Type in the Field. Click the OK button to continue.
    c. Click on the second value and enter pdf. (Use lower case as the compare option is case sensitive).
     
  8. Click the Actions button and insert the Execute Custom Code action.
     
  9. Click parameter 1 and enter a relative or absolute path to the PDF file you want to append to the current workflow item. For example /sites/PDFConversion/Shared%20Documents/appendix.pdf  or Shared%20Documents/appendix.pdf
     
  10. Optionally, click parameter 2 to specify a second PDF file to append.
     
  11. Insert the C# based code listed below by clicking this code in the workflow designer.  Note that copying this code using Internet Explorer may remove line breaks. Preferably use Chrome or Firefox to copy the code or alternatively paste it from Internet Explorer into Windows Wordpad and then copy it from there into SharePoint Designer.
     
    /*********************************************************************************************
                         Muhimbi PDF Converter - Combining Multiple PDF Files
     
                  Copyright 2010, Muhimbi Ltd - www.muhimbi.com - All rights reserved
     
    The following code shows a simple way to merge PDF content from one or more files.
     
    Error and permission checking as well as other minor features have been omitted for the sake
    of brevity and clarity.
     
    This code requires Muhimbi’s PDF Converter and Workflow Power Pack to be installed.
    **********************************************************************************************/
     
    using Syncfusion.Pdf.Parsing;
    using System.IO;
     
    // ** Some variables we're going to use
    SPFile spDocument1ToAppend = null;
    SPFile spDocument2ToAppend = null;
    PdfLoadedDocument document1ToAppend = null;
    PdfLoadedDocument document2ToAppend = null;
     
    // ** Get and load current pdf document (the one which triggered the workflow)
    SPFile spSourceDocument = MyWorkflow.Item.File;
    PdfLoadedDocument sourcePdfDocument = new PdfLoadedDocument(spSourceDocument.OpenBinary());
     
    // ** If supplied, get and load 1st pdf document to append to
    string document1ToAppendPath = MyWorkflow.Parameter1 as string;
    if (!string.IsNullOrEmpty(document1ToAppendPath))
    {
        spDocument1ToAppend = MyWorkflow.Web.GetFile(document1ToAppendPath);
        document1ToAppend = new PdfLoadedDocument(spDocument1ToAppend.OpenBinary());
    }
     
    // ** If supplied, get and load 2nd pdf document to append to
    string document2ToAppendPath = MyWorkflow.Parameter2 as string;
    if (!string.IsNullOrEmpty(document2ToAppendPath))
    {
        spDocument2ToAppend = MyWorkflow.Web.GetFile(document2ToAppendPath);
        document2ToAppend = new PdfLoadedDocument(spDocument2ToAppend.OpenBinary());
    }
     
    // ** Get destination file and folder
    string destinationFolderUrl = spSourceDocument.ParentFolder.Url;
    SPFolder spDestinationFolder = MyWorkflow.Web.GetFolder(destinationFolderUrl);
    string destinationFileName = spSourceDocument.Name;
    string destinationFilePath = string.Format("{0}/{1}", destinationFolderUrl,
                                 destinationFileName);
    SPWeb spDestinationWeb = spDestinationFolder.ParentWeb;
    SPFile spDestinationFile = spDestinationWeb.GetFile(destinationFilePath);
     
    // ** If a document library requires manual checkout and the file is not checked out,
    // ** then check the file out before uploading
    if (spDestinationFile.Exists && spDestinationFile.Item.ParentList.ForceCheckout &&
        spDestinationFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)
    {
        spDestinationFile.CheckOut();
    }
     
    // ** Add the file to the site including the meta data
    using (MemoryStream mergedDocument = new MemoryStream())
    {
        // ** Append files to destination document
        if (document1ToAppend != null)
            sourcePdfDocument.Append(document1ToAppend);
     
        if (document2ToAppend != null)
            sourcePdfDocument.Append(document2ToAppend);
     
        // ** Save merged file and overwrite in document library
        sourcePdfDocument.Save(mergedDocument);
        spDestinationFile = spDestinationWeb.Files.Add(destinationFilePath, mergedDocument,
                            spSourceDocument.Item.Properties, true);
    }
     
    // ** Check the file back in if this script was responsible for checking it out
    if (spDestinationFile.Item.ParentList.ForceCheckout)
        spDestinationFile.CheckIn("Auto check-in after PDF document appending.");
             
  12. Click the Actions button, select Log to History List, click this message and enter PDF content appended to current item.
     
  13. Close the Workflow Designer.
     
  14. Update an existing PDF or add a new PDF file to your document library to trigger the workflow and append contents from the files defined in parameter 1 and parameter 2.
     
    Files to append   
     
  15. The workflow should look something like this.

    Workflow 


Of course this is just a sample, feel free to play around with the code, change which parameters are passed into the workflow, use different document libraries as source and destination of PDF documents, change the sequence in which documents are appended, etc.

Please leave a comment if you’re trying to do anything specific or if you want to share your experience with this approach.

As always, feel free to contact us using Twitter, our Blog, regular email or subscribe to our newsletter.

.





Labels: , , , , , ,

4 Comments:

  • Hey great article! Will I be able to use this to convert infopath forms into PDF files?

    By Anonymous AKhan, At 30 July, 2010 20:09  

  • Hi Akhan,

    Absolutely, many of our customers use our tools to convert InfoPath to PDF.

    You can either do it from SharePoint (http://blog.muhimbi.com/2009/04/using-pdf-converter-from-sharepoint.html) or from any Web Services based platform (http://blog.muhimbi.com/2009/12/converting-office-files-to-pdf-format.html)

    Contact support@Muhimbi.com if you have any questions.

    By Blogger Muhimbi, At 30 July, 2010 22:23  

  • I am attempting to get code that worked in previous sharepoint version and muhimbi version, to the latest of both, but the validate fails every time now when I add a lookup field. I use the dynamic lookup fields to populate a batch file, that is then passed on to custom code. I am unsure what to do to fix it, the code runs fine without the lookup, but when I include it, then it will not save. Please help.

    By Blogger Tom, At 01 November, 2010 20:24  

  • Hi Tom,

    Perhaps best if you contact the support desk using the 'Contact Us' link at the top of the page. Please include details about the version of the software you used to run, what version you upgraded to and what it is exactly that you are trying to do. The more information the better, including messages from the Windows Application event log.

    By Blogger Muhimbi, At 01 November, 2010 22:03  

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link