Convert Office Word Documents Stored In SharePoint to PDF Using Save As PDF Add In

A while back I was working on a project where we had to convert a massive amount of documents into PDF with minimal intervention. We looked at several products and compared their features as well as their pricing and value-add. Our budget was limited and this functionality was not really needed at all times. The solution didn’t require any robust mechanism for automating this and our client was open for creative solutions.

This post is meant to describe the solution we ended up with along with some code snippets and reference material.

Given the above situation, the solution from a very high level utilized the Save As Add In available for free and the SharePoint Copy Web service to convert the Word documents into PDF. The solution was built in Visual Studio 2005 as a Windows Forms Application that ran on a desktop Win XP SP2 machine.

Steps to Convert

  1. Retrieved the Office document stored in a SharePoint Library
  2. Save the document retrieved in the Windows Temp folder
  3. Convert the document to PDF
  4. Upload the document now converted to PDF back into the SharePoint Library
  5. All of that keeping document metadata intact

Code Snippet

1. Add references to the Copy web service and Word Interop Assembly

   1: using CopyService;
   2: using Microsoft.Office.Interop.Word;

2. This piece of code here takes input from a multiline textbox that contains all Word documents to be converted. Of course, you may call SharePoint Lists web service and get your File names that way too.

<code>

try
{
    //  Copy Web service
    CopyService.Copy service = new CopyService.Copy();               
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    string tempPath = Path.GetTempPath();
    string tempFolder = “ConvertDocToPDF”;
    string tempPathComplete = tempPath + tempFolder;

    //  Create a temp folder to save all converted files
    if (!Directory.Exists(tempPathComplete))
        Directory.CreateDirectory(tempPathComplete);

    foreach (string lineFileName in txtFileName.Lines)
    {
        if (lineFileName.IndexOf(“.pdf”) <= 0)
        {
            string fileNameComplete = lineFileName; //  Includes the extension
            string filePathComplete = tempPathComplete + “/” + fileNameComplete;

            string fileName = lineFileName.Remove(lineFileName.LastIndexOf(“.”)); //    Remove the extension
            string localDestinationFilePath = tempPathComplete + “/” + fileName + “.pdf”;

            string copySourceFilePath = txtSharePointSiteUrl.Text + “/” + txtSharePointLibraryName.Text + “/” + lineFileName;
            byte[] sourceFileArray = null;

            string[] destinationUrls = new string[] { txtSharePointSiteUrl.Text + “/” + txtSharePointLibraryName.Text + “/” + fileName + “.pdf” };

            CopyService.FieldInformation fieldInformation = new CopyService.FieldInformation();
            CopyService.FieldInformation[] fieldInformationArray = { fieldInformation };

            CopyService.CopyResult copyResult1 = new CopyService.CopyResult();
            CopyService.CopyResult copyResult2 = new CopyService.CopyResult();
            CopyService.CopyResult[] copyResultArray = { copyResult1, copyResult2 };

            uint fileUint = service.GetItem(copySourceFilePath, out fieldInformationArray, out sourceFileArray);

            //  Check to see if the original file exists, and if so, remove it
            if (File.Exists(filePathComplete))
            {
                File.Delete(filePathComplete);
            }

            if (File.Exists(filePathComplete.Remove(filePathComplete.LastIndexOf(“.”)) + “.pdf”))
            {
                File.Delete(filePathComplete.Remove(filePathComplete.LastIndexOf(“.”)) + “.pdf”);
            }

            //  Write the bytes array from GetItem to local user temporary folder             
            File.WriteAllBytes(filePathComplete, sourceFileArray);

            //  Start working on converting the downloaded file to PDF
            ApplicationClass wordApplication = new ApplicationClass();
            Document wordDocument = null;

            object paramSourceDocPath = filePathComplete;
            object paramMissing = Type.Missing;

            string paramExportFilePath = localDestinationFilePath;
            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;

            WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

            int paramStartPage = 0;
            int paramEndPage = 0;

            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;

            WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            // Try to convert the downloaded file
            try
            {
                // Open the source document.
                wordDocument = wordApplication.Documents.Open(
                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing);

                // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Close and release the Document object.
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            try
            {
                uint copyUint = service.CopyIntoItems(localDestinationFilePath, destinationUrls, fieldInformationArray, File.ReadAllBytes(localDestinationFilePath), out copyResultArray);                           
            }
            catch (Exception exc)
            {                           
            }
            finally
            {
            }
        }
    }         
}
catch (Exception ex)
{
    throw ex;
}
finally
{                              
} </code>

References

Saving Word 2007 documents to PDF and XPS Formats on MSDN – http://msdn.microsoft.com/en-us/library/bb412305(office.12).aspx

SharePoint Office Server Copy Web Service on MSDN – http://msdn.microsoft.com/en-us/library/copy.copy(office.12).aspx

Leave a comment