WPF实现在线预览和显示Word和PDF文件

 更新时间:2024年02月23日 14:45:42   作者:搬砖的诗人Z  
这篇文章主要为大家详细介绍了如何使用WPF实现在线预览和显示Word和PDF文件,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

效果图

PDF和Word预览,可用于WPF和Winform。 原理是采用spire把word或者pdf文件转成xps文件,用documentViewer来呈现,并重新封装了显示的样子

WPF 需要引用的包,本地程序集

ReachFramework spire.office
Microsoft.Office.Interop.Word.dll

spire把word或者pdf文件转成xps文件

word转化提供另外一种方法,采用引用Microsoft.Office.Interop.Word.dll

实现代码

using Spire.Doc;
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Xps.Packaging;

namespace WpfApp3
{
    /// <summary>
    /// Main.xaml 的交互逻辑
    /// </summary>
    public partial class Main : Window
    {
        /// <summary>
        /// 转化临时显示文件
        /// </summary>
        public string tempPdfPreAddress = Environment.CurrentDirectory + "\\tempPdfPre\\";

        /// <summary>
        /// 统一读取
        /// </summary>
        XpsDocument readerDoc;

        public Main()
        {
            InitializeComponent();

            if (!Directory.Exists(tempPdfPreAddress))
            {
                Directory.CreateDirectory(tempPdfPreAddress);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string filePath = Environment.CurrentDirectory + "\\个人申请微信公众号教程.docx";
            ConvertWordToXPS2(filePath);
        }

        private void Button_Pdf_Click(object sender, RoutedEventArgs e)
        {
            string filePath = Environment.CurrentDirectory + "\\样板.pdf";
            ConvertPdfToXPS(filePath);
        }

        private void ConvertPdfToXPS(string pdfDocName)
        {
            if (readerDoc != null)
                readerDoc.Close();

            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.LoadFromFile(pdfDocName); 
            string name = tempPdfPreAddress + System.IO.Path.GetFileNameWithoutExtension(pdfDocName) + ".xps";
            pdfDocument.SaveToFile(name, Spire.Pdf.FileFormat.XPS);
            pdfDocument.Close();

            readerDoc = new XpsDocument(name, FileAccess.Read);
            docViewer.Document = readerDoc.GetFixedDocumentSequence();
            docViewer.FitToWidth();

        }

        private void ConvertWordToXPS2(string wordDocName)
        {
            if (readerDoc != null)
                readerDoc.Close();

            Document document = new Document(wordDocName);
            string name = tempPdfPreAddress + System.IO.Path.GetFileNameWithoutExtension(wordDocName) + ".xps";
            document.SaveToFile(name, Spire.Doc.FileFormat.XPS);
            document.Close();

            readerDoc = new XpsDocument(name, FileAccess.Read);
            docViewer.Document = readerDoc.GetFixedDocumentSequence();
            docViewer.FitToWidth();
        }

        //private XpsDocument ConvertWordToXPS(string wordDocName)
        //{
        //    FileInfo fi = new FileInfo(wordDocName);
        //    XpsDocument result = null;
        //    string xpsDocName = System.IO.Path.Combine(tempPdfPreAddress, 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, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatXPS, false, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 0, 0, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, true, true, Microsoft.Office.Interop.Word.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(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
        //    }

        //    wordApplication.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);

        //    return result;
        //}

        private void Button_page_Click(object sender, RoutedEventArgs e)
        {

            MessageBox.Show(docViewer.MasterPageNumber + "/" + docViewer.PageCount.ToString());
        }

        private void ZoomInButton_mouse_down(object sender, MouseButtonEventArgs e)
        {
            docViewer.Zoom += 10;
        }

        private void ZoomOutButton_mouse_down(object sender, MouseButtonEventArgs e)
        {
            docViewer.Zoom -= 10;
        }

        private void Back_mouse_down(object sender, MouseButtonEventArgs e)
        {

        }
    }
}

到此这篇关于WPF实现在线预览和显示Word和PDF文件的文章就介绍到这了,更多相关WPF在线预览文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Unity3D实现3D迷宫小游戏的示例代码

    基于Unity3D实现3D迷宫小游戏的示例代码

    迷宫游戏作为经典的小游戏,一直深受大家的喜爱。本文小编将为大家详细介绍一下如何用Unity实现一个3D版的迷宫小游戏,感兴趣的可以动手试一试
    2022-03-03
  • C#中使用ArrayPool和MemoryPool实例

    C#中使用ArrayPool和MemoryPool实例

    对资源的可复用是提升应用程序性能的一个非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它们就有效的减少了内存使用和对GC的压力,从而提升应用程序性能。感兴趣的可以了解一下
    2021-05-05
  • C#程序员应该养成的程序性能优化写法

    C#程序员应该养成的程序性能优化写法

    工作和生活中经常可以看到一些程序猿,写代码的时候只关注代码的逻辑性,而不考虑运行效率,其实这对大多数程序猿来说都是没有问题的,不过作为一只有理想的CodeMonkey,我还是希望给大家分享一些性能优化心得
    2017-08-08
  • C#中Sleep() 和 Wait()的区别小结

    C#中Sleep() 和 Wait()的区别小结

    Sleep()和 Wait()是两个不同的方法,用于控制线程的执行,本文主要介绍了C#中Sleep()和Wait()的区别小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-04-04
  • 基于c# 类、接口、结构的联系与区别详解

    基于c# 类、接口、结构的联系与区别详解

    本篇文章是对c#中类与接口以及结构的联系与区别进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • C#编程获取资源文件中图片的方法

    C#编程获取资源文件中图片的方法

    这篇文章主要介绍了C#编程获取资源文件中图片的方法,涉及C#针对项目中资源文件操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • unity实现贪吃蛇游戏

    unity实现贪吃蛇游戏

    这篇文章主要为大家详细介绍了unity实现贪吃蛇游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • C#探秘系列(二)——IsXXX 系列方法

    C#探秘系列(二)——IsXXX 系列方法

    VS是个大平台,当C#不好实现的时候,可以想想是否可以引用下其他语言下面的方法,或许你有大收获~
    2014-05-05
  • C#中事件只能在内部调用的原因分析

    C#中事件只能在内部调用的原因分析

    事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件,这篇文章主要介绍了C#中事件为什么只能在内部调用,需要的朋友可以参考下
    2021-11-11
  • 浅析C#线程本地存储中为什么线程间值不一样

    浅析C#线程本地存储中为什么线程间值不一样

    这篇文章主要想来和大家一起讨论一下C#线程本地存储中为什么线程间值不一样,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-01-01

最新评论