[WPF]读取word文档


//将word转换成xps
private XpsDocument ConvertWordToXPS(string wordDocName) { FileInfo fi = new FileInfo(wordDocName); XpsDocument result = null; string xpsDocName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), fi.Name); xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps"); Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application(); try { if (!File.Exists(xpsDocName)) { wordApplication.Documents.Add(wordDocName); Microsoft.Office.Interop.Word.Document doc = wordApplication.ActiveDocument; doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, Type.Missing); result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read); } if (File.Exists(xpsDocName)) { result = new XpsDocument(xpsDocName, FileAccess.Read); } } catch (Exception ex) { string error = ex.Message; wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges); } wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges); return result; }
//页面调用

DocumentViewer docViewer = new DocumentViewer();
docViewer.Document = new XpsDocument(("地址参数"+ ".xps"), System.IO.FileAccess.Read).GetFixedDocumentSequence();

docViewer.FitToWidth();

 

 

posted on 2019-05-30 09:11  菜鸟也想上天  阅读(1304)  评论(0编辑  收藏  举报