WPF 浏览PDF 文件

简介:

 很长时间没写文章感觉手有点生了,前段时间忙的要死公事、家事、私事,事事操心。还好现在有些时间可以继续写博客了。本篇将为大家演示如何在WPF 程序中浏览PDF 文件,本例将通过Adobe PDF Reader COM 组件、WinForm 与WPF 集成方面的工具实现PDF 浏览功能。

用户控件

     打开VS2010,新建项目(WpfPDFReader),右键项目添加User Control(用户控件)。因为Adobe PDF Reader COM 组件是不支持WPF的,为此我们需要将它放到WinForm 控件中。所以,在列表中需要选择User Control,而不是User Control(WPF)。这里我将控件命名为:AdobeReaderControl.cs。完成添加双击控件进入设计模式。

AddUserCtl

在工具箱里选择添加组件,在COM 组件列表中点选“Adobe PDF Reader”。

SelectCom

AcroPDFLib 和AxAcroPDFLib 库会自动添加到项目中。

Reference

添加成功后会在工具箱里看到下图所示的控件。

Toolbox

将该COM 控件拖入User Control 。

AddCom

控件默认名称为:axAcroPDF1,可按需要自行更改。

ComName

Dock属性设置为“Fill”,这样可以使控件自动适应窗口尺寸。

Dock

打开控件程序,修改构造函数。将PDF 文件传入控件并进行加载。

using System.Windows.Forms;

namespace WpfPDFReader
{
    public partial class AdobeReaderControl : UserControl
    {
        public AdobeReaderControl(string fileName)
        {
            InitializeComponent();

            this.axAcroPDF1.LoadFile(fileName);
        }
    }
}

到此用户控件就基本完成了,下面开始WPF 部分的开发。

WPF

由于要将上面的WinForm 控件加载到WPF 程序中,所以先要为WPF 添加WindowsFormsIntegration。

WinForm

     打开XAML 在<Grid> 中添加Button 和WindowsFormsHost 控件,其中Button 用来启动文件目录窗口,从中选择要浏览的PDF文件;WindowsFormsHost 则用于嵌入WinForm 控件。

<Window x:Class="WpfPDFReader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF PDF Reader" Height="350" Width="525">
    <Grid>
        <Button Content="Open File" Click="Button_Click" Width="100" Height="30" 
                VerticalContentAlignment="Center" VerticalAlignment="Top" 
                Margin="0,10,0,0"/>
        <WindowsFormsHost x:Name="winFormHost" Margin="0,46,0,0" />
    </Grid>
</Window>

     下面来完成Button 点击事件,将通过OpenFileDialog 选择的PDF 文件路径及名称传入AdobeReaderControl 用户控件中,并将该控件添加到WindowsFormsHost。

private string openFileName;
private OpenFileDialog openFileDialog;

private void Button_Click(object sender, RoutedEventArgs e)
{
    openFileDialog = new OpenFileDialog();
    openFileDialog.DefaultExt = "pdf";
    openFileDialog.Filter = "pdf files (*.pdf)|*.pdf";

    DialogResult result = openFileDialog.ShowDialog();

    if (result == System.Windows.Forms.DialogResult.OK)
    {
        openFileName = openFileDialog.FileName;

        AdobeReaderControl pdfCtl = new AdobeReaderControl(openFileName);
        winFormHost.Child = pdfCtl;                
    }
    else
    {
        return;
    }
}

F5看下效果,点击“Open File” 选择一个PDF ,这样一个简单的WPF PDF Reader 就完成了。

Reader

源代码

WpfPDFReader.zip





本文转自Gnie博客园博客,原文链接:http://www.cnblogs.com/gnielee/archive/2010/11/16/wpf-pdf-reader.html,如需转载请自行联系原作者

相关文章
|
26天前
|
Web App开发 Windows
【Windows】 chrome 如何下载网站在线预览PDF文件,保存到本地
【Windows】 chrome 如何下载网站在线预览PDF文件,保存到本地
134 0
|
1月前
|
前端开发
开发过程中遇到过的docx、pptx、xlsx、pdf文件预览多种方式
开发过程中遇到过的docx、pptx、xlsx、pdf文件预览多种方式
17 0
|
2月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
119 0
|
2月前
|
存储 缓存 Python
如何使用Python抓取PDF文件并自动下载到本地
如何使用Python抓取PDF文件并自动下载到本地
37 0
|
1月前
|
JSON 关系型数据库 数据库
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
|
1月前
|
JSON 关系型数据库 数据库
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
【python】Python将100个PDF文件对应的json文件存储到MySql数据库(源码)【独一无二】
|
3月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
70 1
|
9天前
|
弹性计算 运维 Shell
|
9天前
|
JSON 监控 JavaScript
【LLM】基于LLama构建智能助理实现与PDF文件智能对话
【4月更文挑战第12天】构建智能助理服务,实现与PDF的自由对话
|
15天前
|
Python
python html(文件/url/html字符串)转pdf
python html(文件/url/html字符串)转pdf
9 0