C# 导出到 PDF [代码示例教程]

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 是一款 C# PDF库 它允许您使用 C# 将 HTML 保存为 PDF。 它还允许 C# / VB 开发人员以编程方式编辑 PDF 文档。


适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

适用于PDF的C# NuGet库

安装使用 NuGet

Install-Package IronPdf
Java PDF JAR

下载 DLL

下载DLL

手动安装到你的项目中

开始在您的项目中使用IronPDF,并立即获取免费试用。

第一步:
green arrow pointer

查看 IronPDFNuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。

适用于PDF的C# NuGet库 nuget.org/packages/IronPdf/
Install-Package IronPdf

考虑安装 IronPDF DLL 直接。下载并手动安装到您的项目或GAC表单中: IronPdf.zip

手动安装到你的项目中

下载DLL

保存 PDF 的选项

如何将 PDF 保存到磁盘

使用 PdfDocument.SaveAs 方法将 PDF 保存到磁盘。

你会发现这种方法支持添加密码保护。查看以下文章,了解更多有关对导出的 PDF 进行数字签名的信息:'对 PDF 文档进行数字签名.'

如何用 C# 将 PDF 文件保存到 MemorySteam 中 (System.IO.MemoryStream)

"(《世界人权宣言》) IronPdf.PdfDocument.Stream 属性,使用 System.IO.MemoryStream 将 PDF 保存到内存中。

如何保存为二进制数据

如何保存为二进制数据 IronPdf.PdfDocument.BinaryData 属性将 PDF 文档导出为内存中的二进制数据。

这将 PDF 输出为字节数组(ByteArray),在 C# 中表示为 byte [].

如何从网络服务器向浏览器提供服务

要向网络提供 PDF,我们需要将其作为二进制数据而不是 HTML 发送。

MVC PDF 导出

// Send MyPdfDocument.Stream to this method
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
// Send MyPdfDocument.Stream to this method
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
' Send MyPdfDocument.Stream to this method
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "file.pdf"}
VB   C#

ASP.NET PDF 导出

byte [] Binary = MyPdfDocument.BinaryData;
Response.Clear();
Response.ContentType = "application/octet-stream";
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
Response.Flush();      
byte [] Binary = MyPdfDocument.BinaryData;
Response.Clear();
Response.ContentType = "application/octet-stream";
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
Response.Flush();      
Dim Binary() As Byte = MyPdfDocument.BinaryData
Response.Clear()
Response.ContentType = "application/octet-stream"
Context.Response.OutputStream.Write(Binary, 0, Binary.Length)
Response.Flush()
VB   C#