WPF實現(xiàn)在線預(yù)覽和顯示W(wǎng)ord和PDF文件
效果圖
PDF和Word預(yù)覽,可用于WPF和Winform。 原理是采用spire把word或者pdf文件轉(zhuǎn)成xps文件,用documentViewer來呈現(xiàn),并重新封裝了顯示的樣子
WPF 需要引用的包,本地程序集
ReachFramework spire.office
Microsoft.Office.Interop.Word.dll
spire把word或者pdf文件轉(zhuǎn)成xps文件
word轉(zhuǎn)化提供另外一種方法,采用引用Microsoft.Office.Interop.Word.dll
實現(xiàn)代碼
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> /// 轉(zhuǎn)化臨時顯示文件 /// </summary> public string tempPdfPreAddress = Environment.CurrentDirectory + "\\tempPdfPre\\"; /// <summary> /// 統(tǒng)一讀取 /// </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) { } } }
到此這篇關(guān)于WPF實現(xiàn)在線預(yù)覽和顯示W(wǎng)ord和PDF文件的文章就介紹到這了,更多相關(guān)WPF在線預(yù)覽文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法
工作和生活中經(jīng)??梢钥吹揭恍┏绦蛟?寫代碼的時候只關(guān)注代碼的邏輯性,而不考慮運行效率,其實這對大多數(shù)程序猿來說都是沒有問題的,不過作為一只有理想的CodeMonkey,我還是希望給大家分享一些性能優(yōu)化心得2017-08-08C#中Sleep() 和 Wait()的區(qū)別小結(jié)
Sleep()和 Wait()是兩個不同的方法,用于控制線程的執(zhí)行,本文主要介紹了C#中Sleep()和Wait()的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2024-04-04基于c# 類、接口、結(jié)構(gòu)的聯(lián)系與區(qū)別詳解
本篇文章是對c#中類與接口以及結(jié)構(gòu)的聯(lián)系與區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06