陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

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

2022年1月14日 1876点热度 0人点赞 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)
        {

        }
    }
}

 

标签: pdf WPF
最后更新:2022年1月14日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
python C# linux nginx centos js DevExpress winform
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
python版本的图片比较相似度 关于使用webBrowser操作HTML的帮助类 python 多线程和多进程 python操作隐藏Selenium登录网站的身份指纹 设置保持winform显示,不会因为分辨率和屏幕大小而变化 python 发送邮件

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS