最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2024年02月23日 14:45:42   作者:搬磚的詩人Z  
這篇文章主要為大家詳細(xì)介紹了如何使用WPF實(shí)現(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

實(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è)人申請微信公眾號教程.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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

三河市| 高台县| 句容市| 马边| 阿拉善右旗| 社会| 宁城县| 呼伦贝尔市| 运城市| 肃南| 喀喇沁旗| 定安县| 齐齐哈尔市| 东乌珠穆沁旗| 逊克县| 德兴市| 曲水县| 江孜县| 鄂托克旗| 同心县| 伽师县| 交口县| 越西县| 麟游县| 锦屏县| 丰城市| 子洲县| 琼中| 东平县| 黔西县| 乌兰浩特市| 滦南县| 富源县| 遂溪县| 都江堰市| 太谷县| 汽车| 玉龙| 同德县| 吴旗县| 沙雅县|