WPF:WPF显示PDF文档

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF:WPF显示PDF文档相关的知识,希望对你有一定的参考价值。

简述

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

MoonPdf库使用方式:

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

代码

PDFViewer.xaml

<UserControl x:Class="NovelRPS.PdfViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
             mc:Ignorable="d" 
             >
    <Grid>
        <!-- ContextMenu -->
        <Grid.ContextMenu>
            <ContextMenu>
                <MenuItem x:Name="ZoomToOrigin" Header="100%" Click="ZoomToOrigin_Click"></MenuItem>
                <MenuItem x:Name="SinglePage" Header="单页" Click="SinglePage_Click"></MenuItem>
                <MenuItem x:Name="DoublePage" Header="双页" Click="DoublePage_Click"></MenuItem>
            </ContextMenu>
        </Grid.ContextMenu>

        <Border>
            <!-- 参考:https://www.cnblogs.com/yang-fei/p/4885570.html -->
            <mpp:MoonPdfPanel x:Name="pdfViewer" MaxZoomFactor="2.5" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True"></mpp:MoonPdfPanel>
        </Border>
    </Grid>
</UserControl>

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

<Window x:Class="NovelRPS.HelpWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NovelRPS"
        Title="HelpWin" Background="#FF212121" WindowState="Maximized" Topmost="True" WindowStyle="None" ResizeMode="NoResize">    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="66"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>

        <!-- Title -->
        <Label x:Name="Title" Grid.Row="0" Background="#FF414141" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="0">
            <DockPanel>
                <Image DockPanel.Dock="Left" Source="Images/SubPageLogo.png"></Image>
                <Image x:Name="ImgClose" DockPanel.Dock="Right" Source="Images/Close.png" MouseDown="ImgClose_MouseDown"></Image>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Image Source="Images/Setting/Help.png" Width="30"></Image>
                    <Label Content="帮助" FontSize="26"  FontWeight="Bold"></Label>
                </StackPanel>
            </DockPanel>
        </Label>

        <!-- Content -->
        <Label Grid.Row="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <local:PdfViewer x:Name="PDFView" Loaded="PDFView_Loaded"/>
        </Label>

    </Grid>
</Window>

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中怎么显示PPT文件

在C#/ WPF应用程序中创建LaTeX文档

采用WPF技术,开发OFD电子文档阅读器

pdf文件内容查看器 -- 采用wpf开发

秀!微软《550页图解.Net+WPF完整版》.pdf 附下载!

保存为PDF时,WPF DataGrid GridLines不可见