如何使用IronPDF C#在.NET中查看PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF通过MAUI的RasterizeToImageFiles将页面光栅化为任何UI框架中的图像,以及通过诸如WPF和Windows Forms中的WebView2之类的主机控件。 正确的选择取决于您正在构建什么类型的应用程序以及您需要多少查看器chrome。

"查看"在这些路径中有不同的意义。 MAUI查看器是一个带工具栏的全互动组件。 栅格化路径将每页转换为您可以在任何控件中显示的图像。 WebView2和默认查看器路径将文件交给浏览器引擎或用户安装的PDF阅读器。 本文展示了每一个,并诚实地说明了它能和不能给您什么。

快速入门:使用IronPDF在MAUI中查看PDF

添加IronPdf.Viewer.Maui NuGet包,在IronPdfView。 下面的单行代码将一个文件加载到查看器控件中。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 复制并运行这段代码。

    new IronPdf.Viewer.Maui.IronPdfView { Source = IronPdf.Viewer.Maui.IronPdfViewSource.FromFile("document.pdf") };
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPDF

    arrow pointer

首先使用ConfigureIronPdfView中添加您的IronPDF许可证密钥以去除试用横幅。 MAUI查看器教程提供完整的项目设置。


如何在 ASP.NET 和 MVC 中查看 PDF?

从控制器操作服务PDF,并用HTML iframe嵌入。 浏览器内置的PDF查看器处理渲染,因此文档内联出现,而您的页面布局保持完整。 当PDF即时生成时,IronPDF的HTML到PDF转换生成动作返回的文件。

// Controller action to serve PDF
public ActionResult ViewPdf()
{
    var pdfPath = Server.MapPath("~/Content/sample.pdf");
    return File(pdfPath, "application/pdf");
}

// In your Razor view
<iframe src="@Url.Action("ViewPdf")" width="100%" height="600px"></iframe>
// Controller action to serve PDF
public ActionResult ViewPdf()
{
    var pdfPath = Server.MapPath("~/Content/sample.pdf");
    return File(pdfPath, "application/pdf");
}

// In your Razor view
<iframe src="@Url.Action("ViewPdf")" width="100%" height="600px"></iframe>
Imports System.Web.Mvc

' Controller action to serve PDF
Public Function ViewPdf() As ActionResult
    Dim pdfPath = Server.MapPath("~/Content/sample.pdf")
    Return File(pdfPath, "application/pdf")
End Function

' In your Razor view
<iframe src="@Url.Action("ViewPdf")" width="100%" height="600px"></iframe>
$vbLabelText   $csharpLabel

如果您需要文本选择、缩放和页面导航,由您控制而非浏览器默认,渲染PDF页面为图像(如下所示)并显示它们,或者连接您选择的JavaScript查看器。


如何在WPF、WinForms或Blazor中显示PDF页面为图像?

调用RasterizeToImageFiles将每个PDF页面转换为PNG,然后在任何控件中显示这些图像:WPF Image、WinForms <img>。 这是IronPDF原生的查看路径。 它在每个UI框架上以及IronPDF支持的每个操作系统上工作效果一样,因为它生成普通图像文件,而不是依赖于主机浏览器控件。

输入

:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-rasterize.cs
using IronPdf;

// Load the PDF you want to display.
PdfDocument pdf = PdfDocument.FromFile("sample-report.pdf");

// Render every page to a PNG. The asterisk in the path is replaced with the page number,
// producing viewer-page-1.png, viewer-page-2.png, and so on. The last argument (100) is the DPI.
pdf.RasterizeToImageFiles("viewer-page-*.png", IronPdf.Imaging.ImageType.Png, 100);

// To show a page inside a GUI control without writing to disk, get in-memory bitmaps instead.
// ToBitmap returns one AnyBitmap per page; bind bitmaps[0] to a WPF Image or a WinForms PictureBox.
var bitmaps = pdf.ToBitmap();
bitmaps[0].SaveAs("viewer-firstpage.png");
Imports IronPdf

' Load the PDF you want to display.
Dim pdf As PdfDocument = PdfDocument.FromFile("sample-report.pdf")

' Render every page to a PNG. The asterisk in the path is replaced with the page number,
' producing viewer-page-1.png, viewer-page-2.png, and so on. The last argument (100) is the DPI.
pdf.RasterizeToImageFiles("viewer-page-*.png", IronPdf.Imaging.ImageType.Png, 100)

' To show a page inside a GUI control without writing to disk, get in-memory bitmaps instead.
' ToBitmap returns one AnyBitmap per page; bind bitmaps(0) to a WPF Image or a WinForms PictureBox.
Dim bitmaps = pdf.ToBitmap()
bitmaps(0).SaveAs("viewer-firstpage.png")
$vbLabelText   $csharpLabel

输出

示例报告 PDF 的第一页栅格化为 PNG 图像,用于在 .NET UI 控件中显示

对于内存中的选项,ToBitmap返回每页一个图像,您可以在不触摸磁盘的情况下绑定到控件。 调整DPI参数以在文件大小与清晰度之间做出权衡,与IronPDF生产文档时您在渲染选项上拥有的控制一样。


如何在 WPF 应用程序中查看 PDF?

在WebView2控件中托管PDF,它使用Microsoft Edge的Chromium引擎,并附带其自己的PDF查看器(包括工具栏、缩放和打印)。 将文档保存到磁盘,然后将控件导航到文件URI。 WebView2是对早期使用已弃用的Internet Explorer引擎的WebBrowser控件的当前替代品。

:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-wpf-viewer.cs
// WPF: display a PDF using the WebView2 control (Microsoft Edge / Chromium).
// Install the NuGet package Microsoft.Web.WebView2 and add a <wv2:WebView2 x:Name="pdfWebView" />
// element to your XAML (xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf").
// WebView2 renders PDFs with the same built-in viewer as the Edge browser (toolbar, zoom, print).
using System;
using System.IO;

// Save the IronPDF-generated document to disk first, then point WebView2 at it.
var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.pdf");
pdf.SaveAs(pdfPath);

// pdfWebView is the WebView2 control declared in your XAML.
// EnsureCoreWebView2Async must complete before navigating.
await pdfWebView.EnsureCoreWebView2Async();
pdfWebView.CoreWebView2.Navigate(new Uri(pdfPath).AbsoluteUri);

// Legacy alternative: the older System.Windows.Controls.WebBrowser control still works on
// Windows machines that have a PDF handler installed, but it relies on the deprecated Internet
// Explorer engine and is not recommended for new applications. Prefer WebView2 above.
Imports System
Imports System.IO
Imports IronPdf

' WPF: display a PDF using the WebView2 control (Microsoft Edge / Chromium).
' Install the NuGet package Microsoft.Web.WebView2 and add a <wv2:WebView2 x:Name="pdfWebView" />
' element to your XAML (xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf").
' WebView2 renders PDFs with the same built-in viewer as the Edge browser (toolbar, zoom, print).

' Save the IronPDF-generated document to disk first, then point WebView2 at it.
Dim pdf = New ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello IronPDF</h1>")
Dim pdfPath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.pdf")
pdf.SaveAs(pdfPath)

' pdfWebView is the WebView2 control declared in your XAML.
' EnsureCoreWebView2Async must complete before navigating.
Await pdfWebView.EnsureCoreWebView2Async()
pdfWebView.CoreWebView2.Navigate(New Uri(pdfPath).AbsoluteUri)

' Legacy alternative: the older System.Windows.Controls.WebBrowser control still works on
' Windows machines that have a PDF handler installed, but it relies on the deprecated Internet
' Explorer engine and is not recommended for new applications. Prefer WebView2 above.
$vbLabelText   $csharpLabel

警告WebView2需要目标机器上的Microsoft Edge WebView2运行时。在当前的Windows 11版本中预安装; 对于旧系统,将Evergreen引导程序与您的安装程序一起提供。


Icon Quote related to 如何在 WPF 应用程序中查看 PDF?

我最喜欢的这种库是 IronPDF。它允许快速高效地操作 PDF 文件。它还具有许多有价值的功能,比如导出为 PDF/A 格式和数字签名 PDF 文档。

Milan Jovanovic related to 如何在 WPF 应用程序中查看 PDF?

Milan Jovanovic

微软MVP

查看案例研究
Icon Quote related to 如何在 WPF 应用程序中查看 PDF?

IronOCR 意味着我们每年可以节省 $40,000 的人工处理成本,同时提高生产力,并释放资源用于高影响任务。我强烈推荐它。

Brent Matzelle related to 如何在 WPF 应用程序中查看 PDF?

Brent Matzelle

首席技术官,OPYN

查看案例研究
Icon Quote related to 如何在 WPF 应用程序中查看 PDF?

Iron Suite 在我们的运营中起着至关重要的作用。这些工具提高了业务各方面的效率,包括创建平面图和改善库存管理。

David Jones related to 如何在 WPF 应用程序中查看 PDF?

David Jones

首席软件工程师,Agorus Build

查看案例研究

如何在 Windows 窗体中查看 PDF?

将一个WebView2控件拖放到您的表单中,并指向保存的PDF,完全如同在WPF中。 控件异步初始化,因此在调用EnsureCoreWebView2Async。 这为WinForms应用提供了相同的基于Chromium的查看器,而无需捆绑第三方组件。

:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-winforms-viewer.cs
// Windows Forms: display a PDF using the WebView2 control (Microsoft Edge / Chromium).
// Install the NuGet package Microsoft.Web.WebView2, then drag a WebView2 control onto your form
// (or add it in code) and name it pdfWebView.
using System;
using System.IO;

// Generate or load the PDF with IronPDF, then save it so WebView2 can open the file.
var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");
string pdfPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.pdf");
pdf.SaveAs(pdfPath);

// pdfWebView is the WebView2 control on your form.
// Initialize the runtime, then navigate to the local file URI.
await pdfWebView.EnsureCoreWebView2Async();
pdfWebView.CoreWebView2.Navigate(new Uri(pdfPath).AbsoluteUri);

// Legacy alternative: the System.Windows.Forms.WebBrowser control can host a PDF when a system
// PDF handler is registered, but it uses the deprecated Internet Explorer engine. Use WebView2
// for new projects so the viewer works consistently across modern Windows installs.
Imports System
Imports System.IO
Imports IronPdf

' Generate or load the PDF with IronPDF, then save it so WebView2 can open the file.
Dim pdf = New ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello IronPDF</h1>")
Dim pdfPath As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "document.pdf")
pdf.SaveAs(pdfPath)

' pdfWebView is the WebView2 control on your form.
' Initialize the runtime, then navigate to the local file URI.
Await pdfWebView.EnsureCoreWebView2Async()
pdfWebView.CoreWebView2.Navigate(New Uri(pdfPath).AbsoluteUri)

' Legacy alternative: the System.Windows.Forms.WebBrowser control can host a PDF when a system
' PDF handler is registered, but it uses the deprecated Internet Explorer engine. Use WebView2
' for new projects so the viewer works consistently across modern Windows installs.
$vbLabelText   $csharpLabel

如果您完全不想托管浏览器控件,上面的基于图像的方法会直接进入PictureBox,并且完全避免了WebView2运行时依赖。


如何在默认系统 PDF 查看器中查看 PDF?

将文件路径传递给System.Diagnostics.Process.Start以打开PDF,无论用户设置的默认阅读器是什么,例如浏览器或Adobe Acrobat。 这将渲染交给外部应用,而不是嵌入其中,适用于仅需展示最终文件的实用程序和批处理工具。

:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-default-pdf-viewer.cs
using IronPdf;

// Render any HTML fragment or document to HTML
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");

var outputPath = "ChromePdfRenderer.pdf";

// Export PDF document
pdf.SaveAs(outputPath);

// This neat trick opens our PDF file so we can see the result in our default PDF viewer
System.Diagnostics.Process.Start(outputPath);
Imports IronPdf

' Render any HTML fragment or document to HTML
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")

Private outputPath = "ChromePdfRenderer.pdf"

' Export PDF document
pdf.SaveAs(outputPath)

' This neat trick opens our PDF file so we can see the result in our default PDF viewer
System.Diagnostics.Process.Start(outputPath)
$vbLabelText   $csharpLabel

在生产中,包装调用以防止缺少PDF处理程序导致程序崩溃:

try
{
    var psi = new System.Diagnostics.ProcessStartInfo
    {
        FileName = outputPath,
        UseShellExecute = true
    };
    System.Diagnostics.Process.Start(psi);
}
catch (Exception ex)
{
    // Handle the case where no PDF viewer is installed
    MessageBox.Show($"Unable to open PDF: {ex.Message}");
}
try
{
    var psi = new System.Diagnostics.ProcessStartInfo
    {
        FileName = outputPath,
        UseShellExecute = true
    };
    System.Diagnostics.Process.Start(psi);
}
catch (Exception ex)
{
    // Handle the case where no PDF viewer is installed
    MessageBox.Show($"Unable to open PDF: {ex.Message}");
}
Imports System.Diagnostics
Imports System.Windows.Forms

Try
    Dim psi As New ProcessStartInfo With {
        .FileName = outputPath,
        .UseShellExecute = True
    }
    Process.Start(psi)
Catch ex As Exception
    ' Handle the case where no PDF viewer is installed
    MessageBox.Show($"Unable to open PDF: {ex.Message}")
End Try
$vbLabelText   $csharpLabel

结论

此处的每个查看路径都将交互性换成便携性:MAUI查看器提供完整的工具栏,光栅化为图像适用于任何平台上的UI,WebView2重用Edge引擎,Process.Start委托给用户的阅读器。 选择一个适合您应用程序的,而不是强迫唯一的答案。

由于大多数查看从您生成的文档开始,您需要一个交互式工具栏组件时,可以将这些技术与完整的MAUI查看器教程结合使用。

常见问题解答

如何在 .NET MAUI 应用程序中显示 PDF?

IronPDF在IronPdf.Viewer.Maui NuGet包中为MAUI项目提供一个交互式PDF查看器组件。安装包并调用ConfigureIronPdfView()后,您可以将PDF绑定到IronPdfView控件,通过IronPdfViewSource.FromFile从文件加载或从流加载。这个MAUI组件是唯一带有内置工具栏的路径;在桌面和网络上,您可以通过托管WebView2,嵌入iframe或将页面栅格化为图像来查看PDF。

在 ASP.NET 网络应用程序中查看 PDF 的最简单方法是什么?

对于 ASP.NET 应用程序,IronPDF 支持通过浏览器窗口或 iframe 查看 PDF。iframe 方法特别有效,因为它在保持应用程序布局的同时,还能内联显示 PDF 内容。您还可以使用 IronPDF 的 HTML 至 PDF 转换功能,在运行中生成动态 PDF 供查看。

能否将 PDF 查看功能集成到 WPF 和 WinForms 应用程序中?

是的。对于WPF和WinForms,托管一个WebView2控件并导航到保存的PDF;WebView2使用Microsoft Edge的Chromium引擎,并包含自己的工具栏、缩放和打印。WebView2取代了依赖于弃用的Internet Explorer引擎的旧版WebBrowser控件。如果您不想托管浏览器控件,可以将每个页面栅格化为图像,并使用RasterizeToImageFiles将其显示在Image或PictureBox中。IronPDF还可以处理相同文档上的数字签名、表单填写和PDF压缩。

是否可以在 .NET 应用程序中使用系统默认的 PDF 查看器?

当然可以。IronPDF 支持使用 System.Diagnostics.Process 与系统默认的 PDF 查看器集成。这种方法允许您在用户首选的 PDF 应用程序中打开 PDF 文件,同时仍可使用 IronPDF 事先生成、处理或准备 PDF 文档。

除了查看之外,PDF 还具有哪些其他功能?

IronPDF提供广泛的PDF功能,包括用于文档安全性的数字签名、交互式PDF的表单填写、用于减小文件大小的PDF压缩、用于动态内容生成的HTML到PDF转换,以及以多种格式保存/导出PDF。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 20,296,129 | 版本: 2026.7 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPdf
运行示例看着你的HTML代码变成PDF文件。