C#中pdf文件与base64字符串的相互转换

1、pdf文件转换为base64字符串

private string GetBase64StringByPdf(string file)

{

byte\[\] bytes = null;

FileStream fileStream = new FileStream(file, FileMode.Open);

bytes = new bytefileStream.Length;

fileStream.Read(bytes, 0, bytes.Length);

fileStream.Close();

return Convert.ToBase64String(bytes);

}

//这里是你的pdf路径

string bytes=GetBase64StringByPdf("E:\\PDF文件\\测试1.pdf");

2、base64字符串转换为pdf

public string Base64StringToPdf(string base64String, string filePath)

{

byte\[\] pdfBytes = Convert.FromBase64String(base64String);

File.WriteAllBytes(filePath, pdfBytes);

return filePath;

}

//使用方法

string base64string="这里是base64字符串";

string filepath="你要写入字符流的pdf";

//得到pdf

string pdf=GetBase64StringByPdf(base64string,filepath);

相关推荐
兰令水14 分钟前
hot100【acm版】【2026.7.14打卡-java版本】
java·数据结构·算法·leetcode·面试
thefool1122661 小时前
Java 数组是引用类型
java
geovindu1 小时前
CSharp: Bridge Pattern
开发语言·后端·桥接模式·结构型模式
এ慕ོ冬℘゜1 小时前
深入 JavaScript BOM:掌握 window 对象的窗口控制魔法
开发语言·javascript·ecmascript
2501_941982052 小时前
企业微信二次开发:如何用 Python 搭建通用的 Webhook 实时消息接收
开发语言·python·企业微信
cfm_29142 小时前
Spring监听器
java·spring boot·后端
逝水无殇2 小时前
C# 特性详解
开发语言·后端·c#
SamDeepThinking2 小时前
Java面向对象在JVM里怎么实现
java·后端·面试
灵晔君2 小时前
【C++】二叉搜索树
开发语言·c++
LongtengGensSupreme3 小时前
C#图像内存高速拷贝:使用Marshal.AllocHGlobal与ArrayPool实现内存拷贝的几个方法
开发语言·数码相机·c#