跳過到頁腳內容
使用IRONPDF

在 ASP.NET Core MVC 中建立 PDF 檢視器 | 教學

使用 IronPDF 基於 Chrome 的渲染引擎建立 ASP.NET Core MVC PDF 檢視器,以在瀏覽器中內嵌顯示 PDF 文件,從 HTML 內容產生動態 PDF,並控制使用者是查看還是下載文件——所有這些都不需要外部插件或依賴項。

現代瀏覽器都內建了 PDF 檢視器,當 Web 應用程式提供具有正確 MIME 類型的 PDF 檔案時,該檢視器會自動啟動。 這樣就無需使用第三方工具或插件,使用者可以直接在瀏覽器中顯示 PDF 文件。 IronPDF是一個 .NET PDF 庫,它採用基於 Chrome 的渲染引擎,可以輕鬆地在 ASP.NET Core MVC 應用程式中產生、渲染和顯示 PDF 檔案。

!{--01001100010010010100001001010010010000010101001001011001010111110100011101000101010101 01000101111101010011010101000100000101010010010101000100010101000100010111110101011101001000110 1010101000100100001011111010100000101001001001111010001000101010101010000110101010100101010101011 10101010001010010010010010010000010100110001011111010000100100110001001111101000011010010111111010000110100101110--

如何在 ASP.NET Core MVC 專案中安裝 IronPDF?

在 ASP.NET Core MVC 中建立 PDF 檢視器之前,需要將 IronPDF 新增到您的專案中。 最快的方法是透過 Visual Studio 中的 NuGet 套件管理器,或透過命令列使用 .NET CLI 或套件管理器控制台。

Install-Package IronPdf
dotnet add package IronPdf
Install-Package IronPdf
dotnet add package IronPdf
SHELL

安裝完軟體包後,將 IronPdf 命名空間新增至您的控制器檔案中,即可開始產生和提供 PDF 文件。 IronPDF 的目標平台是 .NET 8 和 .NET 10,因此無需任何額外配置即可與最新的 ASP.NET Core 版本搭配使用。

對於需要離線安裝或特定版本鎖定的項目,您也可以直接下載 NuGet 套件並將其新增為本機來源。 IronPDF 許可頁面涵蓋了試用版和正式版授權選項,如果您在正式上線前需要這些選項,可以參考這些許可。

現代瀏覽器如何顯示PDF檔案?

Chrome、Firefox、Edge 和 Safari 等現代瀏覽器都內建了 PDF 檢視器功能。 當您的 ASP.NET Core 應用程式傳回內容類型為 application/pdf 的文件時,瀏覽器會內嵌呈現 PDF 文檔,而無需 Adobe Acrobat 或外部插件。 這款內建的 PDF 檢視器支援文字選擇、列印、縮放控制、書籤和頁面導航,創造了使用者已經熟悉的文件檢視體驗。

為了安全地提供現有文件,最佳實踐是使用託管環境來定位它們,而不是依賴在開發和生產之間可能會發生變化的目錄路徑。 對於大型文檔,使用文件流比加載整個位元組數組更節省記憶體。

using Microsoft.AspNetCore.Mvc;

public class DocumentController : Controller
{
    public IActionResult ViewPdf()
    {
        // Path to an existing PDF file in the wwwroot folder
        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "documents", "sample.pdf");
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        // Return file for inline browser display
        return File(fileBytes, "application/pdf");
    }
}
using Microsoft.AspNetCore.Mvc;

public class DocumentController : Controller
{
    public IActionResult ViewPdf()
    {
        // Path to an existing PDF file in the wwwroot folder
        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "documents", "sample.pdf");
        byte[] fileBytes = System.IO.File.ReadAllBytes(path);
        // Return file for inline browser display
        return File(fileBytes, "application/pdf");
    }
}
Imports Microsoft.AspNetCore.Mvc

Public Class DocumentController
    Inherits Controller

    Public Function ViewPdf() As IActionResult
        ' Path to an existing PDF file in the wwwroot folder
        Dim path As String = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "documents", "sample.pdf")
        Dim fileBytes As Byte() = System.IO.File.ReadAllBytes(path)
        ' Return file for inline browser display
        Return File(fileBytes, "application/pdf")
    End Function
End Class
$vbLabelText   $csharpLabel

PDF檔案在瀏覽器中顯示時是什麼樣子?

一個關於"什麼是 PDF?"的 PDF 文件在本機 7162/Pdf/ViewPdf 的網頁瀏覽器中顯示,以 PDF 檢視器介面呈現格式化的文字內容,並帶有縮放控制項和導覽選項。

上面的程式碼從伺服器讀取一個現有的 PDF 文件,並將其傳回給瀏覽器。 File() 方法接受一個位元組數組和一個內容類型,指示瀏覽器的文檔檢視器以內聯方式渲染內容。這種方法適用於桌面和行動裝置上的所有現代瀏覽器,為所有使用者提供一致的體驗。

對於更高級的場景,您可能需要從記憶體或Azure Blob 儲存裝置載入 PDF ,這樣可以提高可擴充性並減少伺服器儲存需求。 微軟關於在 ASP.NET Core 中提供靜態文件的文件涵蓋了生產環境中文件處理的最佳實踐。

如何在 ASP.NET Core 中動態產生 PDF 文件?

靜態 PDF 文件雖然有用,但許多 Web 應用程式需要根據當前使用者或請求動態產生文件。 IronPDF 的 ChromePdfRenderer 類別使用底層真正的 Chromium 瀏覽器引擎,將 HTML 內容轉換為專業渲染的 PDF 檔案。

您可以直接在 HTML 字串中包含外部資源,例如特定主題的 CSS圖表的 JavaScript 。 渲染引擎支援現代網頁標準,包括 CSS3、JavaScript ES6+ 和網頁字體,因此產生的 PDF 看起來與瀏覽器渲染的相同 HTML 頁面完全一樣。

using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class ReportController : Controller
{
    public IActionResult GenerateReport()
    {
        var renderer = new ChromePdfRenderer();
        // HTML content with CSS styling
        string html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; padding: 40px; }
                    h1 { color: #2c3e50; }
                    .report-body { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Monthly Sales Report</h1>
                <div class='report-body'>
                    <p>Generated: " + DateTime.Now.ToString("MMMM dd, yyyy") + @"</p>
                    <p>This report contains the latest sales figures.</p>
                </div>
            </body>
            </html>";

        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class ReportController : Controller
{
    public IActionResult GenerateReport()
    {
        var renderer = new ChromePdfRenderer();
        // HTML content with CSS styling
        string html = @"
            <html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; padding: 40px; }
                    h1 { color: #2c3e50; }
                    .report-body { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Monthly Sales Report</h1>
                <div class='report-body'>
                    <p>Generated: " + DateTime.Now.ToString("MMMM dd, yyyy") + @"</p>
                    <p>This report contains the latest sales figures.</p>
                </div>
            </body>
            </html>";

        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Public Class ReportController
    Inherits Controller

    Public Function GenerateReport() As IActionResult
        Dim renderer As New ChromePdfRenderer()
        ' HTML content with CSS styling
        Dim html As String = "
            <html>
            <head>
                <style>
                    body { font-family: Arial, sans-serif; padding: 40px; }
                    h1 { color: #2c3e50; }
                    .report-body { line-height: 1.6; }
                </style>
            </head>
            <body>
                <h1>Monthly Sales Report</h1>
                <div class='report-body'>
                    <p>Generated: " & DateTime.Now.ToString("MMMM dd, yyyy") & "</p>
                    <p>This report contains the latest sales figures.</p>
                </div>
            </body>
            </html>"

        Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
        Return File(pdf.BinaryData, "application/pdf")
    End Function
End Class
$vbLabelText   $csharpLabel

PDF產生後,HTML內容會如何顯示?

! PDF 檢視器顯示一份每月銷售報告,其中包含格式化的標題文字和生成日期,演示了透過 IronPDF 將 HTML 轉換為 PDF 並套用自訂 CSS 樣式的過程。

此範例示範了 IronPDF 如何將HTML 字串轉換為 PDF文件。 ChromePdfRenderer 使用基於 Chromium 的引擎,確保準確的 CSS 渲染和 JavaScript 支援。 產生的 PDF 文件保留了 HTML 中定義的所有樣式,因此非常適合建立報告發票和其他需要一致格式的文件。

有關HTML 轉 PDF 轉換的更多程式碼範例,請參閱 IronPDF 的文件。 您也可以從CSHTML Razor 視圖URL甚至Markdown 內容產生 PDF。

如何控制頁面內嵌顯示與文件下載?

有時用戶需要下載 PDF 檔案而不是在瀏覽器中查看。 瀏覽器處理回應的方式取決於 Content-Disposition 標頭。 理解這一區別對於在您的應用程式中提供正確的用戶體驗至關重要。

當您在 File() 方法中省略 filename 參數時,ASP.NET Core 不會設定 Content-Disposition 標頭,因此瀏覽器使用其預設行為-通常是內嵌顯示。 當您將檔案名稱作為第三個參數提供時,ASP.NET Core 會自動新增 Content-Disposition: attachment,提示使用者儲存檔案。

using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class PdfController : Controller
{
    public IActionResult DisplayInline()
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
        renderer.RenderingOptions.MarginTop = 25;
        renderer.RenderingOptions.MarginBottom = 25;

        PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
        // Display PDF inline in browser -- no filename = inline
        return File(pdf.BinaryData, "application/pdf");
    }

    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.PaperSize = PdfPaperSize.Letter;
        renderer.RenderingOptions.EnableJavaScript = true;

        PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
        // Prompt download with specified filename
        return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

public class PdfController : Controller
{
    public IActionResult DisplayInline()
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
        renderer.RenderingOptions.MarginTop = 25;
        renderer.RenderingOptions.MarginBottom = 25;

        PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
        // Display PDF inline in browser -- no filename = inline
        return File(pdf.BinaryData, "application/pdf");
    }

    public IActionResult DownloadPdf()
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.PaperSize = PdfPaperSize.Letter;
        renderer.RenderingOptions.EnableJavaScript = true;

        PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
        // Prompt download with specified filename
        return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Public Class PdfController
    Inherits Controller

    Public Function DisplayInline() As IActionResult
        Dim renderer As New ChromePdfRenderer()
        renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait
        renderer.RenderingOptions.MarginTop = 25
        renderer.RenderingOptions.MarginBottom = 25

        Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")
        ' Display PDF inline in browser -- no filename = inline
        Return File(pdf.BinaryData, "application/pdf")
    End Function

    Public Function DownloadPdf() As IActionResult
        Dim renderer As New ChromePdfRenderer()
        renderer.RenderingOptions.PaperSize = PdfPaperSize.Letter
        renderer.RenderingOptions.EnableJavaScript = True

        Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page")
        ' Prompt download with specified filename
        Return File(pdf.BinaryData, "application/pdf", "webpage-report.pdf")
    End Function
End Class
$vbLabelText   $csharpLabel

何時應該使用內聯顯示而不是下載顯示?

螢幕截圖顯示了維基百科主頁轉換為 PDF 格式後,在瀏覽器視窗中以內嵌方式顯示,並帶有 PDF 檢視器控件,包括縮放、頁面導航和列印選項。

這兩個控制器操作的差異在於 File() 中的第三個參數。 這種靈活性使您的應用程式能夠根據使用者需求或業務要求支援這兩種場景。 為了增強控制,您還可以在渲染之前設定自訂 HTTP 標頭或配置紙張尺寸方向設定

有些應用程式結合了這兩種方法:一個預覽端點,用於線上顯示 PDF;以及一個單獨的下載端點,用於觸發文件保存。 這種模式使用戶在決定下載文件之前可以預覽文件內容。

如何將 Razor Pages 與 PDF 生成功能整合?

ASP.NET Core MVC 中的 Razor Pages 為實作 .NET PDF 檢視器提供了另一種方法。 頁面模型可以使用與標準 MVC 控制器中相同的 IronPDF 功能來產生和返回 PDF 檔案。 這種模式對於已經使用 Razor Pages 的應用程式來說效果很好,因為它將 PDF 生成邏輯與觸發它的頁面放在一起。

Razor Page 中的 OnGet 處理程序就像控制器操作一樣-它接收請求、執行工作並傳回結果。 ASP.NET Core 支援從頁面模型傳回 FileResult,其運作方式與從控制器返回 File() 相同。

using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

public class InvoiceModel : PageModel
{
    public IActionResult OnGet(int id)
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.MarginTop = 20;
        renderer.RenderingOptions.MarginBottom = 20;
        renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;

        // Add header and footer
        renderer.RenderingOptions.TextHeader.CenterText = "Invoice Document";
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
        renderer.RenderingOptions.TextFooter.FontSize = 10;

        string html = $@"
            <html>
            <head>
                <style>
                    body {{ font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; }}
                    h1 {{ color: #1a5490; border-bottom: 2px solid #1a5490; padding-bottom: 10px; }}
                    .invoice-details {{ margin: 20px 0; }}
                    table {{ width: 100%; border-collapse: collapse; }}
                    th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                </style>
            </head>
            <body>
                <h1>Invoice #{id}</h1>
                <div class='invoice-details'>
                    <p><strong>Date:</strong> {DateTime.Now:yyyy-MM-dd}</p>
                    <p><strong>Due Date:</strong> {DateTime.Now.AddDays(30):yyyy-MM-dd}</p>
                </div>
                <table>
                    <tr><th>Description</th><th>Amount</th></tr>
                    <tr><td>Professional Services</td><td>$1,500.00</td></tr>
                </table>
                <p style='margin-top: 40px;'>Thank you for your business!</p>
            </body>
            </html>";

        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

public class InvoiceModel : PageModel
{
    public IActionResult OnGet(int id)
    {
        var renderer = new ChromePdfRenderer();
        renderer.RenderingOptions.MarginTop = 20;
        renderer.RenderingOptions.MarginBottom = 20;
        renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;

        // Add header and footer
        renderer.RenderingOptions.TextHeader.CenterText = "Invoice Document";
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}";
        renderer.RenderingOptions.TextFooter.FontSize = 10;

        string html = $@"
            <html>
            <head>
                <style>
                    body {{ font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; }}
                    h1 {{ color: #1a5490; border-bottom: 2px solid #1a5490; padding-bottom: 10px; }}
                    .invoice-details {{ margin: 20px 0; }}
                    table {{ width: 100%; border-collapse: collapse; }}
                    th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                </style>
            </head>
            <body>
                <h1>Invoice #{id}</h1>
                <div class='invoice-details'>
                    <p><strong>Date:</strong> {DateTime.Now:yyyy-MM-dd}</p>
                    <p><strong>Due Date:</strong> {DateTime.Now.AddDays(30):yyyy-MM-dd}</p>
                </div>
                <table>
                    <tr><th>Description</th><th>Amount</th></tr>
                    <tr><td>Professional Services</td><td>$1,500.00</td></tr>
                </table>
                <p style='margin-top: 40px;'>Thank you for your business!</p>
            </body>
            </html>";

        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
        return File(pdf.BinaryData, "application/pdf");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.AspNetCore.Mvc.RazorPages

Public Class InvoiceModel
    Inherits PageModel

    Public Function OnGet(id As Integer) As IActionResult
        Dim renderer = New ChromePdfRenderer()
        renderer.RenderingOptions.MarginTop = 20
        renderer.RenderingOptions.MarginBottom = 20
        renderer.RenderingOptions.PaperSize = PdfPaperSize.A4

        ' Add header and footer
        renderer.RenderingOptions.TextHeader.CenterText = "Invoice Document"
        renderer.RenderingOptions.TextFooter.RightText = "Page {page} of {total-pages}"
        renderer.RenderingOptions.TextFooter.FontSize = 10

        Dim html As String = $"
            <html>
            <head>
                <style>
                    body {{ font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; }}
                    h1 {{ color: #1a5490; border-bottom: 2px solid #1a5490; padding-bottom: 10px; }}
                    .invoice-details {{ margin: 20px 0; }}
                    table {{ width: 100%; border-collapse: collapse; }}
                    th, td {{ padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }}
                </style>
            </head>
            <body>
                <h1>Invoice #{id}</h1>
                <div class='invoice-details'>
                    <p><strong>Date:</strong> {DateTime.Now:yyyy-MM-dd}</p>
                    <p><strong>Due Date:</strong> {DateTime.Now.AddDays(30):yyyy-MM-dd}</p>
                </div>
                <table>
                    <tr><th>Description</th><th>Amount</th></tr>
                    <tr><td>Professional Services</td><td>$1,500.00</td></tr>
                </table>
                <p style='margin-top: 40px;'>Thank you for your business!</p>
            </body>
            </html>"

        Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
        Return File(pdf.BinaryData, "application/pdf")
    End Function
End Class
$vbLabelText   $csharpLabel

PDF自訂有哪些渲染選項?

PDF 檢視器以專業格式顯示第 20 號發票,包括樣式化的標題、到期日資訊和感謝訊息,並在深色主題瀏覽器介面中顯示

此 Razor Pages 範例示範了 OnGet 處理程序如何從 URL 參數產生 PDF。 RenderingOptions 屬性允許對邊距、頁面方向和其他佈局設定進行精細控制。 您也可以新增頁首和頁腳,配置頁碼,或設定自訂紙張尺寸

透過 RenderingOptions 提供的進階功能包括浮水印PDF 壓縮和數位簽章。 有關每個可用選項的完整詳細信息,請參閱IronPDF 渲染選項參考

如何處理PDF安全性與存取控制?

生產應用程式通常需要限制誰可以檢視或修改 PDF 文件。 IronPDF 提供對PDF 密碼和權限的內建支持,讓您可以加密文件、要求輸入密碼才能開啟文檔,或限制列印和複製。

在從控制器返回任何 PdfDocument 物件之前,您可以對其套用安全設定。 無論 PDF 是從 HTML 產生的、從磁碟載入的還是從資料庫中檢索的,這種方法都適用。

IronPDF 中提供的常用 PDF 安全選項
安全功能 IronPDF屬性 用例
所有者密碼 OwnerPassword 防止未經授權的權限更改
使用者密碼 UserPassword 需要密碼才能開啟文檔
停用列印 AllowUserPrinting 阻止收件人列印文件
停用複製 AllowUserCopyPasteContent 從PDF中提取文字塊
數位簽名 PdfSignature 核實文件真實性

對於合約、財務報表或醫療記錄等敏感文件,結合使用者密碼和權限限制,可以精確控制收件者對文件的操作。 PDF /A 合規模式是另一個值得考慮的選擇,適用於需要長期保存文件完整性長達數十年的歸檔場景。

如何優化PDF生成性能?

PDF 生成涉及 CPU 和記憶體工作,因此在高流量應用程式中效能至關重要。 IronPDF 中的非同步渲染 API可讓您將 PDF 建立任務卸載到後台線程,而不會阻塞請求管道。這在單一請求中產生多個 PDF 或處理批次作業時尤其有用。

對於在 Linux 或 Docker 容器中進行的生產部署,IronPDF 支援跨平台執行,無需任何 Windows 特定的依賴項。 IronPDF Linux 安裝指南Docker 設定參考涵蓋了設定步驟。 Mozilla PDF.js 專案和原生瀏覽器檢視器都與 IronPDF 產生的任何 PDF 檔案相容,因此在顯示器方面也具有靈活性。

以下幾種做法可以提高生產效率:

  • 盡可能重複使用 ChromePdfRenderer 實例,而不是每次請求都建立新實例,因為渲染器初始化會帶來一些開銷。
  • 使用非同步方法(例如 RenderHtmlAsPdfAsync)在 Chromium 引擎渲染時釋放線程。
  • 當重複要求相同文件時,使用IMemoryCache或分散式快取快取產生的 PDF。
  • 對於非常大的文檔,請考慮串流輸出,而不是將整個位元組數組緩衝到記憶體中。

異步渲染在實踐中是如何運作的?

非同步 API 與同步版本類似,但傳回 Task<PdfDocument>。 在 async 控制器操作中等待結果,保持線程池空閒以處理其他傳入的請求,同時繼續渲染。 在高負載場景下,從同步渲染切換到非同步渲染通常可以減少執行緒爭用,並提高並發負載下的整體回應時間。

下一步計劃是什麼?

在 ASP.NET Core MVC 中建立 PDF 檢視器,將瀏覽器原生顯示功能與 IronPDF 的生成功能結合。 現代瀏覽器中內建的 PDF 檢視器會在 ASP.NET 控制器傳回具有正確 MIME 類型的檔案時自動處理顯示、列印和導航。 IronPDF 負責產生部分——將 HTML、URL 或現有文件轉換為格式良好的 PDF 文檔,並完全支援 CSS、JavaScript、頁首、頁尾和安全設定。

本指南涵蓋的主要功能:

  • 使用 File() 方法和 application/pdf MIME 類型以內聯方式提供現有 PDF 文件
  • 使用 ChromePdfRenderer 從 HTML 字串產生動態 PDF
  • 透過 Content-Disposition 標頭控制內嵌顯示與檔案下載
  • 透過 RenderingOptions 新增頁首、頁尾、邊距和安全設定
  • 使用 Razor Pages 取代 MVC 控制器產生 PDF 文件 對敏感文件應用密碼保護和權限限制
  • 透過非同步渲染和回應快取提高效能

從這裡,您可以探索更進階的工作流程,例如合併多個 PDF建立可填寫表單或從CSHTML Razor 視圖產生 PDF。 IronPDF 文件涵蓋了所有功能,並提供了可運行的程式碼範例。

立即開始免費試用,探索 IronPDF 的全部功能,或購買許可證用於生產環境。

常見問題解答

如何在 ASP.NET Core MVC 應用程式中顯示 PDF 檔案?

您可以使用 IronPDF for .NET 在 ASP.NET Core MVC 應用程式中顯示 PDF 檔案。它允許您使用現代的內建 PDF 檢視器直接在瀏覽器中產生、渲染和顯示 PDF 檔案。

在瀏覽器中檢視 PDF 是否需要第三方外掛程式?

不,現代瀏覽器已內建 PDF 檢視器,可在提供具有正確 MIME 類型的 PDF 檔案時自動啟動。IronPDF 可協助確保您的 PDF 正確提供。

在 ASP.NET Core MVC 中使用 IronPDF 有何優勢?

IronPDF for .NET 是一個 .NET PDF 函式庫,可簡化在 ASP.NET Core MVC 應用程式中產生和渲染 PDF 文件的流程,提高生產力並簡化 PDF 管理。

IronPDF 可與現有的瀏覽器 PDF 檢視器搭配使用嗎?

是的,IronPDF 可與現有的瀏覽器 PDF 檢視器無縫配合,確保 PDF 以正確的 MIME 類型提供,以便在瀏覽器中自動顯示。

IronPDF 是否經常更新?

是的,IronPDF 是一個經常更新的 .NET PDF 函式庫,提供在 ASP.NET Core MVC 應用程式中處理 PDF 文件的最新功能和改進。

IronPDF 如何處理 Web 應用程式中的 PDF 生成?

IronPDF 提供從各種內容類型產生 PDF 的強大功能,讓開發人員可以在 Web 應用程式中建立動態且互動的 PDF 文件。

應使用何種 MIME 類型來提供 PDF 檔案?

為了確保在瀏覽器中正常顯示,PDF 檔案應使用 MIME 類型「application/pdf」。IronPDF 可協助有效管理這方面的工作。

我可以自訂 IronPDF 中的 PDF 渲染方式嗎?

是的,IronPDF 提供廣泛的自訂選項來渲染 PDF,讓您可以量身訂做輸出,以符合特定的設計和功能需求。

IronPDF 是否僅支援 ASP.NET Core MVC 應用程式?

雖然 IronPDF 非常適合 ASP.NET Core MVC 應用程式,但它也具有多樣性,可搭配其他 .NET 應用程式來處理 PDF 功能。

Curtis Chau
技術作家

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

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我