需要打开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了,非常不错。

感谢您的阅读。

WPF中查看PDF文件的更多相关文章

  1. 4:WPF中查看PDF文件

    引用连接:https://www.cnblogs.com/yang-fei/p/4885570.html 在Github上看到一个非常好的WPF中承载PDF文件的类库. https://github. ...

  2. WPF中查看PDF文件之MoonPdfLib类库

    最近研究了两种PDF文件查看器,MoonPdfLib或者AdobeReader. 今天先说第一种,在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件. 在Sourceforge上下载到Mo ...

  3. WPF中查看PDF文件 - 基于开源的MoonPdfPanel (无需安装任何PDF阅读器)问题汇总

    致敬Yang-Fei—有一个简单的用法示例: http://www.cnblogs.com/yang-fei/p/4885570.html. 写MoonPdfPanel的哥们关于这个开源软件的实现介绍 ...

  4. IOS下载查看PDF文件(有下载进度)

    IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...

  5. 【转载】Chrome 0day漏洞:不要用Chrome查看pdf文件

    英文原文地址:https://blog.edgespot.io/2019/02/edgespot-detects-pdf-zero-day-samples.html 中文原文地址:https://ww ...

  6. 怎么用OCR图文识别软件在MS Office中创建PDF文件

    ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以从纸质文档.图像文件和任何其他流行格式创建PDF文件(相关文章请参考如何从 ...

  7. [置顶] 如何在浏览器中打开PDF文件并实现预览的思路与代码

    编写项目遇到一个需要在浏览器中打开PDF文件的问题.最终实现效果如下: 其实也就是简单的在浏览器中实现一个打开pdf文件,并有类似预览功能的边框. 其实在网上经常见到类似的页面,在浏览器中打开pdf文 ...

  8. web中浏览PDF文件

    1.在web中浏览pdf文件. 2.支持大多数主流浏览器,包括IE8 3.参考网址: https://pdfobject.com/ http://mozilla.github.io/pdf.js/ & ...

  9. Linux中查看各文件夹大小命令:du -h --max-depth=1

    Linux中查看各文件夹大小命令:du -h --max-depth=1 du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-siz ...

随机推荐

  1. JavaScript Coding 模式荟萃

    1.自运行的匿名函数 <script type="text/javascript" src="./js/jquery-1.7.2.js"></ ...

  2. bootstrap常用的定义风格

    primary()     首选项 success()     成功 info()        一般信息 warning()       警告 danger()         危险 <bod ...

  3. bootstrap框架-----可见 隐藏

    可见框架-像素选择 -block-inline  :块内联元素 -inline-block将对象呈递为内联对象,但是对象的内容作为块对象呈递.旁边的内联对象会被呈递在同一行内,允许空格 可以设置宽度和 ...

  4. Range Sum Query 2D - Mutable & Immutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...

  5. Zigzag Iterator

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  6. 抓取网页内容生成kindle电子书

    参考: http://calibre-ebook.com/download_linux http://blog.codinglabs.org/articles/convert-html-to-kind ...

  7. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

  8. web.config中配置页面出错后跳转指定错误页面

    每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...

  9. codeforces 567D.One-Dimensional Battle Ships 解题报告

    题目链接:http://codeforces.com/problemset/problem/567/D 题目意思:给出 1 * n 的 field,编号从左至右依次为 1,2,...,n.问射 m 枪 ...

  10. LeetCode 242 Valid Anagram

    Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...