使用WPF在线预览和显示Word和PDF文件

8 篇文章 0 订阅

在这里插入图片描述

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)
        {

        }
    }
}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值