如何在ASP.NET Core中显示PDF文件
PDF文件被广泛用于文档共享和数据展示。 在Web应用程序中,经常需要直接在浏览器中向用户展示PDF文件。 ASP.NET Core应用程序提供了多种选项来实现这种功能,其中一个流行的用于处理PDF文件的库是IronPDF。
IronPDF是一个强大的.NET库,使开发人员能够轻松创建、编辑和操作PDF。 本文将探讨如何在ASP.NET Core的PDF查看器应用程序中使用IronPDF显示PDF文件。 它将介绍设置必要组件的步骤,并提供示例逻辑以演示ASP.NET Core PDF查看器的集成。
环境设置
要开始,请确保您具备以下先决条件:
- Visual Studio:安装最新版本的Visual Studio或您选择的任何其他兼容的集成开发环境 (IDE)。
- IronPDF库:从IronPDF官方网站或通过NuGet包管理器获取IronPDF库。
NuGet包管理器
- .NET Core应用程序:确保您对< a href="/how-to/net-pdf-viewer/">ASP.NET Core设置说明有基本的了解,并已在您的开发机器上安装。
一旦您设置了环境,让我们深入了解使用IronPDF在ASP.NET Core应用程序中显示PDF文件的步骤。
创建一个新项目
- 打开Visual Studio并创建一个新的 ASP.NET Core Web App 项目。
Web应用程序
- 选择"ASP.NET Core Web App"模板。
.NET框架
- 选择所需的项目设置并点击"创建"以生成新项目。
添加IronPDF库
要在您的项目中使用IronPDF,您需要添加IronPDF库引用。
- 在解决方案资源管理器中右键点击项目,选择"为解决方案管理NuGet包..."
NuGet包管理器
- 在NuGet包管理器中搜索"IronPDF"并安装最新版本的包。
NuGet包管理器 - 解决方案资源管理器
使用ASP.NET Core网页创建PDF
要从服务器端的ASP.NET Core网页创建一个PDF,请按以下步骤操作:
解决方案资源管理器
步骤1 添加IronPDF命名空间
打开要转换为PDF的ASP.NET Core网页的源文件路径。 在代码隐藏文件 (Index.cshtml.cs) 中,在顶部添加IronPdf命名空间:
using IronPdf;using IronPdf;Imports IronPdf步骤2 将Razor页面转换为PDF
在OnGet函数中,添加以下代码:
public FileContentResult OnGet()
{
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the current Razor page to a PDF document
PdfDocument pdf = renderer.RenderRazorToPdf(this);
// Add HTTP header to display PDF in the browser
Response.Headers.Add("Content-Disposition", "inline");
// Return the PDF file to the client
return File(pdf.BinaryData, "application/pdf");
}public FileContentResult OnGet()
{
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the current Razor page to a PDF document
PdfDocument pdf = renderer.RenderRazorToPdf(this);
// Add HTTP header to display PDF in the browser
Response.Headers.Add("Content-Disposition", "inline");
// Return the PDF file to the client
return File(pdf.BinaryData, "application/pdf");
}Public Function OnGet() As FileContentResult
' Create a new instance of ChromePdfRenderer
Dim renderer As New ChromePdfRenderer()
' Render the current Razor page to a PDF document
Dim pdf As PdfDocument = renderer.RenderRazorToPdf(Me)
' Add HTTP header to display PDF in the browser
Response.Headers.Add("Content-Disposition", "inline")
' Return the PDF file to the client
Return File(pdf.BinaryData, "application/pdf")
End Function通过仅一行代码,Razor页面将通过RenderRazorToPdf方法转换为PDF文档。
要实现这一功能,需要安装IronPDF.Extensions.Razor NuGet包。
步骤3 显示或下载PDF
默认情况下,代码将在浏览器中显示PDF文档。 如果您希望下载PDF,请修改代码如下:
return File(pdf.BinaryData, "application/pdf", "razorPageToPDF.pdf");return File(pdf.BinaryData, "application/pdf", "razorPageToPDF.pdf");Return File(pdf.BinaryData, "application/pdf", "razorPageToPDF.pdf")此代码将把ASP.NET网页的PDF文件下载到您的本地"下载"文件夹中。
Razor页面转PDF
在ASP.NET Core中加载和显示PDF文件
接下来,本节将探讨使用IronPDF在ASP.NET Core应用程序中生成和显示PDF文件的不同方法。
从URL生成PDF
IronPDF通过生成HTML文件并将其转换为PDF,简化了创建PDF文档的过程。 以下代码演示了如何从URL生成PDF文件:
// Render a PDF from a URL
using var pdf = new IronPdf.ChromePdfRenderer().RenderUrlAsPdf("https://www.google.co.in/");
// Read the File as Byte Array
byte[] bytes = pdf.BinaryData;
// Convert File to Base64 string and send to Client
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);// Render a PDF from a URL
using var pdf = new IronPdf.ChromePdfRenderer().RenderUrlAsPdf("https://www.google.co.in/");
// Read the File as Byte Array
byte[] bytes = pdf.BinaryData;
// Convert File to Base64 string and send to Client
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);' Render a PDF from a URL
Dim pdf = (New IronPdf.ChromePdfRenderer()).RenderUrlAsPdf("https://www.google.co.in/")
' Read the File as Byte Array
Dim bytes() As Byte = pdf.BinaryData
' Convert File to Base64 string and send to Client
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Return Content(base64)在上述代码中,IronPDF的ChromePdfRenderer类用于从指定URL呈现HTML内容并将其转换为PDF文档。 然后,PDF文档转换为字节数组,并作为base64字符串发送给客户端。
从HTML字符串生成PDF
IronPDF提供了一种有效的方法将HTML字符串转换为PDF文档。 下面的代码片段展示了如何从字符串生成PDF文件:
// Render a PDF from an HTML string
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");// Render a PDF from an HTML string
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");' Render a PDF from an HTML string
Dim pdf = (New IronPdf.ChromePdfRenderer()).RenderHtmlAsPdf("<h1>Hello world!!</h1>")在上述示例中,RenderHtmlAsPdf方法用于呈现HTML字符串并将其转换为PDF文档。 生成的PDF可以根据应用程序的要求进一步处理或保存。
Web应用程序输出
从HTML文件生成PDF
IronPDF还支持将HTML文件或CSS文件转换为PDF文档示例。 以下代码展示了如何从HTML文件生成PDF文件:
// Render a PDF from an HTML file
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlFileAsPdf("demo.html");
// Read the file as Byte Array
byte[] bytes = pdf.BinaryData;
// Convert File to Base64 string and send to Client
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);// Render a PDF from an HTML file
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlFileAsPdf("demo.html");
// Read the file as Byte Array
byte[] bytes = pdf.BinaryData;
// Convert File to Base64 string and send to Client
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);' Render a PDF from an HTML file
Dim pdf = (New IronPdf.ChromePdfRenderer()).RenderHtmlFileAsPdf("demo.html")
' Read the file as Byte Array
Dim bytes() As Byte = pdf.BinaryData
' Convert File to Base64 string and send to Client
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Return Content(base64)在以上代码片段中,RenderHtmlFileAsPdf方法用于将指定文件名的HTML内容呈现并转换为PDF文档。 生成的PDF被转换为字节数组,并作为base64字符串发送给客户端。

使用IronPDF通过ASP.NET Web API将ASP.NET Web表单转换为PDF文件
您可以通过一行代码将ASP.NET Web Forms轻松转换为PDF格式,而不是HTML。 将此代码放在页面的代码隐藏文件中的Page_Load方法中,以便显示在页面上。
导入IronPdf命名空间
使用using关键字在代码隐藏文件中导入IronPdf命名空间。
using IronPdf;
using System;
using System.Web.UI;using IronPdf;
using System;
using System.Web.UI;IRON VB CONVERTER ERROR developers@ironsoftware.com将ASP.NET Web表单转换为PDF
在您希望转换为PDF的页面的代码隐藏文件中(例如,Default.aspx.cs),添加以下代码:
namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Convert the ASPX page to a PDF displayed in the browser
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Convert the ASPX page to a PDF displayed in the browser
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}Namespace WebApplication7
Partial Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
' Convert the ASPX page to a PDF displayed in the browser
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser)
End Sub
End Class
End NamespaceAspxToPdf类的RenderThisPageAsPdf方法将从Web API中将Web表单转换为PDF文档。
应用HTML模板
对于公司内联网和网站开发人员来说,使用模板生成PDF通常是一个常见的要求。 IronPDF使这一过程变得简单,通过允许您生成HTML模板并用数据填充。
以下是如何使用HTML模板和IronPDF生成多个定制PDF的示例:
// Define an HTML template with placeholders
string HtmlTemplate = "<p>[[NAME]]</p>";
string[] Names = { "John", "James", "Jenny" };
foreach (var name in Names)
{
// Replace placeholder in template with actual data
string HtmlInstance = HtmlTemplate.Replace("[[NAME]]", name);
// Render HTML to PDF
using (var Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance))
{
// Save the PDF with the name as filename
Pdf.SaveAs(name + ".pdf");
}
}// Define an HTML template with placeholders
string HtmlTemplate = "<p>[[NAME]]</p>";
string[] Names = { "John", "James", "Jenny" };
foreach (var name in Names)
{
// Replace placeholder in template with actual data
string HtmlInstance = HtmlTemplate.Replace("[[NAME]]", name);
// Render HTML to PDF
using (var Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance))
{
// Save the PDF with the name as filename
Pdf.SaveAs(name + ".pdf");
}
}' Define an HTML template with placeholders
Dim HtmlTemplate As String = "<p>[[NAME]]</p>"
Dim Names() As String = { "John", "James", "Jenny" }
For Each name In Names
' Replace placeholder in template with actual data
Dim HtmlInstance As String = HtmlTemplate.Replace("[[NAME]]", name)
' Render HTML to PDF
Using Pdf = Renderer.RenderHtmlAsPdf(HtmlInstance)
' Save the PDF with the name as filename
Pdf.SaveAs(name & ".pdf")
End Using
Next nameASP MVC 路由 下载本页的PDF版本
如果您使用ASP.NET MVC,您可以轻松将用户引导至PDF文件。以下是源代码应该怎样写的示例:
using IronPdf;
using System;
using System.Web.Mvc;
namespace WebApplication8.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the URL as a PDF
using (var PDF = renderer.RenderUrlAsPdf(new Uri("https://en.wikipedia.org")))
{
// Return the PDF file with a specified filename
return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf");
}
}
// Other action methods...
}
}using IronPdf;
using System;
using System.Web.Mvc;
namespace WebApplication8.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the URL as a PDF
using (var PDF = renderer.RenderUrlAsPdf(new Uri("https://en.wikipedia.org")))
{
// Return the PDF file with a specified filename
return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf");
}
}
// Other action methods...
}
}Imports IronPdf
Imports System
Imports System.Web.Mvc
Namespace WebApplication8.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As IActionResult
' Create a new instance of ChromePdfRenderer
Dim renderer As New ChromePdfRenderer()
' Render the URL as a PDF
Using PDF = renderer.RenderUrlAsPdf(New Uri("https://en.wikipedia.org"))
' Return the PDF file with a specified filename
Return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf")
End Using
End Function
' Other action methods...
End Class
End Namespace向PDF文档添加封面
要为现有的PDF文档添加封面页或封底页,您可以使用IronPDF的合并功能。 下面是一个例子:
using (var PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/"))
{
// Merge the cover page with the main PDF
using (var Merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), PDF))
{
// Save the combined PDF
Merged.SaveAs("Combined.Pdf");
}
}using (var PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/"))
{
// Merge the cover page with the main PDF
using (var Merged = PdfDocument.Merge(new PdfDocument("CoverPage.pdf"), PDF))
{
// Save the combined PDF
Merged.SaveAs("Combined.Pdf");
}
}Using PDF = Renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")
' Merge the cover page with the main PDF
Using Merged = PdfDocument.Merge(New PdfDocument("CoverPage.pdf"), PDF)
' Save the combined PDF
Merged.SaveAs("Combined.Pdf")
End Using
End Using向您的文档添加水印
您还可以使用C#代码为PDF文档添加水印。 下面是一个例子:
using IronPdf;
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the URL as a PDF
using (var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"))
{
// Add watermark text to all pages
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45);
// Save the watermarked PDF
pdf.SaveAs(@"C:\PathToWatermarked.pdf");
}using IronPdf;
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the URL as a PDF
using (var pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf"))
{
// Add watermark text to all pages
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45);
// Save the watermarked PDF
pdf.SaveAs(@"C:\PathToWatermarked.pdf");
}Imports IronPdf
' Create a new instance of ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
' Render the URL as a PDF
Using pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
' Add watermark text to all pages
pdf.WatermarkAllPages("<h2 style='color:red'>SAMPLE</h2>", PdfDocument.WaterMarkLocation.MiddleCenter, 50, -45)
' Save the watermarked PDF
pdf.SaveAs("C:\PathToWatermarked.pdf")
End Using使用密码保护您的PDF
您可以使用 IronPDF 对 PDF 文档进行密码加密和保护。 下面是一个例子:
using IronPdf;
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render HTML to PDF
using (var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>"))
{
// Set a password to protect the PDF
pdfDocument.Password = "strong!@#pass&^%word";
// Save the secured PDF
pdfDocument.SaveAs("secured.pdf");
}using IronPdf;
// Create a new instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render HTML to PDF
using (var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>"))
{
// Set a password to protect the PDF
pdfDocument.Password = "strong!@#pass&^%word";
// Save the secured PDF
pdfDocument.SaveAs("secured.pdf");
}Imports IronPdf
' Create a new instance of ChromePdfRenderer
Private renderer As New ChromePdfRenderer()
' Render HTML to PDF
Using pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
' Set a password to protect the PDF
pdfDocument.Password = "strong!@#pass&^%word"
' Save the secured PDF
pdfDocument.SaveAs("secured.pdf")
End Using除了上述功能外,IronPDF还提供其他功能,例如从PDF中提取图像和文本与OCR,将图表渲染为PDF,向PDF添加条形码,使用密码提高文档安全性,以及PDF的水印技术,甚至处理和自定义PDF表单,等等。 使用IronPDF,您可以简化创建PDF的过程,并改进文档整体呈现。
结论
IronPDF是一款专为.NET开发人员设计的出色工具,提供广泛的功能以轻松处理.NET项目中的PDF操作。 使用IronPDF,开发人员可以提升其工作流程并简化其工作过程。 这个强大的工具提供了无数功能,使得PDF文件格式化、页面删除、页面添加等无缝连接。 它使开发人员能够根据他们的具体要求有效管理和自定义PDF文档。
IronPDF不仅在功能上表现出色,还具有在开发过程中免费使用的附加优势。 这意味着开发人员可以在项目开发阶段利用它的能力而无需承担任何费用。 通过使用IronPDF,开发人员可以提高他们的生产力,并在与PDF相关的任务中取得显著效果,最终在他们的.NET项目中提供高质量和高效的解决方案。
还有许多其他有用的库,如IronPDF用于处理PDF文档,IronXL用于处理Excel文档,而IronOCR用于处理OCR。 目前,您只需购买完整的Iron Suite,即可以两库的价格获得所有五个库。访问我们的Iron Software许可信息以获取更多详细信息。
常见问题解答
如何在 ASP.NET Core Web 应用程序中显示 PDF 文件?
您可以使用 IronPDF 库在 ASP.NET Core Web 应用程序中显示 PDF 文件。首先通过 NuGet 包管理器安装 IronPDF 包,然后使用 RenderRazorToPdf 或 RenderHtmlAsPdf 等方法将 Razor 页面或 HTML 字符串转换为用于显示的 PDF 文档。
设置 ASP.NET Core 项目以显示 PDF 的步骤是什么?
要设置用于显示 PDF 的 ASP.NET Core 项目,请安装 Visual Studio,创建一个新的 ASP.NET Core Web 应用项目,通过 NuGet 添加 IronPDF 库,并使用其方法在应用程序中渲染和显示 PDF 文件来进行集成。
如何使用 C# 将 HTML 字符串转换为 PDF?
要在 C# 中将 HTML 字符串转换为 PDF,请使用 IronPDF 的 RenderHtmlAsPdf 方法。这使您可以将 HTML 内容渲染为 PDF 文档,然后在 ASP.NET Core 应用程序中显示。
我可以将 ASP.NET Razor 页面转换为 PDF 吗?
是的,您可以使用 IronPDF 将 ASP.NET Razor 页面转换为 PDF。在代码隐藏文件中添加 IronPDF 命名空间,并使用 RenderRazorToPdf 方法将 Razor 页面渲染为 PDF 文档。
如何向 PDF 文档添加封面页?
您可以使用 IronPDF 通过将附加的 HTML 页面或文件渲染为 PDF 文档的第一页来添加封面页,然后将其与主 PDF 内容合并。
是否可以使用 C# 合并多个 PDF 文档?
是的,IronPDF 提供合并多个 PDF 文档的功能。您可以使用 PdfDocument.Merge 方法将多个 PDF 合并为一个文档。
如何为 PDF 的所有页面应用水印?
要为 PDF 的所有页面应用水印,请使用 IronPDF 的 WatermarkAllPages 方法。这使您可以将文本或图像作为水印覆盖在文档的每一页上。
使用 IronPDF 对于 .NET 开发者有哪些好处?
IronPDF 对 .NET 开发者非常有益,因为它提供了一个强大且灵活的 PDF 操作库,包括 PDF 转换、编辑和在 Web 应用程序中显示 PDF 它提高了生产力,简化了工作流程,是开发者工具包中的一个有价值的工具。
如何在 C# 中使用密码保护 PDF 文档?
在 C# 中用密码保护 PDF 文档,使用 IronPDF 渲染您的内容,然后在保存之前在 PdfDocument 对象上设置密码。这确保只有授权用户可以打开该文档。
IronPDF 是否支持 .NET 10?如何在 .NET 10 项目中使用它?
是的——IronPDF 完全兼容 .NET 10。该库支持 .NET 10(以及 .NET 9、8、7 等),无需特殊配置即可在 Web、桌面、控制台和云环境中使用。要使用它,只需在 .NET 10 项目中通过 NuGet 引用 IronPDF,然后像往常一样调用诸如ChromePdfRenderer().RenderHtmlAsPdf(...)之类的方法即可。
.NET 10 有哪些新功能可以提升 IronPDF 的性能?
是的——.NET 10 引入了多项性能增强功能,例如减少堆内存分配、结构体逃逸分析以及数组接口方法去虚拟化,这些功能共同提升了运行时效率。IronPDF 尤其受益于这些改进,尤其是在 HTML 转 PDF 渲染以及多线程或高并发场景下。






