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 ...
随机推荐
- 关于wordpress升级遇到的问题
前几天登录我的博客后台,发现可以升级了,顺手点了一下,但顺接出现了几个报错,然后这里记录一下解决方法: 1.出现“另一更新正在进行”的错误解决: 这是因为可能你上次更新失败了,或者WordPress在 ...
- Luogu P5309 [Ynoi2012]D1T1
小清新分块题,从头到尾都十分套路,和CXR大佬一起YY了一下就出来了 首先套路地设个阈值\(S\),把修改的\(x\)分成大于\(S\)和小于等于\(S\)两部分,然后我们考虑分别求解这两部分 \(x ...
- Python算法练习--把搜索树转成双向链表
本文目前分享的题目都是来自于July的分享,然后把具体算法实现.搜索树转双向链表主要的实现逻辑是在中序遍历时,调整节点的左右子树:因为中序遍历是递归调用,所以在调整时一定要注意调整的位置,如果写错了, ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- shell脚本获取进程ID并杀死的实现及问题解析
经常需要杀死某个进程,操作了几次之后,对一个熟练的码农来说,就要制作自己的工具了.有些工具虽然很小,但是却能节省一大部分的时间. 输入某个进程的ID并杀死的方法.这种事情,一般是先搜索再进行优化,这种 ...
- 中文分词实战——基于jieba动态加载字典和调整词频的电子病历分词
分词是自然语言处理中最基本的一个任务,这篇小文章不介绍相关的理论,而是介绍一个电子病历分词的小实践. 开源的分词工具中,我用过的有jieba.hnlp和stanfordnlp,感觉jieba无论安装和 ...
- webstorm使用问题总结
webstorm 打断点调试配置 run/edit configurations中,把nodejs里的历史都删除:然后打开Defaults/Node.js将node的可执行程序放到Node inter ...
- .NET Core微服务之基于MassTransit实现数据最终一致性(Part 2)
Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.案例结构与说明 在上一篇中,我们了解了MassTransit这个开源组件的基本用法,这一篇我们结合一个小案例来了解在ASP.NET C ...
- springcloud~Eureka实例搭建
服务端 build.gradle配置 dependencies { compile('org.springframework.cloud:spring-cloud-starter-netflix-eu ...
- Pi Hybrids问题
Pi Hybrids问题 清华大学肖秀波梁湧老师翻译的Rardin教授的<运筹学>[1]已于今年年中出版,感谢机械工业出版社张有利老师的推荐和赠书,让我能看到如此完美的千页级宏篇译著.该书 ...