设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间18 现在时间18 缓存数据 为什么现在的分手理由 基本都是不喜欢了 不合适 没未来 就他妈不肯承认自己喜欢上别人了 真是 怂

为什么现在的分手理由 基本都是不喜欢了 不合适 没未来 就他妈不肯承认自己喜欢上别人了 真是 怂 -- 只想对你说

查看: 1325|回复: 2

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

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:204
  • 打卡月天数:0
  • 打卡总奖励:3136
  • 最近打卡:2023-08-27 04:04:29
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
393
主题
369
精华
0
金钱
4286
积分
780
注册时间
2022-12-24
最后登录
2025-5-31

发表于 2024-2-25 12:02:59 | 显示全部楼层 |阅读模式
效果图
1.png

PDF和Word预览,可用于WPF和Winform。 原理是采用spire把word或者pdf文件转成xps文件,用documentViewer来呈现,并重新封装了显示的样子
WPF 需要引用的包,本地程序集
  1. ReachFramework spire.officeMicrosoft.Office.Interop.Word.dll
复制代码
spire把word或者pdf文件转成xps文件
word转化提供另外一种方法,采用引用Microsoft.Office.Interop.Word.dll
实现代码
  1. using Spire.Doc;
  2. using Spire.Pdf;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using System.Windows.Xps.Packaging;

  18. namespace WpfApp3
  19. {
  20.     /// <summary>
  21.     /// Main.xaml 的交互逻辑
  22.     /// </summary>
  23.     public partial class Main : Window
  24.     {
  25.         /// <summary>
  26.         /// 转化临时显示文件
  27.         /// </summary>
  28.         public string tempPdfPreAddress = Environment.CurrentDirectory + "\\tempPdfPre\";

  29.         /// <summary>
  30.         /// 统一读取
  31.         /// </summary>
  32.         XpsDocument readerDoc;

  33.         public Main()
  34.         {
  35.             InitializeComponent();

  36.             if (!Directory.Exists(tempPdfPreAddress))
  37.             {
  38.                 Directory.CreateDirectory(tempPdfPreAddress);
  39.             }
  40.         }

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

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

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

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

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

  63.         }

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

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

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

  76.         //private XpsDocument ConvertWordToXPS(string wordDocName)
  77.         //{
  78.         //    FileInfo fi = new FileInfo(wordDocName);
  79.         //    XpsDocument result = null;
  80.         //    string xpsDocName = System.IO.Path.Combine(tempPdfPreAddress, fi.Name);
  81.         //    xpsDocName = xpsDocName.Replace(".docx", ".xps").Replace(".doc", ".xps");
  82.         //    Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
  83.         //    try
  84.         //    {
  85.         //        if (!File.Exists(xpsDocName))
  86.         //        {
  87.         //            wordApplication.Documents.Add(wordDocName);
  88.         //            Microsoft.Office.Interop.Word.Document doc = wordApplication.ActiveDocument;
  89.         //            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);
  90.         //            result = new XpsDocument(xpsDocName, System.IO.FileAccess.Read);
  91.         //        }

  92.         //        if (File.Exists(xpsDocName))
  93.         //        {
  94.         //            result = new XpsDocument(xpsDocName, FileAccess.Read);
  95.         //        }

  96.         //    }
  97.         //    catch (Exception ex)
  98.         //    {
  99.         //        string error = ex.Message;
  100.         //        wordApplication.Quit(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
  101.         //    }

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

  103.         //    return result;
  104.         //}

  105.         private void Button_page_Click(object sender, RoutedEventArgs e)
  106.         {

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

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

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

  117.         private void Back_mouse_down(object sender, MouseButtonEventArgs e)
  118.         {

  119.         }
  120.     }
  121. }
复制代码
到此这篇关于WPF实现在线预览和显示Word和PDF文件的文章就介绍到这了,更多相关WPF在线预览文件内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

  • 打卡等级:偶尔看看
  • 打卡总天数:14
  • 打卡月天数:0
  • 打卡总奖励:175
  • 最近打卡:2025-03-19 19:23:26
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
204
积分
30
注册时间
2023-1-24
最后登录
2025-3-19

发表于 前天 01:06 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
21
积分
22
注册时间
2022-12-25
最后登录
2022-12-25

发表于 3 小时前 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表