示例#1
0
        public static void SaveDocumentAsPDF(Word.Document document, string targetPath)
        {
            if (Path.GetExtension(targetPath).ToUpper() != "PDF")
            {
                targetPath = Path.Combine(Path.GetDirectoryName(targetPath), string.Concat(Path.GetFileNameWithoutExtension(targetPath), ".pdf"));
            }
            try
            {
                if (File.Exists(targetPath))
                {
                    File.Delete(targetPath);
                }

                Word.WdExportFormat exportFormat           = Word.WdExportFormat.wdExportFormatPDF;
                bool openAfterExport                       = false;
                Word.WdExportOptimizeFor exportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange       exportRange       = Word.WdExportRange.wdExportAllDocument;
                int startPage = 0;
                int endPage   = 0;
                Word.WdExportItem exportItem = Word.WdExportItem.wdExportDocumentContent;
                bool includeDocProps         = false;
                bool keepIRM = true;
                Word.WdExportCreateBookmarks createBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool docStructureTags   = true;
                bool bitmapMissingFonts = true;
                //11/05/2017
                bool   useISO19005_1 = true;
                object missing       = Missing.Value;

                // Export it in the specified format.
                if (document != null)
                {
                    document.ExportAsFixedFormat(
                        targetPath,
                        exportFormat,
                        openAfterExport,
                        exportOptimizeFor,
                        exportRange,
                        startPage,
                        endPage,
                        exportItem,
                        includeDocProps,
                        keepIRM,
                        createBookmarks,
                        docStructureTags,
                        bitmapMissingFonts,
                        useISO19005_1,
                        ref missing);
                }
            }
            catch (System.IO.IOException ex)
            {
                logger.LogException(LogLevel.Error, "SaveDocumentAsPDF", ex);
                throw ex;
            }
            catch (Exception ex)
            {
                logger.LogException(LogLevel.Error, "SaveDocumentAsPDF", ex);
            }
        }
示例#2
0
        static void word2pdf(string sourcePath, string targetPath)
        {
            // Console.WriteLine("hello..");
            Word.Application myWordApp;
            Word.Document    myWordDoc;
            myWordApp = new Word.ApplicationClass();
            // object filepath =  fileString;
            object filepath = sourcePath;
            object oMissing = System.Reflection.Missing.Value;

            myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport             = false;

            Word.WdExportOptimizeFor paramExportOptimizeFor =
                // Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage   = 0;

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

            Word.WdExportCreateBookmarks paramCreateBookmarks =
                Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool   paramDocStructureTags   = true;
            bool   paramBitmapMissingFonts = true;
            bool   paramUseISO19005_1      = false;
            string paramExportFilePath     = targetPath;

            myWordDoc.ExportAsFixedFormat(paramExportFilePath,
                                          paramExportFormat, paramOpenAfterExport,
                                          paramExportOptimizeFor, paramExportRange, paramStartPage,
                                          paramEndPage, paramExportItem, paramIncludeDocProps,
                                          paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                                          paramBitmapMissingFonts, paramUseISO19005_1,
                                          ref oMissing);
            myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            myWordDoc = null;
            myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            myWordDoc = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
示例#3
0
        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public static bool DOCConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;

            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
            object paramMissing = Type.Missing;

            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document         wordDocument    = null;
            try
            {
                object paramSourceDocPath                       = sourcePath;
                string paramExportFilePath                      = targetPath;
                Word.WdExportFormat paramExportFormat           = exportFormat;
                bool paramOpenAfterExport                       = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange       paramExportRange       = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage   = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps         = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks = Word.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);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                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;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }
示例#4
0
        public static string ConvertData(string files)
        {
            FileInfo newFile = new FileInfo(files);

            //if (!Directory.Exists(System.Windows.Forms.Application.StartupPath + @"\Report\"))
            //Directory.CreateDirectory(System.Windows.Forms.Application.StartupPath + @"\Report\");

            object paramSourceDocPath = files;//System.Windows.Forms.Application.StartupPath + @"\" + files;// @"D:\test.xls";
            object paramMissing       = Type.Missing;

            string name = Path.GetFileNameWithoutExtension(files);
            //string paramExportFilePath = System.Windows.Forms.Application.StartupPath + @"\Report\" + name + ".pdf";
            string paramExportFilePath = newFile.DirectoryName + @"\\" + name + ".pdf";

            Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;

            bool result;

            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document         wordDocument    = null;
            wordApplication.Caption = System.Guid.NewGuid().ToString().ToUpper();
            try
            {
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage   = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps         = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks =
                    Word.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);
                }
                result = true;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    Config.EnsureProcessKilled(IntPtr.Zero, wordApplication.Caption);

                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(paramExportFilePath);
        }
示例#5
0
        public void ToPdf()
        {
            bool occupy = true;

            System.Collections.Generic.List <string> strAry = new List <string>();
            while (occupy)
            {
                try
                {
                    using (File.Open(m_FilePathWord, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    { }
                    occupy = false;//如果可以运行至此那么就是
                }
                catch (IOException e)
                {
                    strAry.Add(e.ToString());

                    Thread.Sleep(100);
                }
            }
            WORD.ApplicationClass wordApp = new WORD.ApplicationClass();

            WORD._Document wordDoc             = null;
            object         paramSourceDoc      = m_FilePathWord;
            object         paramMissing        = Type.Missing;
            string         strPdf              = m_FilePathWord.Substring(0, m_FilePathWord.LastIndexOf('.'));
            string         paramExportFilePath = strPdf;

            WORD.WdExportFormat paramexportFormat = WORD.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport             = false;

            WORD.WdExportOptimizeFor parameExportOptimizeFor = WORD.WdExportOptimizeFor.wdExportOptimizeForPrint;
            WORD.WdExportRange       parameExportRange       = WORD.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage   = 0;

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

            WORD.WdExportCreateBookmarks paramCreateBookMarks = WORD.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags   = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1      = false;

            try
            {
                wordDoc = wordApp.Documents.Open(
                    ref paramSourceDoc, 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 (wordDoc != null)
                {
                    wordDoc.ExportAsFixedFormat(paramExportFilePath,
                                                paramexportFormat, paramOpenAfterExport,
                                                parameExportOptimizeFor, parameExportRange, paramStartPage,
                                                paramEndPage, paramExportItem, paramIncludeDocProps,
                                                paramKeepIRM, paramCreateBookMarks, paramDocStructureTags,
                                                paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing);
                }
                if (wordDoc != null)
                {
                    wordDoc.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
                }
                if (wordApp != null)
                {
                    wordApp.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
                }
                PostHttpMsg pm = new PostHttpMsg();
                pm.DataPost = strPdf + ".pdf";
                pm.PostMsg();
            }
            catch (System.Exception ex)
            {
                ex.ToString();
            }
            finally
            {
                wordDoc = null;
                wordApp = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
        /// <summary>
        /// 将word文档转换成PDF格式
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns></returns>
        private bool WordConvertPDF(string sourcePath, string targetPath)
        {
            bool result;

            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;   //PDF格式
            object paramMissing = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document         wordDocument    = null;
            try
            {
                object paramSourceDocPath  = sourcePath;
                string paramExportFilePath = targetPath;

                Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                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;

                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);
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception ex)
            {
                Response.Write("<p>Word转PDF出现问题,详情:" + ex.ToString() + "</p>");
                result = false;
            }
            finally
            {
                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;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }
示例#7
0
        /// <summary>
        /// 将word文档转换成PDF格式
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <param name="exportFormat"></param>
        /// <returns></returns>
        public bool WordConvertPDF(string sourcePath, string targetPath = null)
        {
            if (sourcePath == null)
            {
                throw new ArgumentNullException("wpsFilename");
            }
            //保存路径为空时,保存在原始文件目录
            if (targetPath == null)
            {
                targetPath = Path.ChangeExtension(sourcePath, "pdf");
            }
            bool   result;
            object paramMissing = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath  = sourcePath;
                string paramExportFilePath = targetPath;

                Word.WdExportFormat paramExportFormat           = Word.WdExportFormat.wdExportFormatPDF;
                bool paramOpenAfterExport                       = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage   = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps         = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks =
                    Word.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);
                }
                result = true;
            }
            finally
            {
                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;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }
示例#8
0
        public bool Save(Word.Document Doc, string pdffullfilename)
        {
            bool retval = false;

            try
            {
                string docfilename = Doc.Name;
                //string filedir = Doc.Path;

                //string pdffilename = docfilename.Replace(Path.GetExtension(docfilename), ".pdf");
                Word.WdExportFormat      exportFormat      = Word.WdExportFormat.wdExportFormatPDF;
                Word.WdExportOptimizeFor exportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange       exportRange       = Word.WdExportRange.wdExportAllDocument;
                //Word.WdExportItem exportItem = Word.WdExportItem.wdExportDocumentWithMarkup;
                Word.WdExportItem            exportItem      = Word.WdExportItem.wdExportDocumentContent;
                Word.WdExportCreateBookmarks createBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;

                bool includeDocProps    = true;
                bool keepIRM            = true;
                bool docStructureTags   = true;
                bool bitmapMissingFonts = true;
                bool useISO19005_1      = true;
                bool openAfterExport    = false;

                //If Export Range Is Set
                int startPage = 0;
                int endPage   = 0;

                object missing = Missing.Value;

                // Export it in the specified format.
                if (Doc != null)
                {
                    Doc.ExportAsFixedFormat(
                        pdffullfilename /*Path.Combine(filedir, pdffilename)*/,
                        exportFormat,
                        openAfterExport,
                        exportOptimizeFor,
                        exportRange,
                        startPage,
                        endPage,
                        exportItem,
                        includeDocProps,
                        keepIRM,
                        createBookmarks,
                        docStructureTags,
                        bitmapMissingFonts,
                        useISO19005_1,
                        ref missing);
                }
                retval = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Save PDF Failed With: " + ex.ToString());
                Console.WriteLine("Press Enter To Continue/Exit");
                Console.ReadLine();
                throw (ex);
            }
            return(retval);
        }
示例#9
0
        /// <summary>
        /// doc转pdf
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <param name="exportFormat"></param>
        /// <returns></returns>
        public bool Convert(string sourcePath, string targetPath, Microsoft.Office.Interop.Word.WdExportFormat exportFormat)
        {
            bool   result;
            object paramMissing = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document         wordDocument    = null;
            try
            {
                object paramSourceDocPath  = sourcePath;
                string paramExportFilePath = targetPath;

                Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                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;

                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);

                //ClosePwd(WordDocArr[0], "myclou");
                //增加插入图片的功能
                //object bookmark = "sherolee";

                //wordDocument.Bookmarks.get_Item(ref bookmark).Select();

                //if (wordDocument.Bookmarks.Exists(bookmark.ToString()) == true)
                //{
                //    try
                //    {

                //        wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //        object Nothing = System.Reflection.Missing.Value;
                //        //定义该插入图片是否为外部链接
                //        object linkToFile = true;
                //        //定义插入图片是否随word文档一起保存
                //        object saveWithDocument = true;
                //        InlineShape inlineShape = wordApplication.Selection.InlineShapes.AddPicture(@"C:\Users\screw\Desktop\ReportSave\sign.jpg", ref linkToFile, ref saveWithDocument, ref Nothing);

                //        inlineShape.Width = 20;

                //        inlineShape.Height = 16;
                //    }
                //    catch (Exception e)
                //    {

                //    }


                //}

                //InsertEditPwd(ref wordDocument, "myclou");

                //
                if (wordDocument != null)
                {
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                                                     paramExportFormat, paramOpenAfterExport,
                                                     paramExportOptimizeFor, paramExportRange, paramStartPage,
                                                     paramEndPage, paramExportItem, paramIncludeDocProps,
                                                     paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                                                     paramBitmapMissingFonts, paramUseISO19005_1,
                                                     ref paramMissing);
                }
                result = true;
            }
            finally
            {
                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;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }
示例#10
0
        public bool Convert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
        {
            object _nullobj     = System.Reflection.Missing.Value;
            bool   result       = false;
            int    page         = 0;
            object paramMissing = Type.Missing;
            object Nothing      = System.Reflection.Missing.Value;

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

            try
            {
                //DrawItems.WriteLog("Convert");
                object paramSourceDocPath  = sourcePath;
                string paramExportFilePath = targetPath;

                Word.WdExportFormat paramExportFormat           = exportFormat;
                bool paramOpenAfterExport                       = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor =
                    Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportFromTo;
                int paramStartPage = 1;
                int paramEndPage   = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps         = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks =
                    Word.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)
                {
                    page = wordDocument.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref _nullobj);
                }

                if (page % 2 != 0)//删除最后一页空白页
                {
                    paramEndPage = page - 1;
                }
                else
                {
                    paramEndPage = page;
                }

                wordDocument.ExportAsFixedFormat(paramExportFilePath,
                                                 paramExportFormat, paramOpenAfterExport,
                                                 paramExportOptimizeFor, paramExportRange, paramStartPage,
                                                 paramEndPage, paramExportItem, paramIncludeDocProps,
                                                 paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                                                 paramBitmapMissingFonts, paramUseISO19005_1,
                                                 ref paramMissing);
                result = true;
            }
            catch (Exception ex)
            {
                DrawItems.WriteLog(ex.ToString());

                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;
                }
                throw ex;
            }
            finally
            {
                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;
                }

                GC.Collect();
                //GC.WaitForPendingFinalizers();
            }

            return(result);
        }