有什么想法可以在 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控件在WPF应用程序中使Acrobat Reader控件正常工作。我在这里有一篇博客文章:
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浏览器。
<WebBrowser Source="file://c:\path\to\my.pdf" />
。 - jeroenh哎呀,这是针对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 - 一款基于WPF的PDF查看器控件
http://www.codeproject.com/Articles/579878/MoonPdfPanel-A-WPF-based-PDF-viewer-control
只需使用框架和Web浏览器即可
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中看到了严重的内存泄漏问题。
String fileName = "FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();
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);
}
DocumentViewer
控件进行显示,而不是使用WebBrowser
控件。 - hertzogth