需要打开PDF文件时,我们第一印象就是使用Adobe Reader。在开发中,经常会遇到需要展示PDF文件的需求。我们会借助于Adobe Reader的Active控件来实现。不过这需要客户的机器上安装有Adobe Reader,并且使用Adobe Reader的Active控件会在顶部有一个隐藏不了的工具栏。这样显然不合理。
在Github上看到一个非常好的WPF中承载PDF文件的类库。
https://github.com/reliak/moonpdf
使用非常简单,本篇博客将介绍使用MoonPdf类库来展示PDF文件。在 Sourceforge 上下载到MoonPdf编译好的Dll。下面通过一个WPF程序来演示,
1. 工程中添加对MoonPdfLib.dll的引用,另外记得将libmupdf.dll放置于WPF工程Bin文件下。
2. 代码:
XAML:
<Window x:Class="WPFPdfViewer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib" xmlns:local="clr-namespace:WPFPdfViewer" mc:Ignorable="d" Title="WPF Pdf Viewer" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Border Background="#f0f0f0"> <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> <Button Content="文件" Width="50" Click="FileButton_Click" Margin="5,5"/> <Button Content="放大" Width="50" Click="ZoomInButton_Click" Margin="0,5"/> <Button Content="缩小" Width="50" Click="ZoomOutButton_Click" Margin="5,5"/> <Button Content="100%" Width="50" Click="NormalButton_Click" Margin="0,5"/> <Button Content="整页" Width="50" Click="FitToHeightButton_Click" Margin="5,5"/> <Button Content="单页" Width="50" Click="SinglePageButton_Click" Margin="0,5"/> <Button Content="双页" Width="50" Click="FacingButton_Click" Margin="5,5"/> </StackPanel> </Border> <Border Background="#d3d3d3" Grid.Row="1"> <mpp:MoonPdfPanel x:Name="moonPdfPanel" Background="LightGray" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True"/> </Border> </Grid> </Window>
C#:
public partial class MainWindow : Window { private bool _isLoaded = false; public MainWindow() { InitializeComponent(); } private void FileButton_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog(); if (dialog.ShowDialog().GetValueOrDefault()) { string filePath = dialog.FileName; try { moonPdfPanel.OpenFile(filePath); _isLoaded = true; } catch (Exception) { _isLoaded = false; } } } private void ZoomInButton_Click(object sender, RoutedEventArgs e) { if(_isLoaded) { moonPdfPanel.ZoomIn(); } } private void ZoomOutButton_Click(object sender, RoutedEventArgs e) { if(_isLoaded) { moonPdfPanel.ZoomOut(); } } private void NormalButton_Click(object sender, RoutedEventArgs e) { if(_isLoaded) { moonPdfPanel.Zoom(1.0); } } private void FitToHeightButton_Click(object sender, RoutedEventArgs e) { moonPdfPanel.ZoomToHeight(); } private void FacingButton_Click(object sender, RoutedEventArgs e) { moonPdfPanel.ViewType = MoonPdfLib.ViewType.Facing; } private void SinglePageButton_Click(object sender, RoutedEventArgs e) { moonPdfPanel.ViewType = MoonPdfLib.ViewType.SinglePage; } }
3. 运行程序,打开PDF文件,
代码点击 这里 下载。
MoonPdf的原理是将Pdf文件转成了图片文件,然后再显示出来的。MoonPdf的开发者在 CodePlex 上的这篇文章介绍了MoonPdf了,非常不错。
感谢您的阅读。