IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月29日

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

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

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

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

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2复制并运行这段代码。

    new IronPdf.Viewer.Maui.IronPdfView { Source = IronPdf.Viewer.Maui.IronPdfViewSource.FromFile("document.pdf") };
    C#
  3. 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>

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


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

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

输入

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");
C#

输出

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

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


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

Milan Jovanovic

微软MVP

查看案例研究

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

Brent Matzelle

首席技术官,OPYN

查看案例研究

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

David Jones

首席软件工程师,Agorus Build

查看案例研究

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

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

// 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.
C#
警告: WebView2需要目标机器上的Microsoft Edge WebView2运行时。在当前的Windows 11版本中预安装; 对于旧系统,将Evergreen引导程序与您的安装程序一起提供。

如何在 Windows 窗体中查看 PDF?

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

// 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.
C#

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


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

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

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);

在生产中,包装调用以防止缺少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}");
}

结论

此处的每个查看路径都将交互性换成便携性: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。

How can I open a PDF in the default system viewer using IronPDF?

Use System.Diagnostics.Process.Start with the PDF file path to open it in the user's default PDF reader, such as a browser or Adobe Acrobat.

Is it possible to display PDFs in Blazor applications with IronPDF?

Yes, you can convert each PDF page to an image using IronPDF's RasterizeToImageFiles, and display those images using an img tag in a Blazor component.

What should I do if the default PDF viewer is not installed on the user's system?

Wrap the System.Diagnostics.Process.Start call in a try-catch block to handle cases where a PDF viewer is missing, ensuring your application does not crash.

How can IronPDF's MAUI viewer enhance my application's PDF viewing experience?

The MAUI viewer provides a full interactive component with a toolbar, allowing users to interact with the PDF directly within your app.

Can I use IronPDF to integrate PDF viewing capabilities without depending on browser controls?

Yes, by rasterizing PDF pages to images, you can integrate viewing capabilities in any UI framework without relying on browser-based controls.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 21,105,021版本:2026.9刚刚发布

立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起

Key in blue circle

立即获取免费的 30 天试用版密钥。

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

OR
bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥。
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 “IronPDF”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

或在此处下载 Windows 安装程序。

  1. 下载并解压 IronPDF 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键点击引用。选择浏览,“IronPDF.dll”

$999 起