WPF中查看PDF文件之MoonPdfLib类库
最近研究了两种PDF文件查看器,MoonPdfLib或者AdobeReader。
今天先说第一种,在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件。
在Sourceforge上下载到MoonPdf编译好的Dll。
https://github.com/reliak/moonpdf这是源码地址。
使用非常简单,废话不多说,上码上步骤。
1、程中添加对MoonPdfLib.dll的引用。注意:将libmupdf.dll放置于WPF工程Bin文件下。
2、XMAL源码:
<Grid>
<Border Background="#f0f0f0">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<Button Content="上传" Width="95" Click="FileButton_Click" Margin="5,5,5,375"/>
<Button Content="放大" Width="105" Click="ZoomInButton_Click" Margin="0,5,0,375"/>
<Button Content="缩小" Width="95" Click="ZoomOutButton_Click" Margin="5,5,5,375"/>
<Button Content="100%" Width="95" Click="NormalButton_Click" Margin="0,5,0,375"/>
<Button Content="整页" Width="100" Click="FitToHeightButton_Click" Margin="5,5,5,375"/>
<Button Content="单页" Width="90" Click="SinglePageButton_Click" Margin="0,5,0,375"/>
<Button Content="双页" Width="90" Click="FacingButton_Click" Margin="5,5,5,375"/>
</StackPanel>
</Border>
<Grid HorizontalAlignment="Left" Height="340" Margin="10,70,0,0" VerticalAlignment="Top" Width="155">
<TreeView x:Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Childern}">
<TextBlock Name="text1">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}">
<Binding x:Name="name1" Path="Name" />
<Binding x:Name="name2" Path="FilePath" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
<Border Background="#d3d3d3" Margin="170,70,0,0">
<mpp:MoonPdfPanel x:Name="moonPdfPanel" Background="LightGray" ViewType="SinglePage" PageRowDisplay="ContinuousPageRows" PageMargin="0,2,4,2" AllowDrop="True"/>
</Border>
</Grid>
3、C#代码:
private bool _isLoaded = false;
public MainWindow()
{
InitializeComponent(); //数据库要存放的路径
string filePath = "\\File\\ExceptionHandlingVSTO.pdf";
//获取文件名字
string fileName = Path.GetFileName(filePath); TreeModel loc = new TreeModel()
{
Name = "施工前",
Childern = new List<TreeModel>()
{
new TreeModel()
{
Name = fileName,
FilePath = filePath
},
new TreeModel()
{
Name ="测试节点可以无视",
FilePath = filePath
}
}
};
List<TreeModel> list = new List<TreeModel>() { loc };
treeView.ItemsSource = list; }
private void FileButton_Click(object sender, RoutedEventArgs e)
{
form.OpenFileDialog dialog = new form.OpenFileDialog();
dialog.Multiselect = true;
if (dialog.ShowDialog() == form.DialogResult.OK)
{
string[] filesPath = dialog.FileNames; if (filesPath != null && filesPath.Length > )
{
foreach (var filePath in filesPath)
{
Upload(filePath);
}
}
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="filePath">文件原来的路径</param>
public void Upload(string filePath)
{
try
{
FileInfo info = new FileInfo(filePath); string directory = Directory.GetCurrentDirectory();
string uploadFile = $"{directory}\\File";
//文件要上传的路径
string uploadPath = $"{uploadFile}\\{info.Name}"; WebClient wc = new WebClient();
//wc.Credentials = new NetworkCredential("xuyue", "111111");
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] result = br.ReadBytes(Convert.ToInt32(fs.Length));
Stream s = wc.OpenWrite(new Uri(uploadPath), "PUT"); if (s.CanWrite)
{
s.Write(result, , result.Length);
}
s.Flush();
s.Close();
fs.Close(); }
catch (Exception ex)
{
throw ex;
} } 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;
} private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//数据库要存放的路径
string fileName = "\\File\\ExceptionHandlingVSTO.pdf";
string filePath = $"{Directory.GetCurrentDirectory()}{fileName}"; try
{
moonPdfPanel.OpenFile(filePath);
_isLoaded = true;
}
catch (Exception ex)
{
_isLoaded = false;
}
}
}
WPF中查看PDF文件之MoonPdfLib类库的更多相关文章
- WPF中查看PDF文件
需要打开PDF文件时,我们第一印象就是使用Adobe Reader.在开发中,经常会遇到需要展示PDF文件的需求.我们会借助于Adobe Reader的Active控件来实现.不过这需要客户的机器上安 ...
- 4:WPF中查看PDF文件
引用连接:https://www.cnblogs.com/yang-fei/p/4885570.html 在Github上看到一个非常好的WPF中承载PDF文件的类库. https://github. ...
- WPF中查看PDF文件 - 基于开源的MoonPdfPanel (无需安装任何PDF阅读器)问题汇总
致敬Yang-Fei—有一个简单的用法示例: http://www.cnblogs.com/yang-fei/p/4885570.html. 写MoonPdfPanel的哥们关于这个开源软件的实现介绍 ...
- IOS下载查看PDF文件(有下载进度)
IOS(object-c) 下载查看 PDF 其实还是蛮容易操作的.在下载前,首先要把 IOS 可以保存文件的目录给过一遍: IOS 文件保存目录 IOS 可以自定义写入的文件目录,是很有限的,只能是 ...
- 【转载】Chrome 0day漏洞:不要用Chrome查看pdf文件
英文原文地址:https://blog.edgespot.io/2019/02/edgespot-detects-pdf-zero-day-samples.html 中文原文地址:https://ww ...
- 怎么用OCR图文识别软件在MS Office中创建PDF文件
ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以从纸质文档.图像文件和任何其他流行格式创建PDF文件(相关文章请参考如何从 ...
- [置顶] 如何在浏览器中打开PDF文件并实现预览的思路与代码
编写项目遇到一个需要在浏览器中打开PDF文件的问题.最终实现效果如下: 其实也就是简单的在浏览器中实现一个打开pdf文件,并有类似预览功能的边框. 其实在网上经常见到类似的页面,在浏览器中打开pdf文 ...
- web中浏览PDF文件
1.在web中浏览pdf文件. 2.支持大多数主流浏览器,包括IE8 3.参考网址: https://pdfobject.com/ http://mozilla.github.io/pdf.js/ & ...
- Linux中查看各文件夹大小命令:du -h --max-depth=1
Linux中查看各文件夹大小命令:du -h --max-depth=1 du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-siz ...
随机推荐
- 「工具」Aquarelo - 来自意大利的色阶管理工具
Aquarelo是一款小而美的色阶管理工具,由意大利团队开发,包含三个核心功能,可用于色彩管理.色彩搭配等场景. ## 相关参数 * 操作系统:macOS* 工具官网:[Aquarelo for Ma ...
- Redux进阶(一)
State的不可变化带来的麻烦 在用Redux处理深度复杂的数据时会有一些麻烦.由于js的特性,我们知道当对一个对象进行复制时实际上是复制它的引用,除非你对这个对象进行深度复制.Redux要求你每次你 ...
- 一篇读懂HTTPS:加密原理、安全逻辑、数字证书等
1.引言 HTTPS(全称: Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.本文,就来深入介绍下其 ...
- 一句话HTML编辑器
一句话HTML编辑器 data:text/html,<body oninput="i.srcdoc=h.value"><style>#i{width:70% ...
- 使用 shell / python 进行sql的excel报表导出
如果要求你进行一个表数据的导出,如果使用shell的话,很容易做到,即执行一下 select 语句就可以拿到返回结果了! 如下: /usr/bin/mysql -u"${username}& ...
- 第3章 简单的C程序设计——顺序程序设计
3.1 顺序程序设计举例 例:有人用温度计测量出用华氏法表示的温度(如64F),今要求把它转换为一摄氏法表示的温度(如17.8C) 解题思路:问题的关键在于找到两者的转换公式.根据物理学知识,公式为c ...
- [Abp 源码分析]零、文章目录
0.系列文章目录 一.Abp 框架启动流程分析 二.模块系统 三.依赖注入 四.模块配置 五.系统设置 六.工作单元的实现 七.仓储与 Entity Framework Core 八.缓存管理 九.事 ...
- Castle Windsor 的动态代理类如何获取实际类型
问题 在实际开发过程当中我们可能会针对某些类型使用动态代理技术(AOP),注入了一些拦截器进行处理,但是一旦某个类型被动态代理了,那么就会生成一个代理类.这个时候在该类内部使用 GetType() 方 ...
- Android-PickerView【仿iOS的PickerView控件,并封装了时间选择和选项选择这两种选择器】使用
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本文主要演示Android-PickerView的选项选择器.时间选择器的简单运用.由于每一个版本略有不用,所以实际使用方式以git ...
- js实现二分查找算法
二分查找:是一种搜索某个值的索引的算法. 基本条件:有序的数组. 思路:1.将数组折半,分成左右两个数组. 2.判断要查找的数和中间位置数值的大小,来判断要查找的数实在哪一半. 3.之后继续折半查找, ...