IRONSOFTWAREHOME
CSHTML 轉 PDF (MVC Core)

// PM > Install-Package IronPdf.Extensions.Mvc.Core

public async Task<IActionResult> Persons()
{
    var persons = new List<Person>
    {
    new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
    new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
    new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
    };
    if (_httpContextAccessor.HttpContext.Request.Method == HttpMethod.Post.Method)
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render View to PDF document
        PdfDocument pdf = renderer.RenderRazorViewToPdf(_viewRenderService, "Views/Home/Persons.cshtml", persons);
        Response.Headers.Add("Content-Disposition", "inline");

        // Output PDF document
        return File(pdf.BinaryData, "application/pdf", "viewToPdfMVCCore.pdf");
    }
    return View(persons);
}
Public Async Function Persons() As Task(Of IActionResult)
    Dim persons = New List(Of Person) From {
        New Person With {.Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer"},
        New Person With {.Name = "Bob", .Title = "Mr.", .Description = "Software Engineer"},
        New Person With {.Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer"}
    }

    If _httpContextAccessor.HttpContext.Request.Method = HttpMethod.Post.Method Then
        Dim renderer As New ChromePdfRenderer()

        ' Render View to PDF document
        Dim pdf As PdfDocument = renderer.RenderRazorViewToPdf(_viewRenderService, "Views/Home/Persons.cshtml", persons)
        Response.Headers.Add("Content-Disposition", "inline")

        ' Output PDF document
        Return File(pdf.BinaryData, "application/pdf", "viewToPdfMVCCore.pdf")
    End If

    Return View(persons)
End Function
NuGet Download
PM > Install-Package IronPdf
CSHTML 轉 PDF (MVC Core)

CSHTML 轉 PDF (MVC Core)

此程式碼範例展示了將視圖轉換為PDF的過程。

兩個套件,IronPdf,協同工作以實現視圖到PDF的渲染。 IronPdf的擴展,啟用將視圖渲染為PDF。

使用RenderRazorViewToPdf方法將視圖渲染為PDF。 此方法需要一個IRazorViewRenderer,".cshtml"文件的路徑以及顯示在".cshtml"文件上的資料。請存取如何在ASP.NET Core MVC中將視圖轉換為PDF文章以了解更多。

此操作還使您能夠存取RenderingOptions類提供的所有功能,例如在PDF中應用IronPDF頁碼,新增IronPDF的文字和HTML頁眉和頁腳,並自定義PDF紙張大小。生成的PDF文件可以根據需要進一步編輯或導出。

學習如何在ASP.NET MVC Core中將CSHTML視圖轉換為PDF。
在 GitHub 上查看

準備開始了嗎?

Nuget Downloads 21,105,021版本:2026.9剛剛發布

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 Iron Suite
預訂您的免費現場演示
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起