WPF开发随笔收录-查看PDF文件
一、前言
在项目的开发过程中,涉及到查看服务器生成的pdf报告文件的查看,起初的方案是通过spire.pdf这个库来将pdf文件转换成图片,然后在进行查看。但是经常被吐槽预览不清晰,后面上网发现了一个专门的pdf预览库,能够清晰的查看pdf文件,同时也支持放大缩小等功能;
二、正文
1、查看pdf文件所用到的库是MoonPdf类库,源码地址:https://github.com/reliak/moonpdf
2、使用方法也很简单,项目中添加对MoonPdfLib.dll的引用,然后记得把libmupdf.dll拷贝到项目的bin目录的debug或者release目录下
3、这里直接贴出网上找的例子的代码,实测可用
<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:local="clr-namespace:WPFPdfViewer" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib" Title="WPF Pdf Viewer" Width="525" Height="350" mc:Ignorable="d"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border Background="#f0f0f0"> <StackPanel HorizontalAlignment="Left" Orientation="Horizontal"> <Button Width="50" Margin="5,5" Click="FileButton_Click" Content="文件" /> <Button Width="50" Margin="0,5" Click="ZoomInButton_Click" Content="放大" /> <Button Width="50" Margin="5,5" Click="ZoomOutButton_Click" Content="缩小" /> <Button Width="50" Margin="0,5" Click="NormalButton_Click" Content="100%" /> <Button Width="50" Margin="5,5" Click="FitToHeightButton_Click" Content="整页" /> <Button Width="50" Margin="0,5" Click="SinglePageButton_Click" Content="单页" /> <Button Width="50" Margin="5,5" Click="FacingButton_Click" Content="双页" /> </StackPanel> </Border> <Border Grid.Row="1" Background="#d3d3d3"> <mpp:MoonPdfPanel x:Name="moonPdfPanel" AllowDrop="True" Background="LightGray" PageMargin="0,2,4,2" PageRowDisplay="ContinuousPageRows" ViewType="SinglePage" /> </Border> </Grid> </Window>
using Microsoft.Win32; using System; using System.Windows; using MoonPdfLib.MuPdf; namespace WPFPdfViewer { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> 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; } } }