How to Convert docx(word) to pdf in ASP.NET with C#.Net

 In my previous example i have shown HTML to pdf conversion using ASP.NET with C# code.In this example i am showing how to convert word(docx) to pdf in ASP.NET with C# code. for this we have to do little set up. Please follow below steps carefully.

Input docx: 
How to Convert docx(word) to pdf in ASP.NET with C#.Net
Convert docx(word) to pdf in ASP.NET with C#.Net

Output pdf:
How to Convert docx(word) to pdf  C#.Net
How to Convert docx(word) to pdf  C#.Net


Steps:

1. Word 2007 should install in your system.

2. Download SaveAsPDFandXPS.exe and install.(Note: If you are doing any word automation, please install  http://www.microsoft.com/en-us/download/details.aspx?id=5124)

3. Take Add Reference of Microsoft.Office.Interop.Word (12.0 Version) . Please refer below image.

Convert Docx to pdf addreference.
Convert Docx to pdf addreference.

4.Add below name in the namespace declarations.
 using Microsoft.Office.Interop.Word;

5. Use below code for conversion: 
ConvertDocument(string sourceDocPath, string targetFilePath, Microsoft.Office.Interop.Word.WdExportFormat targetFormat) method will do the conversion from docx to pdf. for thid you have to give sourcePath, TargetPath as well desire export format(WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;)

class Program
    {
        static void Main(string[] args)
        {
            WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
            string sourceDoc = @"d:\\RangaRajesh.docx";
            string targetPDF = @"d:\\rangarajesh.pdf";
            ConvertDocument(sourceDoc, targetPDF, exportFormat);
        }
        public static void ConvertDocument(string sourceDocPath, string targetFilePath, Microsoft.Office.Interop.Word.WdExportFormat targetFormat)
        {
            // Make sure the source document exists.
            if (!System.IO.File.Exists(sourceDocPath))
                throw new Exception("The specified source document does not exist.");

            // Create an instance of the Word ApplicationClass object.          
            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document wordDocument = null;

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

            // Declare variables for the Document.ExportAsFixedFormat method parameters.
            string paramExportFilePath = targetFilePath;       
            Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen;
            Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks =
                Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            try
            {
                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();
            }
        }
    }


If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

+ comments + 1 comments

Anonymous
3 May 2012 at 10:43

It is working perfectly... Thanks for this.

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar