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 ...
随机推荐
- mssql sqlserver 给已存在表添加新的字段及字段备注的方法
转自:http://www.maomao365.com/?p=8102 摘要: 下文讲述向已存在表上添加新字段及字段备注的方法,如下所示: 实验环境:sql server 2008 R2 1. 添加新 ...
- Windows API编程(SDK编程)配置VS2017——出现LNK 2019错误的win32项目如何解决
最近刚入门SDK编程,在 我终于知道为什么windowsApi学的人这么少了 这篇文章中,确实发现了这样的问题,我的教程使用VS2013->Windows桌面->win32,就诞生了能使用 ...
- falkonry
falkonry.com/ 2019 Top 100 AI companies in the world
- 如何让 Editplus 支持 SQL 语法高亮
editplus 用来编辑或查看一些常用程序源码都很方便,而且软件小巧,但是他原生不能支持对 SQL 文件的高亮显示,有点遗憾,但好在我们可以自定义这种高亮显示,那么要如何设置呢 1. 首先点击下载文 ...
- 【视频】Entity Framework Core 2.* 入门教程
视频专辑在B站上:https://www.bilibili.com/video/av34462368/ 内容暂时如下,还在更新中: 1. 简介 & 创建Model,生成数据库 2. 在ASP. ...
- .NET Core微服务之服务间的调用方式(REST and RPC)
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.REST or RPC ? 1.1 REST & RPC 微服务之间的接口调用通常包含两个部分,序列化和通信协议.常见的序列化 ...
- 【朝花夕拾】Android性能篇之(八)ANR篇--草稿
1.ANR概念 2.ANR发生场景 Android开发者官网 上说到了两个原因:(1)点击按键或者触摸屏幕等输入事件在5s内没有响应:(2)10s内没有完成广播事件.如下所示: Android wil ...
- ES 08 - 创建、查看、修改、删除、关闭Elasticsearch的index
目录 1 创建index(配置mapping[映射]) 2 查看index 3 修改index 4 删除index 5 打开/关闭index 6 常见问题及解决方法 index相当于RDBMS(关系型 ...
- docker~不使用yml批量部署服务
回到目录 有时,我们在进行持续集成环境有时,有时yml环境是没有的,它可能只提供了docker工具,而docker-compose这个大家伙可能不被提供,而这样我们如果希望自动化构建解决方案下所有的项 ...
- Scss预处理器的使用总结
变量 .嵌套.Mixin混合.function函数.插值 变量及文件导入 通过$定义变量 $white:#fff; $black:#000; 变量引用 .containner{ color:$blac ...