任何想法如何在 WPF Windows 应用程序中显示 PDF 文件?
我正在使用以下代码运行浏览器,但该Browser.Navigate
方法没有执行任何操作!
WebBrowser browser = new WebBrowser();
browser.Navigate("http://www.google.com");
this.AddChild(browser); // this is the System.Windows.Window
您可以使用 WindowsFormHost 控件使 Acrobat Reader 控件在 WPF 应用程序中工作。我在这里有一篇关于它的博客文章:
http://hugeonion.com/2009/04/06/displaying-a-pdf-file-within-a-wpf-application/
我还有一个 5 分钟的截屏视频,讲述了我是如何在这里制作的:
您可以简单地在表单上托管一个 Web 浏览器控件并使用它来打开 PDF。
.NET 3.51 中有一个新的本机 WPF“WebBrowser”控件,或者您可以在 WPF 应用程序中托管 Windows.Forms 浏览器。
哎呀。这是一个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;
试试MoonPdfPanel - A WPF-based PDF viewer control
http://www.codeproject.com/Articles/579878/MoonPdfPanel-A-WPF-based-PDF-viewer-control
以下代码需要安装 Adobe 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 中看到严重的内存泄漏。
披露:这是一个商业广告,我为这家公司工作。
我意识到答案已被接受,但以下不需要 Adobe Reader/Acrobat,它是 WPF 解决方案 - 与 Winforms 不同。我也意识到这是一个老问题,但它刚刚更新,所以我想它仍然是真实的。
PDFRasterizer.NET 3.0允许您呈现到 WPF FixedDocument。它保留所有矢量图形(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 以快速实现查看器。
您也可以使用 FoxitReader。它是免费的,并带有一个 ActiveX 控件,在您安装 FoxitReader 应用程序后,该控件会在 Web 浏览器(IE 和其他浏览器)中注册。因此,在系统上安装 FoxitReader 后,放置一个 WebBrowser 控件并将其 Source 属性设置为指向 PDF 文件的文件路径。
检查一下: http: //itextsharp.sourceforge.net/ 您可能必须使用 WindowsFormsHost,但由于它是开源的,您可能可以在 WPF 中使它更优雅一点。