IRONSOFTWAREHOME
CSHTML 到 PDF (MVC 框架)

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

public ActionResult 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 (HttpContext.Request.HttpMethod == "POST")
    {
        // Provide the path to your view file
        var viewPath = "~/Views/Home/Persons.cshtml";
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Render Razor view to PDF document
        PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
        Response.Headers.Add("Content-Disposition", "inline");

        // View the PDF
        return File(pdf.BinaryData, "application/pdf");
    }
    return View(persons);
}
Public Function Persons() As ActionResult
    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 HttpContext.Request.HttpMethod = "POST" Then
        ' Provide the path to your view file
        Dim viewPath = "~/Views/Home/Persons.cshtml"
        Dim renderer As New ChromePdfRenderer()

        ' Render Razor view to PDF document
        Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons)
        Response.Headers.Add("Content-Disposition", "inline")

        ' View the PDF
        Return File(pdf.BinaryData, "application/pdf")
    End If

    Return View(persons)
End Function
NuGet Download
PM > Install-Package IronPdf
CSHTML 到 PDF (MVC 框架)

CSHTML 到 PDF (MVC 框架)

这个代码示例说明如何将视图转换为PDF文档。

为实现这一点,我们利用两个软件包:IronPdf.Extensions.Mvc.Framework 和 IronPdf,它们用于方便将视图渲染为PDF。 IronPdf.Extensions.Mvc.Framework 包扩展了 IronPdf 的功能,特别是能够将视图渲染为PDF。

要执行转换,请使用 RenderView 方法。 此方法需要几个关键输入:一个 HttpContext,".cshtml" 文件的路径,以及填充 ".cshtml" 模板所需的数据。 通过调用 Persons 这个动作,您可以无缝地将当前视图渲染为PDF文档。

此外,您可以访问由 RenderingOptions 类提供的一整套功能。 这些功能包括能够使用IronPDF添加页码、插入使用IronPDF的文本和HTML页眉和页脚,以及根据需求自定义PDF纸张大小。 您可以灵活地进行进一步修改或导出生成的PDF文档。

探索 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 起