最近研究了两种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类库的更多相关文章

  1. WPF中查看PDF文件

    需要打开PDF文件时,我们第一印象就是使用Adobe Reader.在开发中,经常会遇到需要展示PDF文件的需求.我们会借助于Adobe Reader的Active控件来实现.不过这需要客户的机器上安 ...

  2. 4:WPF中查看PDF文件

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

  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. bat脚本以管理员身份运行时,目录没有定位到当前目录问题的解决方法

    我们时常使用bat脚本去处理一些事情,有事bat中的命令需要以管理员的身份运行,但是运行后文件的目录自动定位到了C:\Windows\system32下,而不再脚本所在的目录下. 这是我的文件目录,W ...

  2. kodi18.1设置中文的方法

    默认情况下,新安装好的 Kodi 启动后是英文版的界面.但事实上,Kodi 包含多国语言 (包含简繁体中文版),我们可以设置改回简体中文界面.不过,很多人都遇到修改 Kodi 的语言为中文之后整个界面 ...

  3. 第二章.python入门

    2.1环境的安装 解释器:py2和py3 添加环境变量的作用:便于找到python解释器 开发工具:pycharm 2.2编码 2.2.1编码基础 ascii:只表示英文,8位表示一个元素,pytho ...

  4. docker 备注

    1.docker 安装 #安装环境为centos yum -y install docker service docker start #测试是否安装成功,可执行命令 docker run hello ...

  5. mysql性能优化之数据库级别优化--优化sql语句

    一  优化SELECT语句 1.1 WHERE子句优化 本文暂时只讨论可以处理WHERE子句的优化,下面的一些实例使用SELECT语句,但是相同的优化同样适用DELETE和UPDATE语句中的WHER ...

  6. 学习pwn的前提工作及部分解决方案

    一.Ubuntu 在VM安装64位的Ubuntu 二.pwntools 基本语法 sudo apt-get install libffi-dev sudo apt-get install libssl ...

  7. Python3+Flask+uwsgi部署

    python3 按照常规的方式安装即可: wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tgz tar zxvf Python-3 ...

  8. 【最短路径Floyd算法详解推导过程】看完这篇,你还能不懂Floyd算法?还不会?

    简介 Floyd-Warshall算法(Floyd-Warshall algorithm),是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似.该算法名称以 ...

  9. @vue-cli3安装element组件过程

    在创建好的项目目录下 执行: vue add element 如图选择对应的选项 安装成功!KO!

  10. mysql_8.0.12环境配置

    1. 官网下载mysql_8.0.12免安装包,解压到你存放的地方: 2. 配置环境变量(把bin的文件夹弄进系统path里面): 3. 在解压的根路径中,查看是否含有my.ini文件,没有就新建一个 ...