C# 导出到 PDF [代码示例教程]
IronPDF 是一款 C# PDF库 它允许您使用 C# 将 HTML 保存为 PDF。 它还允许 C# / VB 开发人员以编程方式编辑 PDF 文档。
如何在 C# 中导出 PDF
- 下载并安装导出为 PDF 的 C# 库
- 探索 PdfDocument 文档,了解对导出 PDF 进行数字签名的方法
- 使用 System.IO.MemoryStream 将 PDF 保存到内存中
- 以二进制数据而非 HTML 形式向网络提供 PDF 文件
- 将 PDF 导出为文件
开始在您的项目中使用IronPDF,并立即获取免费试用。
查看 IronPDF 上 Nuget 用于快速安装和部署。它有超过800万次下载,正在使用C#改变PDF。
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"}
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()