Translate

Thursday, December 13, 2012

Convert word file to pdf in c#


//Change the path of the .docx file and filename to your file name.
        object paramSourceDocPath = Server.MapPath("IITJEEPaper-1Final.docx");
        object paramMissing = Type.Missing;

        ApplicationClass wordApplication = new ApplicationClass();
        Document wordDocument = null;

        //Change the path of the .pdf file and filename to your file name.
        string paramExportFilePath = Server.MapPath("IITJEEPaper-1Final.pdf");
        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
        {
            // 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)
        {
            // Respond to the error
            //System.Windows.Forms.MessageBox.Show(ex.Message);
            Response.Write(ex.Message);
        }
        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();
        }
    }