关于如何在 WPF Windows 应用程序中显示 PDF 文件有什么想法吗?


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

WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
this.AddChild(browser); // this is the System.Windows.Window
有帮助吗?

解决方案

哎呀。这是一个 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;

其他提示

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

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

我还有一个 5 分钟的截屏视频,展示了我是如何做到这一点的:

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

您可以简单地在表单上托管一个 Web 浏览器控件并使用它来打开 PDF。

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

以下代码需要安装 Adob​​e Reader 并将 Pdf 扩展连接到此。它只是简单地运行它:

String fileName = "FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process(); 
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();

只需使用框架和网络浏览器,就像这样

Frame frame = new Frame();
WebBrowserbrowser = new WebBrowser();
browser.Navigate(new Uri(filename));
frame.Content = browser;

然后,当您不再需要它时,请执行以下操作来清理它:

WebBrowser browser = frame.Content as WebBrowser;
browser.Dispose();
frame.Content = null;

如果你不清理它,那么你可能会遇到内存泄漏问题,具体取决于你使用的 .NET 版本。如果我不清理的话,我会在 .NET 3.5 中看到严重的内存泄漏。

披露:这是一个商业广告,我在这家公司工作。

我意识到答案已经被接受,但以下内容不需要 Adob​​e Reader/Acrobat,它是一个 WPF 解决方案 - 与 Winforms 相反。我也意识到这是一个老问题,但它刚刚更新,所以我想它仍然是实际的。

PDFRasterizer.NET 3.0 允许您渲染到 WPF 固定文档。它保留所有矢量图形(PDF 图形被转换为或多或少等效的 WPF 元素。这可能最接近您的需要。

using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
  pdfDoc = new Document(file);

  ConvertToWpfOptions convertOptions = new ConvertToWpfOptions();
  RenderSettings renderSettings = new RenderSettings();
  ...

  FixedDocument wpfDoc = pdfDoc.ConvertToWpf(renderSettings, convertOptions, 0, 9, summary);
}

您可以将 wpfDoc 传递给例如WPF DocumentViewer 快速实现查看器。

您还可以使用福昕阅读器。它是免费的,并附带一个 ActiveX 控件,在您安装 FoxitReader 应用程序后,该控件会在网络浏览器(IE 和其他浏览器)中注册。因此,在系统上安装 FoxitReader 后,放置一个 WebBrowser 控件并将其 Source 属性设置为指向 PDF 文件的文件路径。

看一下这个: http://itextsharp.sourceforge.net/您可能必须使用 WindowsFormsHost,但由于它是开源的,您也许可以在 WPF 中使其变得更加优雅。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top