WPF實(shí)現(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
實(shí)現(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)化臨時(shí)顯示文件
/// </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 + "\\個(gè)人申請(qǐng)微信公眾號(hào)教程.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實(shí)現(xiàn)在線預(yù)覽和顯示W(wǎng)ord和PDF文件的文章就介紹到這了,更多相關(guān)WPF在線預(yù)覽文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
采用easyui tree編寫簡(jiǎn)單角色權(quán)限代碼的方法
本文主要介紹了如何采用easyui tree編寫簡(jiǎn)單角色權(quán)限代碼,文章思路清晰,需要的朋友可以參考下2015-07-07
C#實(shí)現(xiàn)文件上傳下載Excel文檔示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)文件上傳下載Excel文檔示例代碼,需要的朋友可以參考下2017-08-08
使用C#刪除Excel表格中的重復(fù)行數(shù)據(jù)的代碼詳解
重復(fù)行是指在Excel表格中完全相同的多行數(shù)據(jù),刪除這些重復(fù)行至關(guān)重要,因?yàn)樗鼈儾粌H會(huì)干擾數(shù)據(jù)分析,還可能導(dǎo)致錯(cuò)誤的決策和結(jié)論,所以本文給大家介紹了如何使用C#快速刪除Excel表格中的重復(fù)行數(shù)據(jù),需要的朋友可以參考下2025-05-05
C#中HslCommunication庫的實(shí)現(xiàn)示例
C# HslCommunication庫是一個(gè)用于建立TCP連接并進(jìn)行Modbus通訊的庫,使用該庫可以方便地建立TCP連接,并進(jìn)行讀寫操作,下面就來詳細(xì)的介紹一下,感興趣的可以了解一下2025-12-12
C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法
這篇文章主要介紹了C#實(shí)現(xiàn)winform自動(dòng)關(guān)閉MessageBox對(duì)話框的方法,實(shí)例分析了C#中MessageBox對(duì)話框的相關(guān)操作技巧,需要的朋友可以參考下2015-04-04
c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼
開發(fā)中有時(shí)會(huì)遇到要對(duì)文件進(jìn)行共享狀態(tài)的讀寫操作,代碼如下,需要的朋友可以參考下2012-06-06

