IRONSOFTWAREHOME

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

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

IronPDF 通過 MAUI 的 IronPdf.Viewer.Maui 控制在 .NET 應用程式中顯示 PDF,透過 RasterizeToImageFiles 將頁面光柵化為影像以適用於任何 UI 框架,以及通過像 WPF 和 Windows Forms 中的 WebView2 等主機控件進行顯示。 正確的選擇取決於您正在構建的應用程式型別以及您需要多少查看器介面。

在這些路徑中,"查看"有不同的含義。 MAUI 查看器是一個完整的互動組件,帶有工具列。 光柵化路径將每一頁轉成您可以在任何控制項中顯示的影像。 WebView2和預設查看器路径會將檔案交給瀏覽器引擎或使用者安裝的PDF閱讀器。 本文顯示了每一種方式,並誠實地告訴您它們能夠和不能夠為您提供什麼。

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

新增 IronPdf.Viewer.Maui NuGet 套件,然後在 MauiProgram.cs 中調用 ConfigureIronPdfView(),接著將 PDF 綁定到 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

首先使用 Install-Package IronPdf.Viewer.Maui 安裝套件,然後在 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 PictureBox 或在 Blazor 組件中的 <img>。 這是 IronPDF 原生的查看路徑。 它在每個夾層框架和每個 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

Microsoft MVP

查看案例研究

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

Brent Matzelle

首席技術官,OPYN

查看案例研究

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

將 PDF 設置在 WebView2 控件中,使用來自 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.
C#
警告: WebView2 在目標機器上要求安裝 Microsoft Edge WebView2 Runtime。在當前 Windows 11 構建中預裝; 舊系統可使用永恆更新安裝程式附帶安裝程式。)]}

如何在 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.
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套件中提供了一個互動式PDF檢視元件,用於MAUI專案。在安裝套件並呼叫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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 21,228,841版本: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 IronPDF
預訂您的免費現場演示
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起