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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }