# 如何使用IronPDF C#在.NET中查看PDF
IronPDF通过MAUI的`RasterizeToImageFiles`将页面光栅化为任何UI框架中的图像,以及通过诸如WPF和Windows Forms中的WebView2之类的主机控件。 正确的选择取决于您正在构建什么类型的应用程序以及您需要多少查看器chrome。
"查看"在这些路径中有不同的意义。 MAUI查看器是一个带工具栏的全互动组件。 栅格化路径将每页转换为您可以在任何控件中显示的图像。 WebView2和默认查看器路径将文件交给浏览器引擎或用户安装的PDF阅读器。 本文展示了每一个,并诚实地说明了它能和不能给您什么。
*as-heading:2(快速入门:使用IronPDF在MAUI中查看PDF)*
添加`IronPdf.Viewer.Maui` NuGet包,在`IronPdfView`。 下面的单行代码将一个文件加载到查看器控件中。
```cs
:title=Load a PDF into the MAUI viewer in one line!
new IronPdf.Viewer.Maui.IronPdfView { Source = IronPdf.Viewer.Maui.IronPdfViewSource.FromFile("document.pdf") };
```
首先使用`ConfigureIronPdfView`中添加您的IronPDF许可证密钥以去除试用横幅。 [MAUI查看器教程](/tutorials/pdf-viewing/)提供完整的项目设置。
<div class="hsg-featured-snippet">
<h3>最小工作流程(5 个步骤)</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="trial-license-after-download" href="https://www.nuget.org/packages/IronPdf/">从NuGet安装IronPDF用于.NET PDF渲染</a></li>
<li>选择一个查看路径:MAUI查看器、栅格化为图像、WebView2或默认系统查看器</li>
<li>在ASP.NET中使用HTML <code>iframe</code>嵌入PDF</li>
<li>将PDF页面渲染为图像以供WPF、WinForms或Blazor显示</li>
<li>使用<code>System.Diagnostics.Process.Start</code>在用户安装的阅读器中打开PDF</li>
</ol>
</div>
<br class="clear" />
## 如何在 ASP.NET 和 MVC 中查看 PDF?
从控制器操作服务PDF,并用HTML `iframe`嵌入。 浏览器内置的PDF查看器处理渲染,因此文档内联出现,而您的页面布局保持完整。 当PDF即时生成时,IronPDF的[HTML到PDF转换](/how-to/html-string-to-pdf/)生成动作返回的文件。
```csharp
// 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查看器。
<hr class="separator" />
## 如何在WPF、WinForms或Blazor中显示PDF页面为图像?
调用`RasterizeToImageFiles`将每个PDF页面转换为PNG,然后在任何控件中显示这些图像:WPF `Image`、WinForms `<img>`。 这是IronPDF原生的查看路径。 它在每个UI框架上以及IronPDF支持的每个操作系统上工作效果一样,因为它生成普通图像文件,而不是依赖于主机浏览器控件。
### 输入
<iframe loading="lazy" src="/static-assets/pdf/how-to/net-pdf-viewer/sample-report.pdf" width="100%" height="500px"></iframe>
```csharp
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");
```
### 输出
<img src="/static-assets/pdf/how-to/net-pdf-viewer/rasterized-page-1.png" alt="示例报告 PDF 的第一页栅格化为 PNG 图像,用于在 .NET UI 控件中显示" width="100%" class="img-responsive add-shadow" />
对于内存中的选项,`ToBitmap`返回每页一个图像,您可以在不触摸磁盘的情况下绑定到控件。 调整DPI参数以在文件大小与清晰度之间做出权衡,与IronPDF生产文档时您在[渲染选项](/how-to/rendering-options/)上拥有的控制一样。
<hr class="separator" />
## 如何在 WPF 应用程序中查看 PDF?
在WebView2控件中托管PDF,它使用Microsoft Edge的Chromium引擎,并附带其自己的PDF查看器(包括工具栏、缩放和打印)。 将文档保存到磁盘,然后将控件导航到文件URI。 WebView2是对早期使用已弃用的Internet Explorer引擎的`WebBrowser`控件的当前替代品。
```csharp
// 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.
```
[[w:(WebView2需要目标机器上的Microsoft Edge WebView2运行时。在当前的Windows 11版本中预安装; 对于旧系统,将Evergreen引导程序与您的安装程序一起提供。)]]
<hr class="separator" />
## 如何在 Windows 窗体中查看 PDF?
将一个WebView2控件拖放到您的表单中,并指向保存的PDF,完全如同在WPF中。 控件异步初始化,因此在调用`EnsureCoreWebView2Async`。 这为WinForms应用提供了相同的基于Chromium的查看器,而无需捆绑第三方组件。
```csharp
// 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.
```
如果您完全不想托管浏览器控件,上面的基于图像的方法会直接进入`PictureBox`,并且完全避免了WebView2运行时依赖。
<hr class="separator" />
## 如何在默认系统 PDF 查看器中查看 PDF?
将文件路径传递给`System.Diagnostics.Process.Start`以打开PDF,无论用户设置的默认阅读器是什么,例如浏览器或Adobe Acrobat。 这将渲染交给外部应用,而不是嵌入其中,适用于仅需展示最终文件的实用程序和批处理工具。
```csharp
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处理程序导致程序崩溃:
```csharp
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查看器教程](/tutorials/pdf-viewing/)结合使用。
// Controller action to serve PDFpublic ActionResultViewPdf(){ var pdfPath = Server.MapPath("~/Content/sample.pdf"); returnFile(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>
ImportsSystem.Web.Mvc' Controller action to serve PDFPublic Function ViewPdf() AsActionResult Dim pdfPath = Server.MapPath("~/Content/sample.pdf") ReturnFile(pdfPath, "application/pdf")End Function' 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>
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");
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");
// 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.
// 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.
// 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.
// 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.
using IronPdf;// Render any HTML fragment or document to HTMLChromePdfRenderer renderer = new ChromePdfRenderer();PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");var outputPath = "ChromePdfRenderer.pdf";// Export PDF documentpdf.SaveAs(outputPath);// This neat trick opens our PDF file so we can see the result in our default PDF viewerSystem.Diagnostics.Process.Start(outputPath);
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);
ImportsIronPdf' Render any HTML fragment or document to HTMLPrivate renderer As New ChromePdfRenderer()Private pdf AsPdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")Private outputPath = "ChromePdfRenderer.pdf"' Export PDF documentpdf.SaveAs(outputPath)' This neat trick opens our PDF file so we can see the result in our default PDF viewerSystem.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)
在生产中,包装调用以防止缺少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 installedMessageBox.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}");
}
ImportsSystem.DiagnosticsImportsSystem.Windows.FormsTry Dim psi As New ProcessStartInfoWith { .FileName = outputPath, .UseShellExecute = True }Process.Start(psi)Catch ex AsException ' Handle the case where no PDF viewer is installedMessageBox.Show($"Unable to open PDF: {ex.Message}")EndTry
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
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.