在WPF应用程序中显示PDF

aza*_*arp 26 pdf wpf

有关如何在WPF Windows应用程序中显示PDF文件的任何想法?


我使用以下代码来运行浏览器,但该Browser.Navigate方法没有做任何事情!

WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
this.AddChild(browser); // this is the System.Windows.Window
Run Code Online (Sandbox Code Playgroud)

小智 18

您可以使用WindowsFormHost控件在WPF应用程序中使用Acrobat Reader控件.我在这里有一篇关于它的博客文章:

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

我还有一个5分钟的截屏视频,我是如何在这里制作的:

http://www.screencast.com/t/JXRhGvzvB

  • 链接到博客帖子已经死了. (3认同)

Guy*_*uck 11

您只需在表单上托管Web浏览器控件并使用它来打开PDF.

在.NET 3.51中有一个新的本机WPF"WebBrowser"控件,或者您可以在WPF应用程序中托管Windows.Forms浏览器.

  • +1; 到目前为止最简单的解决方案:`<WebBrowser Source ="file:// c:\ path\to\my.pdf"/>` (10认同)
  • 2016年更新,我不再推荐Foxit(它现在臃肿),我使用sumatra pdf.但是Windows 10 Edge浏览器现在具有内置的PDF支持,因此很可能不需要在Windows上安装任何第三方PDF软件 (2认同)

Gul*_*zim 10

哎呀.这是一个winforms应用程序.不适用于WPF.无论如何我会发布这个.

试试这个

private AxAcroPDFLib.AxAcroPDF axAcroPDF1;
this.axAcroPDF1 = new AxAcroPDFLib.AxAcroPDF();
this.axAcroPDF1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axAcroPDF1.Enabled = true;
this.axAcroPDF1.Name = "axAcroPDF1";
this.axAcroPDF1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPDF1.OcxState")));
axAcroPDF1.LoadFile(DownloadedFullFileName);
axAcroPDF1.Visible = true;
Run Code Online (Sandbox Code Playgroud)


小智 8

以下代码需要安装Adobe Reader并将Pdf扩展连接到此.它只是运行它:

String fileName = "FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)


ody*_*yth 6

只需使用框架和web浏览器

Frame frame = new Frame();
WebBrowserbrowser = new WebBrowser();
browser.Navigate(new Uri(filename));
frame.Content = browser;
Run Code Online (Sandbox Code Playgroud)

然后,当你不再需要它时,这样做是为了清理它:

WebBrowser browser = frame.Content as WebBrowser;
browser.Dispose();
frame.Content = null;
Run Code Online (Sandbox Code Playgroud)

如果您不清理它,那么您可能会遇到内存泄漏问题,具体取决于您使用的.NET版本.如果我没有清理,我在.NET 3.5中看到了不良的内存泄漏.