首页 / 知识

在WPF应用程序中显示PDF

2023-04-14 21:22:00

在WPF应用程序中显示PDF

Display a PDF in WPF Application

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

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

1
2
3
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分钟的截屏视频,介绍了我在这里的制作过程:

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


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

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


哎呀。这是用于winforms应用程序。不适合WPF。无论如何,我都会发布。

尝试这个

1
2
3
4
5
6
7
8
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

GitHub:https://github.com/reliak/moonpdf


以下代码要求安装Adobe Reader,并将Pdf扩展名与此连接。
它只是运行它:

1
2
3
4
5
String fileName ="FileName.pdf";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = fileName;
process.Start();
process.WaitForExit();

像这样使用框架和网络浏览器

1
2
3
4
Frame frame = new Frame();
WebBrowserbrowser = new WebBrowser();
browser.Navigate(new Uri(filename));
frame.Content = browser;

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

1
2
3
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元素。这可能最接近您的需要。

1
2
3
4
5
6
7
8
9
10
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中使其更加优雅。


显示应用程序浏览器运行

最新内容

相关内容

猜你喜欢