最近研究了两种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. [20190130]删除tab$记录的恢复.txt

    [20190130]删除tab$记录的恢复.txt --//网上提到许多删除tab$的案例,主要原因在于没有从官方正规渠道下载oracle版本,还有一些来自工具里面带有一些脚本删除tab$记录. -- ...

  2. windows下 jdk1.7安装教程图解

    java编程的初学者在开始编码前都会遇到一个难题,那就是jdk1.7环境变量配置怎么操作,怎么安装,针对这个难题,小编特地为大家整理相关教程,不了解的朋友可以前往查看使用. 软件说明 虽然现在jdk出 ...

  3. 实地探访:月薪 12K 的北京程序员是怎么工作生活的?

    阅读本文大概需要 4 分钟. “ 每个人都有自己的亲身经历,每个人都有一条生活道路.千万人千万条,各不相同,各有特点.但是并不是好人都有一条好路,坏人都有一条坏路,有的时候却恰恰相反.这虽然不是历史的 ...

  4. PLC

    https://www.cnblogs.com/dathlin/p/7469679.html C#读写三菱PLC和西门子PLC数据 使用TCP/IP 协议https://blog.csdn.net/x ...

  5. 【深度学习篇】--Seq2Seq模型从初识到应用

    一.前述 架构: 问题: 1.压缩会损失信息 2.长度会影响准确率 解决办法: Attention机制:聚焦模式 “高分辨率”聚焦在图片的某个特定区域并以“低分辨率”,感知图像的周边区域的模式.通过大 ...

  6. Kubernetes知识小普及

    大部分概念Kubernetes官网都有详细介绍,Kubernetes中文官网 https://kubernetes.io/zh/docs/tutorials/kubernetes-basics/ 官网 ...

  7. python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍

    目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...

  8. 用v-bind:style时的问题

    今天纠结了挺久一个问题,个人习惯是在HBuilder里先写好前端样式,在放.net去测试数据,但是发现一个问题 就是一个提示框跟随鼠标移动 提示框用v-bind:style绑定一个对象 DIV就是这句 ...

  9. .NET知识体系(转载)

    这篇文章为新手指明的学习方向: https://www.cnblogs.com/mcgrady/p/4725038.html 先生存后发展,先学会选择和使用知识和工具来做东西,然后在研究怎么做的再好一 ...

  10. HeadFirst设计模式读书笔记之工厂模式

    1. 简单工厂 1. 你开了一家披萨店,点披萨的方法可能是这样: public Pizza orderPizza(String type) { Pizza pizza; if (type.equals ...