
离线 TA的专栏
- 打卡等级:热心大叔
- 打卡总天数:204
- 打卡月天数:0
- 打卡总奖励:3136
- 最近打卡:2023-08-27 04:04:29
|
效果图
PDF和Word预览,可用于WPF和Winform。 原理是采用spire把word或者pdf文件转成xps文件,用documentViewer来呈现,并重新封装了显示的样子
WPF 需要引用的包,本地程序集 - ReachFramework spire.officeMicrosoft.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在线预览文件内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|