WPF:WPF显示PDF文档

简述

  软件的帮助文档可借助第三方软件如PDF Reader、Adobe PDF等显示,但客户机上需安装此类软件。WPF开发的软件可借助第三方库 MoonPdf将PDF文档加载显示到软件窗口中(Dll下载地址,GitHub源码地址)。

成都创新互联公司是一家专注于做网站、网站建设与策划设计,城西网站建设哪家好?成都创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:城西等地区。城西做网站价格咨询:028-86922220

MoonPdf库使用方式:

  1. 将MoonPdfLib.dll、libmupdf.dll、MouseKeyboardActivityMonitor.dll放置于WPF工程Bin文件下;
  2. 在项目工程中“引用”右键添加新引用:MoonPdfLib.dll;
  3. 在.xmal及.cs文件中添加相关引用,详见代码。

代码

PDFViewer.xaml


    
        
        
            
                
                
                
            
        

        
            
            
        
    

PDFViewer.cs

using MoonPdfLib; //记得引用此命名空间

public partial class PdfViewer : UserControl
    {
        private bool m_bLoaded = false;

        public PdfViewer()
        {
            InitializeComponent();
        }

        public bool LoadPdfDoc( string strPdfPath )
        {
            try
            {
                this.pdfViewer.OpenFile( strPdfPath );
                this.pdfViewer.Zoom( 1.0 );
                m_bLoaded = true;
            }
            catch
            {
                m_bLoaded = false;
            }

            return m_bLoaded;
        }

        private void ZoomToOrigin_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.Zoom( 1.0 );
        }

        private void SinglePage_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.ViewType = ViewType.SinglePage;
        }

        private void DoublePage_Click( object sender, RoutedEventArgs e )
        {
            if ( !m_bLoaded )
                return;
            this.pdfViewer.ViewType = ViewType.Facing;
        }
    }

调用PdfViewer,及HelpWin窗口类:HelpWin.xaml

    
    
        
            
            
        

        
        

        
        

    

HelpWin.cs

public partial class HelpWin : Window
    {
        private string m_strPdfPath = AppDomain.CurrentDomain.BaseDirectory + "help.pdf"; 

        public HelpWin()
        {
            InitializeComponent();
        }

        private void ImgClose_MouseDown( object sender, MouseButtonEventArgs e )
        {
            this.Close();
        }

        private void PDFView_Loaded( object sender, RoutedEventArgs e )
        {
            if ( !this.PDFView.LoadPdfDoc( m_strPdfPath ) )
            {
                MessageBox.Show( "打开帮助文档失败,请重试!" );
            }
        }
    }

效果

WPF:WPF显示PDF文档


分享名称:WPF:WPF显示PDF文档
网页路径:http://pwwzsj.com/article/pshoep.html