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

Curtis Chau
Curtis Chau
已更新:

IronPDF在.NET應用程式中透過適用於MAUI的IronPdf.Viewer.Maui控制項顯示PDF,方法是將頁面光柵化成使用RasterizeToImageFiles的影像以配合任何UI框架,以及透過主控項如WPF和Windows Forms中的WebView2。 正確的選擇取決於您在哪種應用程序中搭建,以及需要多少查看器框架(chrome)。

「查看」在這些路徑中的含義不同。 MAUI查看器是一個完整的交互組件,帶有工具欄。 光柵化的方式是將每個頁面轉換為圖像,您可以在任何控件中顯示它。 WebView2和默認查看器路徑將文件交給瀏覽器引擎或用戶安裝的PDF閱讀器。 本文展示了每個方面,並如實地說明它的優缺點。

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

添加IronPdf.Viewer.Maui NuGet套件,然後在MauiProgram.cs中呼叫ConfigureIronPdfView(),之後將PDF綁定到IronPdfView。 以下單行將文件加載到查看器控件中。

  1. 1使用 IronPDF 安裝 NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2複製並運行這段程式碼。

    new IronPdf.Viewer.Maui.IronPdfView { Source = IronPdf.Viewer.Maui.IronPdfViewSource.FromFile("document.pdf") };
  3. 3部署到您的生產環境進行測試

    立即在您的項目中開始使用 IronPDF,並使用 免費試用
    arrow pointer

首先使用Install-Package IronPdf.Viewer.Maui安裝套件,然後將您的IronPDF授權金鑰添加到ConfigureIronPdfView中以去除試用橫幅。 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查看器。


如何將PDF頁面以圖像形式展現在WPF、WinForms或Blazor中?

呼叫RasterizeToImageFiles將每個PDF頁面轉換為PNG,然後在任何控件中顯示這些圖像:如WPF的Image、WinForms的PictureBox或Blazor組件中的<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");

輸出

將範例報表 PDF 的第一頁點陣化為 PNG 影像,以顯示於 .NET UI 控制項中

若需內存選項,ToBitmap會為您提供每頁一張的影像,您可以將其綁定至控制項而無需接觸磁碟。 調整DPI參數,以平衡檔案大小和清晰度,這與IronPDF轉換選項中製作文件時的控制相同。


我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic

Microsoft MVP

查看案例研究

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle

首席技術官,OPYN

查看案例研究

如何在WPF應用程式中查看PDF?

在WebView2控件中托管PDF,該控件使用從Microsoft Edge的Chromium引擎,並附帶了它自己的PDF查看器(工具欄、縮放和打印功能)。 將文檔保存到磁碟,然後將控件導航到文件的URI。 WebView2是舊的WebBrowser控制項的現代替代品,舊控制項依賴已被淘汰的Internet Explorer引擎。

// 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.
警告: WebView2需要目標機器上安裝Microsoft Edge WebView2運行時。在目前Windows 11版本上已預先安裝; 對於較舊的系統,隨您的安裝程式附帶Evergreen引導程式。

如何在Windows Forms中查看PDF?

將WebView2控件放置到表單並將其指向保存的PDF,就像WPF中一樣。 控件以非同步方式初始化,因此在呼叫Navigate之前請等待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.

如果您不想完全使用瀏覽器控制項,從上面部分採用基於影像的方法,直接嵌入至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檢視器提供完整工具列,將PDF光柵化為圖片可在任何平台上的任何UI上運作,WebView2重複利用Edge引擎,而Process.Start則取決於用戶的閱讀器。 選擇與您的應用相匹配的路徑,而不是強制一個單一答案。

由於大多數檢視都從您生成的文件開始,當您需要互動式工具列元件時,可將這些技術與完整的MAUI檢視器教學搭配使用。

常見問題

如何在ASP.NET應用程式中使用IronPDF嵌入PDF?

您可以從控制器操作提供PDF,並將其嵌入到HTML iframe中。內建的瀏覽器PDF查看器將處理渲染,維持您的頁面佈局。

使用IronPDF在.NET中查看PDF的不同方法有哪些?

IronPDF支援通過MAUI查看器查看PDF,將頁面光柵化為圖像,使用WPF和Windows Forms中的WebView2,並通過系統預設的PDF查看器開啟。

如何在MAUI應用程式中使用IronPDF顯示PDF?

安裝IronPdf.Viewer.Maui NuGet套件,在MauiProgram.cs中配置,並使用IronPDF的MAUI查看器元件將PDF綁定到IronPdfView。

我可以使用IronPDF將PDF頁面轉換為圖像,以在任何平台上顯示嗎?

可以,您可以使用IronPDF中的RasterizeToImageFiles方法將每個PDF頁面轉換為PNG圖像,適合用於任何UI框架。

使用WebView2在WPF應用程式中顯示PDF的優勢是什麼?

WebView2利用了Microsoft Edge的Chromium引擎,提供內建的PDF查看功能,如工具列、縮放和列印,並比舊版的WebBrowser控制項更受推薦。

如何使用IronPDF在系統預設的查看器中開啟PDF?

使用System.Diagnostics.Process.Start與PDF文件路徑來在使用者的預設PDF閱讀器(如瀏覽器或Adobe Acrobat)中開啟。

是否可以在Blazor應用程式中使用IronPDF顯示PDF?

可以,您可以使用IronPDF的RasterizeToImageFiles將每個PDF頁面轉換為圖像,並在Blazor元件中使用img標籤顯示這些圖像。

如果使用者系統上未安裝預設的PDF查看器應怎麼辦?

將System.Diagnostics.Process.Start調用包裝在try-catch塊中,以處理缺少PDF查看器的情況,確保您的應用程式不會崩潰。

IronPDF的MAUI查看器如何增強我的應用程式的PDF查看體驗?

MAUI查看器提供了一個完整的互動組件帶有工具列,允許用戶在您的應用程式內直接與PDF互動。

我可以使用IronPDF整合PDF查看功能,而不依賴於瀏覽器控制項嗎?

可以,通過將PDF頁面光柵化為圖像,您可以在任何UI框架中整合查看功能,而不依賴於基於瀏覽器的控制項。

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

閱讀更多

準備好開始了嗎?

NuGet 下載次數 22,471,552版本:2026.11剛剛發布