欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF實現(xiàn)在線預(yù)覽和顯示W(wǎng)ord和PDF文件

 更新時間:2024年02月23日 14:45:42   作者:搬磚的詩人Z  
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實現(xiàn)在線預(yù)覽和顯示W(wǎng)ord和PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

效果圖

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)文章

  • 基于Unity3D實現(xiàn)3D迷宮小游戲的示例代碼

    基于Unity3D實現(xiàn)3D迷宮小游戲的示例代碼

    迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實現(xiàn)一個3D版的迷宮小游戲,感興趣的可以動手試一試
    2022-03-03
  • C#中使用ArrayPool和MemoryPool實例

    C#中使用ArrayPool和MemoryPool實例

    對資源的可復(fù)用是提升應(yīng)用程序性能的一個非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內(nèi)存使用和對GC的壓力,從而提升應(yīng)用程序性能。感興趣的可以了解一下
    2021-05-05
  • C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法

    C#程序員應(yīng)該養(yǎng)成的程序性能優(yōu)化寫法

    工作和生活中經(jīng)??梢钥吹揭恍┏绦蛟?寫代碼的時候只關(guān)注代碼的邏輯性,而不考慮運行效率,其實這對大多數(shù)程序猿來說都是沒有問題的,不過作為一只有理想的CodeMonkey,我還是希望給大家分享一些性能優(yōu)化心得
    2017-08-08
  • C#中Sleep() 和 Wait()的區(qū)別小結(jié)

    C#中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ū)別詳解

    本篇文章是對c#中類與接口以及結(jié)構(gòu)的聯(lián)系與區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • C#編程獲取資源文件中圖片的方法

    C#編程獲取資源文件中圖片的方法

    這篇文章主要介紹了C#編程獲取資源文件中圖片的方法,涉及C#針對項目中資源文件操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • unity實現(xiàn)貪吃蛇游戲

    unity實現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了unity實現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#探秘系列(二)——IsXXX 系列方法

    C#探秘系列(二)——IsXXX 系列方法

    VS是個大平臺,當(dāng)C#不好實現(xiàn)的時候,可以想想是否可以引用下其他語言下面的方法,或許你有大收獲~
    2014-05-05
  • C#中事件只能在內(nèi)部調(diào)用的原因分析

    C#中事件只能在內(nèi)部調(diào)用的原因分析

    事件(Event)?基本上說是一個用戶操作,如按鍵、點擊、鼠標(biāo)移動等等,或者是一些提示信息,如系統(tǒng)生成的通知。應(yīng)用程序需要在事件發(fā)生時響應(yīng)事件,這篇文章主要介紹了C#中事件為什么只能在內(nèi)部調(diào)用,需要的朋友可以參考下
    2021-11-11
  • 淺析C#線程本地存儲中為什么線程間值不一樣

    淺析C#線程本地存儲中為什么線程間值不一樣

    這篇文章主要想來和大家一起討論一下C#線程本地存儲中為什么線程間值不一樣,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01

最新評論