IRONSOFTWAREHOME

将 PDF 转换为 Base64.

Curtis Chau
Curtis Chau
Updated: 2026年4月24日
如何将 PDF 转换为 Base64?

PdfDocument 对象不包含获取Base64的直接属性。然而,您可以获取字节数组,然后用于获取Base64字符串。

using System;
using SomePdfLibrary; // Make sure to import the library used for handling PDF files

class Program
{
    static void Main()
    {
        // Create a PdfDocument object for the specified PDF file
        var pdf = new PdfDocument("MyPDF.pdf");
        
        // Get the binary data (byte array) from the PDF document
        var byteArray = pdf.BinaryData;
        
        // Convert the byte array to a Base64 string
        var base64Result = Convert.ToBase64String(byteArray);
        
        // Output the Base64 result
        Console.WriteLine("Base64 of PDF: " + base64Result);
    }
}
  • PdfWriter:此对象负责写入PDF文件。它作用于文件路径,并写入有效PDF文档所需的结构。
  • PdfDocument 初始化: PdfDocument 对象使用您要转换的PDF文件名进行初始化。 将 SomePdfLibrary 替换为您正在使用的实际库。
  • BinaryData 检索: 它检索给定PDF的二进制数据(作为字节数组)。
  • Base64 转换: 使用 Convert.ToBase64String 方法将字节数组转换为Base64字符串。
  • 输出 Base64 字符串: 将 Base64 编码字符串打印到控制台以供验证。
Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

...
阅读更多

准备开始了吗?

Nuget Downloads 20,878,335版本:2026.9刚刚发布

立即获取您的免费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

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户