Word to PDF Conversion using Word Automation Service

In this article, I would like to show you on how to utilize Word Automation Service to convert word document into PDF document. Recently, I have an objective to convert document to PDF when it has been approved. So, I just use it with ItemUpdated event receiver and it works as expected.

The requirement is Word Automation Service needs to be configured and started. I will not go through with you how to setup Word Automation Service, you could refer it here.

Below is the function to convert Word to PDF document, it will return byte array. Replace WORD_AUTOMATION_SERVICE with your “Word Automation Service” service name.

        private byte[] ConvertWordToPDF(SPFile spFile, SPUserToken usrToken)
        {
            byte[] result = null;
            try
            {
                using (Stream read = spFile.OpenBinaryStream())
                {
                    using (MemoryStream write = new MemoryStream())
                    {
                        // Initialise Word Automation Service
                        SyncConverter sc = new SyncConverter(WORD_AUTOMATION_SERVICE);
                        sc.UserToken = usrToken;
                        sc.Settings.UpdateFields = true;
                        sc.Settings.OutputFormat = SaveFormat.PDF;

                        // Convert to PDF
                        ConversionItemInfo info = sc.Convert(read, write);
                        if (info.Succeeded)
                            result = write.ToArray();
                    }
                }
            }
            catch (Exception ex)
            {
                // Do your error management here.
            }
            return result;
        }

The ItemUpdated event, it will look like below. The PDF document will be added into “Documents Public” library with all custom column / metadata updated as well.

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            try
            {
                SPWeb web = properties.Web;
                SPFolder sourceFolder = properties.List.RootFolder;
                SPListItem sourceItem = properties.ListItem;
                SPFile sourceFile = sourceItem.File;
                string sourceFileName = sourceFile.Name.Trim();

                SPFolder destFolder = web.Lists["Documents Public"].RootFolder;
                SPFile destFile = null;
                string destFileName = sourceFileName.Substring(0, sourceFileName.LastIndexOf('.')) + ".pdf";
                bool isOverwrite = true;
                bool isDestApprovalRequired = false;

                // If File Approved
                if (sourceItem.ModerationInformation.Status == SPModerationStatusType.Approved)
                {
                    // Convert the office file
                    if (sourceFileName.EndsWith(".docx") || sourceFileName.EndsWith(".doc"))
                    {
                        // Check out existing PDF file if exists
                        CheckOutFile(web, String.Format("{0}/{1}/{2}", web.Url, destFolder.Url, destFileName));
                        destFile = destFolder.Files.Add(destFileName, ConvertWordToPDF(sourceFile, properties.Site.UserToken), isOverwrite );
                    }

                    if (destFile != null)
                    {
                        // Update PDF File metadata
                        foreach (SPField eachPdfField in destFile.Item.Fields)
                        {
                            if (eachPdfField.FromBaseType == false && !eachPdfField.ReadOnlyField)
                            {
                                destFile.Item[eachPdfField.Id] = sourceItem[eachPdfField.Id];
                            }
                        }

                        // Apply Update to PDF File
                        destFile.Item.Update();
                        destFolder.Update();

                        // Check In PDF File
                        if (destFile != null && destFile.CheckOutType != SPFile.SPCheckOutType.None)
                            destFile.CheckIn("");

                        // Approve
                        if(isDestApprovalRequired)
                            destFile.Approve("");

                        destFolder.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                // Do your error management here.
            }
        }

        private void CheckOutFile(SPWeb web, string sFileUrl)
        {
            try
            {
                SPFile currFile = web.GetFile(sFileUrl);
                if (currFile != null && currFile.CheckOutType == SPFile.SPCheckOutType.None)
                {
                    currFile.CheckOut();
                }
            }
            catch (Exception ex)
            {
                // Do your error management here.
            }
        }
Advertisements

Tagged: , , , , , , , , , , , ,

3 thoughts on “Word to PDF Conversion using Word Automation Service

  1. […] from my previous article to convert word document to PDF, I would like to show you on how to utilize Power Point […]

  2. aaedla 06/26/2014 at 11:52 AM Reply

    Thank you for your post. I tried to implement your code but I am getting SyncConverter name is missing error.

    I have added Microsoft.Office.Word.Server reference still I am getting same error.

    Can you please let me know how to get resolve the issue?

    • tjenho 06/28/2014 at 6:20 AM Reply

      Thanks for looking into my post, where do you get the error ? Is it when initializing SyncConverter instance ? Try below:
      1. Ensure you get the right Word Automation Service name. Check it with powershell “Get-SPServiceApplicationPool”.
      2. Have you tried deleting the Word Automation Service and recreate it again ?

      If you fixed the issue, it would be great if you could share how you fix it here. Thank you …

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: