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、添加文本和HTML页眉页脚与IronPDF以及自定义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 起