Friday, 16 May 2014

Convert Ms Document in to Pdf in ASP.Net C#

In this article we are going to see how to convert the MsDoc in to PDF, for this we have to install one addin for the MsOffice Then we have to use the MsWord class to access the word object 

Click here to install or Download the Add-In for the Microsoft office to convert the Doc to PDF

After Install the Add-in start writing the code in Aspx page to upload the Document to the server and then convert it to do this we have to use the file upload control.Drag and Drop the File upload control and A Button control, then the design will look like following image

Sample Word document content:


Add the reference to the Microsoft.Office.Interop.Word

Then Paste the following code in the button click of the upload control.

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
    public partial class Sample : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)


        protected void Button1_Click(object sender, EventArgs e)
            FileUpload1.SaveAs(Server.MapPath("~/Doc/" + FileUpload1.FileName));

            string sourcefile = Server.MapPath("Doc\\") + FileUpload1.FileName;
            string outputfile = Server.MapPath("Doc\\") + FileUpload1.FileName.Replace(".doc", ".pdf").Replace(".pdfx", ".pdf");
            if(outputfile!=null || outputfile!="")
                Application wordApplication = new Application();
                Document wordDocument = null;
                object paramSourceDocPath = sourcefile;

                object paramMissing = System.Reflection.Missing.Value;
                string paramExportFilePath = outputfile;
                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;
                    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);

                    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)
                    if (wordDocument != null)
                        wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                        wordDocument = null;

                    } if (wordApplication != null)
                        wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing); wordApplication = null;




we have to close and quit the word document, wordapplication otherwise it will remain in the memory.

Now Run the application and choose one doc file and click upload, at the first time it will raise exception because you have to create a doc folder in the server application path, then now try again the doc is uploaded to the under the folder doc and converted pdf is saved in the same directory under the same name.

Output Pdf

From this article you can learn how to convert the document to Pdf using Microsoft Add-in.

1 comment:

  1. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.