C# PDF 檢視器
using IronPdf.Viewer.Maui、ASP.NET iframe 嵌入、WPF/WinForms WebBrowser 控制項,或 System.Diagnostics.Process 進行預設檢視器整合,在 .NET 應用程式中檢視 PDF 檔案。 每種方法皆為不同類型的應用程式提供可靠的 PDF 顯示功能。
本文探討在 .NET 應用程式中檢視 PDF 的各種方法。 在應用程式中檢視 PDF 文件是一項常見需求,透過 PDF Library for .NET 即可輕鬆實現。
IronPDF 為 MAUI 專案提供一套功能齊全的 PDF 檢視器。 如需更多資訊,請參閱以下連結:"在 MAUI for C# .NET 中檢視 PDF"。此函式庫亦支援數位簽章、表單填寫及 PDF 壓縮等進階功能,使其成為滿足您所有 PDF 需求的全方位解決方案。
快速入門:使用 C# 透過 IronPDF 檢視 PDF 檔案
輕鬆開始使用 IronPDF,在您的 .NET 應用程式中檢視 PDF 檔案。 本快速指南將協助您透過 IronPDF 的簡易 API 整合 PDF 檢視器,讓您能輕鬆載入並顯示 PDF 文件。 非常適合希望在 C# 專案中快速且可靠地整合 PDF 檢視功能的開發人員。
簡化工作流程(5 個步驟)
- 從 NuGet 安裝 IronPDF for .NET 以供 PDF 檢視
- Use HTML
iframetag in ASP.NET & MVC PDF viewer - 使用 WPF C# PDF 檢視器
WebBrowser控件 - 直接在 Windows Forms PDF 檢視器中檢視 PDF
- 請使用
System.Diagnostics.Process.Start預設系統 PDF 檢視器
如何在 ASP.NET 與 MVC 中檢視 PDF 檔案?
對於網頁應用程式,PDF 檔案可在瀏覽器視窗中檢視,或 iframe。 iframe 這種方法特別有效,因為它能在內嵌顯示 PDF 內容的同時,維持應用程式的版面配置。在 ASP.NET 應用程式中實作 PDF 檢視功能時,建議考慮使用 IronPDF 的 HTML 轉 PDF 轉換功能,以即時生成動態 PDF 檔案。
此外,您亦可採用 Mozilla 推出的出色 pdf.js 函式庫,該函式庫提供了一個完全以 JavaScript 編寫的完整 PDF 檢視器。 此函式庫提供文字選取、Zoom及頁面導覽等功能,且無需安裝任何瀏覽器外掛程式。
以下是一個使用 iframe 在 ASP.NET 頁面中嵌入 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>
針對更進階的應用情境,您可能需要探索如何直接從 ASP.NET 應用程式儲存及匯出 PDF 檔案,或實作自訂頁首與頁尾,以打造符合品牌形象的文件瀏覽體驗。
如何在 WPF 應用程式中檢視 PDF 檔案?
若要在 WPF 中直接檢視 PDF 文件,您可以使用原生的 WebBrowser 控制項。 此控件利用 Internet Explorer 渲染引擎,可在您的 WPF 應用程式中無縫顯示 PDF 檔案。 當您需要在顯示 PDF 內容時維持一致的使用者介面時,WebBrowser 控制項特別有用。
:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-wpf-viewer.cs
// 此程式碼片段無法顯示!
' 此程式碼片段無法顯示!
WPF 應用程式亦可利用 IronPDF 的渲染選項,自訂 PDF 的顯示方式。 您可能還希望實作頁面旋轉功能,以提升瀏覽的靈活性。
如何在 Windows Forms 中檢視 PDF 檔案?
若要在 Windows Forms 應用程式中直接檢視 PDF 文件,WebBrowser 控制項也是不錯的選擇。與 WPF 類似,此控制項提供了一種簡便的方式,可將 PDF 檢視功能直接嵌入您的 WinForms 應用程式中,無需依賴外部元件。
:path=/static-assets/pdf/content-code-examples/how-to/net-pdf-viewer-winforms-viewer.cs
// 此程式碼片段無法顯示!
' 此程式碼片段無法顯示!
在使用 WinForms PDF 檢視器時,您可能還希望實作其他功能,例如從顯示的 PDF 中擷取文字和圖片,或新增註解以供使用者互動。
如何在系統預設的 PDF 檢視器中開啟 PDF 檔案?
若要在任何應用程式中於外部視窗開啟 PDF 檔案,請使用 System.Diagnostics.Process.Start。 此方法會使用預設的 PDF 檢視應用程式開啟 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)
此程式碼片段示範如何使用 System.Diagnostics.Process.Start 透過系統預設的 PDF 檢視器開啟 PDF 檔案。 請確保指定的檔案路徑指向您系統上的有效 PDF 檔案。 此方法特別適用於您希望利用使用者偏好的 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
其他注意事項
在您的 .NET 應用程式中實作 PDF 檢視功能時,請考慮以下額外因素:
常見問題
如何在 .NET MAUI 應用程式中顯示 PDF 檔案?
IronPDF 提供一款專為 MAUI 專案設計的完整 PDF 檢視器。您只需一行程式碼,即可透過 IronPdf.Viewer.Maui.IronPdfView 嵌入完整的 PDF 檢視器,讓您能將 PDF 檔案、串流或其他來源的 PDF 直接載入至您的 MAUI 應用程式中。
在 ASP.NET 網頁應用程式中,查看 PDF 檔案最簡單的方法是什麼?
對於 ASP.NET 應用程式,IronPDF 支援透過瀏覽器視窗或 iframe 檢視 PDF 檔案。iframe 方法特別有效,因為它能在內嵌顯示 PDF 內容的同時,維持應用程式的版面配置。您也可以利用 IronPDF 的 HTML 轉 PDF 功能,即時生成動態 PDF 檔案以供檢視。
我可以在 WPF 和 WinForms 應用程式中整合 PDF 檢視功能嗎?
是的,IronPDF 透過 WebBrowser 控制項,支援在 WPF 和 WinForms 應用程式中檢視 PDF。這讓您能夠直接在桌面應用程式中嵌入 PDF 檢視功能,同時利用 IronPDF 的進階功能,例如數位簽章、表單填寫和 PDF 壓縮。
是否可以在 .NET 應用程式中使用系統預設的 PDF 檢視器?
沒問題。IronPDF 支援透過 System.Diagnostics.Process 與系統預設的 PDF 檢視器進行整合。此方法讓您能在使用者偏好的 PDF 應用程式中開啟 PDF 檔案,同時仍可預先使用 IronPDF 來產生、處理或準備 PDF 文件。
除了檢視之外,還有哪些額外的 PDF 功能可用?
IronPDF 提供全面的 PDF 功能,包括用於文件安全的數位簽章、互動式 PDF 的表單填寫功能、用於縮小檔案大小的 PDF 壓縮、用於動態內容生成的 HTML 轉 PDF 功能,以及以各種格式儲存/匯出 PDF。

