public bool WordToPdf(string sourcePath)
        {
            object docxPath = sourcePath;
            string pdfPath  = null;

            //docxPath = @"C:\Users\Zhang_weiwei\Desktop\第四周测试论文\基于eLAMP的Linux安全加固方案设计与实现张俊霞论文.docx";
            //pdfPath = @"C:\Users\Zhang_weiwei\Desktop\第四周测试论文\基于eLAMP的Linux安全加固方案设计与实现张俊霞论文.pdf";
            pdfPath = sourcePath.Replace("docx", "pdf");
            bool           result            = false;
            WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
            object         missing           = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
            Document document = null;

            try
            {
                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                document         = applicationClass.Documents.Open(ref docxPath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                if (document != null)
                {
                    document.ExportAsFixedFormat(pdfPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                }
                result = true;


                if (document != null)
                {
                    document.Close();
                    document = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit();
                    applicationClass = null;
                }
            }
            catch (Exception e)
            {
                killWinWord();
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
            }
            return(result);
        }
예제 #2
0
파일: WordEngine.cs 프로젝트: Vocaoson/qlgx
        public static int ConvertDocToPDF(object Source, object Target)
        {
            Microsoft.Office.Interop.Word.ApplicationClass MSdoc;
            object Unknown = Type.Missing;

            //Creating the instance of Word Application
            MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {
                MSdoc.Visible = false;
                MSdoc.Documents.Open(ref Source, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown,
                                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                MSdoc.Application.Visible = false;
                MSdoc.WindowState         = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;

                object format = WdSaveFormat.wdFormatPDF;

                MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                                            ref Unknown, ref Unknown, ref Unknown,
                                            ref Unknown, ref Unknown, ref Unknown,
                                            ref Unknown, ref Unknown, ref Unknown,
                                            ref Unknown, ref Unknown, ref Unknown,
                                            ref Unknown, ref Unknown);
            }
            catch (Exception ex)
            {
                Memory.Instance.Error = ex;
                return(-1);
            }
            finally
            {
                if (MSdoc != null)
                {
                    MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                }
                // for closing the application
                object saveChanges    = Type.Missing;
                object originalFormat = Type.Missing;
                object routerDocument = Type.Missing;
                MSdoc.Quit(ref saveChanges, ref originalFormat, ref routerDocument);
            }
            return(0);
        }
예제 #3
0
        /// <summary>
        /// word转成html
        /// </summary>
        /// <param name="wordFileName">word文档的相对路径</param>
        /// <returns>转化后的Html文档的物理路径</returns>
        private string WordToHtml(object wordFileName)
        {
            wordFileName = HttpContext.Current.Server.MapPath(wordFileName.ToString());
            //在此处放置用户代码以初始化页面
            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();

            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            //打开文件
            Type docsType = docs.GetType();

            Microsoft.Office.Interop.Word.Document doc =
                (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                              System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
            //转换格式,另存为
            Type   docType          = doc.GetType();
            string wordSaveFileName = wordFileName.ToString();
            string strSaveFileName  = wordSaveFileName.Substring(0, wordSaveFileName.LastIndexOf('.')) + ".html";
            object saveFileName     = (object)strSaveFileName;

            //下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:

            /*
             * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
             * null, doc, new object[]{saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML});
             */
            ///其它格式:
            ///wdFormatHTML
            ///wdFormatDocument
            ///wdFormatDOSText
            ///wdFormatDOSTextLineBreaks
            ///wdFormatEncodedText
            ///wdFormatRTF
            ///wdFormatTemplate
            ///wdFormatText
            ///wdFormatTextLineBreaks
            ///wdFormatUnicodeText
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });

            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                 null, doc, null);
            //退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
                                  null, word, null);
            return(saveFileName.ToString());
        }
예제 #4
0
        public static string ReadWord(string wordFileName)
        {
            string text = string.Empty;

            Word.ApplicationClass app = null;
            Word.Document         doc = null;
            object readOnly           = true;
            object missing            = System.Reflection.Missing.Value;
            object fileName           = wordFileName;

            try
            {
                app = new Microsoft.Office.Interop.Word.ApplicationClass();

                doc = app.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                doc.Activate();
                //doc.ActiveWindow.Selection.WholeStory();
                //doc.ActiveWindow.Selection.Copy();
                //doc.Content.;
                //System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
                //text = data.GetData(System.Windows.Forms.DataFormats.Text).ToString();



                // doc.Activate();
                text = doc.Content.Text;
                //text = doc.Content.Text.Replace("\r", "<br>").Replace("\n", "<br>").Replace("\t", string.Empty);
            }
            catch (Exception e)
            {
                text = e.Message;
            }
            finally
            {
                doc.Close(ref missing, ref missing, ref missing);
                doc = null;
                app.Quit(ref missing, ref missing, ref missing);
                app = null;
            }
            return(text);
        }
예제 #5
0
        /// <summary>
        /// 打印word
        /// </summary>
        public static void PrintWord(string filePath, MacrosVerbCollection macrosCollection)
        {
            Word.Application app = new Word.Application();

            Word.Document doc     = null;
            object        collate = true;
            object        item    = Word.WdPrintOutItem.wdPrintDocumentContent;
            object        missing = System.Reflection.Missing.Value;

            try
            {
                object path = filePath;
                doc = app.Documents.Add(ref path, ref missing, ref missing, ref missing);
                Replace(doc, macrosCollection);

                app = new Microsoft.Office.Interop.Word.ApplicationClass();
                doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
                             ref missing, ref missing, ref item, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing);
            }
            catch (Exception exp)
            {
                Function.Alert(exp.Message, "提示");
            }
            finally
            {
                object         saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                Word._Document _doc       = doc as Word._Document;
                if (_doc != null)
                {
                    _doc.Close(ref saveChange, ref missing, ref missing);
                }
                Word._Application _app = app as Word._Application;
                if (_app != null)
                {
                    _app.Quit(ref missing, ref missing, ref missing);
                }
            }
        }
예제 #6
0
        public bool WordToPdf(object sourcePath, string targetPath)
        {
            bool           result            = false;
            WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
            object         missing           = Type.Missing;

            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
            Microsoft.Office.Interop.Word.Document         document         = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                document         = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                if (document != null)
                {
                    document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close(ref missing, ref missing, ref missing);
                    document = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit(ref missing, ref missing, ref missing);
                    applicationClass = null;
                }
            }
            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);
        }