WPF display PDF file (转)

http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/

 

 

Displaying a PDF file within a WPF application

April 6, 2009 — Christopher Hujanen

I had a need to view PDF files directly within a WPF application and Stackoverflow and Google did not provide much assistance, so I set out on my own to see what I could create.  It turns out, it is not that difficult.  The trick is to use the WinForms support in WPF ala the WindowsFormHost control.  The application is so simple, I built it start to finish in under 5 minutes and captured it in a screencast for you to check out.

WPF PDF viewer app

The code is very simple, here’s the whopping 2 lines that I changed from the default usercontrol template:

1public partial class UserControl1 : UserControl
2{
3     public UserControl1(string filename)
4     {
5          InitializeComponent();
6 
7          this.axAcroPDF1.LoadFile(filename);
8     }
9}

The only other code change I made is in creating an instance of the user control in the code behind of my WPF window:

1public Window1()
2{
3     InitializeComponent();
4 
5     // TODO: Make sure you point to a PDF on your system:
6     var uc = new UserControl1(@"C:\FoundationsOfProgramming.pdf");
7     this.windowsFormsHost1.Child = uc;
8}

The code is free for you to use: WpfPDFViewer.zip. Note it is a Visual Studio 2008 solution, and you must have Adobe Acrobat installed on your system of course.

 

 

标签: wpf

posted on 2009-12-17 16:22 逝去的时光 阅读(304) 评论(2)  编辑 收藏

评论

#1楼  回复 引用 查看   

在WPF里添加PDF不难,我遇见一个问题想向你请教一下:
添加PDF控件之后在VS2008里能够正常的编译运行,未出现任何问题,
但是在编译生成的Debug文件中直接运行.exe文件后,能正常打开,但在关闭时就有异常了。
这个问题不仅在WPF中出现,在Winform中添加PDF也出现,加载其他的控件则不出现这个问题。
不知道如何来解决
2010-01-09 12:06 | @编程浪子@      

#2楼  回复 引用 查看   

我也有这个问题,加载pdf文件在程序退出时就会出现异常,但如果仅仅是关闭pdf控件所在窗体而不退出整个程序则不会出现异常,哪位高手能解释一下,
2010-05-14 23:29 | dxhnkt      

导航

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

公告

昵称:逝去的时光
园龄:3年10个月
粉丝:0
关注:0

搜索

 
 

常用链接

我的标签

随笔档案(2)

文章分类(19)

文章档案(18)

最新评论