• WPF读取和显示word


    引言

    在项目开发中,word的读取和显示会经常出现在客户的需求中。特别是一些有关法律规章制度、通知、红头文件等,都是用word发布的。

    在WPF中,对显示WORD没有特定的控件,这对开发显示WORD的需求就增加了点困难。其实只要转换下思路,问题就可以迎刃而解了。

    WORD转换为XPS

    没错,就是XPS了。XPS 是XML Paper Specification(XML文件规格书)的简称,是一种电子文件格式,它是微软公司开发的一种文档保存与查看的规范。.NET平台下,对操作XPS文件格式,提供了支持。另外,在WPF中,可以使用DocumentViewer来浏览XPS文件。这时只需把word转换为XPS,再用DocumentViewer来显示,问题就得到了很好的解决。下面是word转换为xps的方法,需要引用Microsoft.Office.Interop.Word组件。

     1 private XpsDocument ConvertWordToXPS(string wordDocName)
     2         {
     3             FileInfo fi=new FileInfo(wordDocName);
     4             XpsDocument result = null;
     5             string xpsDocName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), fi.Name);
     6             xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps");
     7             Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
     8             try
     9             {
    10                 if (!File.Exists(xpsDocName))
    11                 {
    12                     wordApplication.Documents.Add(wordDocName);
    13                     Document doc = wordApplication.ActiveDocument;
    14                     doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, Type.Missing);
    15                     result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);
    16                 }
    17 
    18                 if (File.Exists(xpsDocName))
    19                 {
    20                     result = new XpsDocument(xpsDocName, FileAccess.Read);                    
    21                 }
    22 
    23             }
    24             catch (Exception ex)
    25             {
    26                 string error = ex.Message;
    27                 wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);
    28             }
    29 
    30             wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);
    31 
    32             return result;
    33         }

    上面方法传递一个word文件路径,然后返回一个XpsDocument。方法用我把XPS文件存放到了SpecialFolder.InternetCache文件夹中,因为XPS是作为中间转换,不需要存放和使用。方法返回XpsDocument,就可以使用下面两行代码即可显示到界面上来:

    docViewer.Document = ConvertWordToXPS(filePath).GetFixedDocumentSequence();
    docViewer.FitToWidth();

    这样,一个显示WORD文档就轻搞定了!

    源码下载:

    扩展

    1、这只是显示WORD的,那能显示PDF,txt嘛?答案是肯定的。同样,只需把PDF,txt转换为XPS就可以了。PDF转换为XPS,可以参考下这篇:Link。采用Adobe Reader 和Microsoft XPS默认打印,然后另存为的方式,这个方法有点麻烦,其实还有更有效的方法,这里就留给大家思考和google了。

    2、上面的XPS只作为中间转换,能否保存起来,这样下次就不用再转换,直接读取?Of course ! 保存XPS,可以有两种方法,一种是用文件式保存到指定目录中;一种是保存到数据库中。文件式的保存,我就不多介绍了。那XPS如何保存到数据库中呢?其实,XPS是一种二进制流的格式,这和image保存到数据库中是一样的。所以,只需把XPS文件读成byte类型,就可以保存到数据库中image类型的字段下。显示时,直接从数据库中读取生成XPS显示到DocumentViewer上就可以了。但这样做有个缺点:当文件转换为XPS,文件会变大,如果保存起来的,相当浪费空间。

    Thank you !

  • 相关阅读:
    angular $modal 模态框
    过滤器 ||(filter)
    info sharp Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag
    git error: unable to create file Invalid argument
    bash shell 快捷键
    options has an unknown property 'modifyVars'. These properties are valid: 处理方法
    test 分支强制替换master 分支的办法
    脚本统计代码行数
    git commit 后,没有push ,怎么撤销
    php 用户ip的获取
  • 原文地址:https://www.cnblogs.com/lisweden/p/3544642.html
Copyright © 2020-2023  润新知