新手记录,参考了很多大佬但是链接都有点找不到了,还请见谅。欢迎大家指出错误。以及3方案可以成功2方案为什么不可成功。
1.PDF文件转为bytes,在转Stream传出去,在其他地方接收,将Stream转为bytes写入文件。这个可行。
//PDF文件转Stream流
public Stream FileToStream(string fileName) //fileName 文件地址/文件全名(xxx.pdf)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
//读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
//把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
//Stream流转PDF文件
public void StreamToFile(Stream stream, string fileName) //stream PDF转化的流 fileName 文件地址/文件全名(xxx.pdf)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create,FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs,Encoding.UTF8);
bw.Write(bytes);
bw.Close();
fs.Close();
}
2.PDF文件转为Stream流,在转byte 在转16进制String字符串传出去,在其他地方接收,将16进制String字符串转为bytes写入文件。这个可不行。文件还是PDF格式(有可能是后缀的原因),但是也能打开,只是里面没有内容,空白页都没有。后生成的PDF文件比原始的那一份存储大小接近翻了一倍。用记事本打开开头是%pdf -1.x 。。。。。。发现了 就是自己向文件里写入了两次 3 中被注释的代码。
//PDF文件转Stream流
public String FileToStream(string fileName) //fileName 文件全名(xxx.pdf)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
fileStream.Position = 0;
StreamReader rs = new StreamReader(fileStream, System.Text.Encoding.UTF8);
string str = rs.ReadToEnd();
rs.Close();
return str;
}
//Stream流转PDF文件
public void StreamToFile(String str, string fileName)
{
byte[] array = System.Text.Encoding.UTF8.GetBytes(str);
FileStream fs = new FileStream(fileName, FileMode.Create,FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs,Encoding.UTF8);
bw.Writer(array);
bw.Close();
fs.Close();
}
3.PDF文件转为stream流,在转byte 在转1String传出去,在其他地方接收,将String转为bytes写入文件。这个可行。性能上有点慢。
/// <summary>
/// PDF文件转字符串
/// </summary>
/// <param name="fileName">文件全称(带后缀名.pdf)</param>
/// <returns>字符串</returns>
public string FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fileStream);
Byte[] byData = br.ReadBytes((int)fileStream.Length);
string binary = byteToStr(byData);
return binary;
}
/// <summary>
/// 字符串转为PDF文件
/// </summary>
/// <param name="pdfstr">pdf文件转化成的字符串</param>
/// <param name="path">地址</param>
public void StreamToFile(string pdfstr,string path)
{
try
{
string strPdf = pdfstr;
byte[] bytes = strToByte(strPdf);
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
fileStream.Write(bytes, 0, bytes.Length);
//BinaryWriter binaryWriter = new BinaryWriter(fileStream);
// binaryWriter.Write(bytes);
// binaryWriter.Close();
fileStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//字节数组转16进制字符串
public static string byteToStr(byte[] bytes)
{
string str = "";
foreach (byte b in bytes)
{
str += b.ToString("X2");
}
return str;
}
//16进制字符串转字节数组
private static byte[] strToByte(string str)
{
str = str.Replace(" ", "");
if ((str.Length % 2) != 0)
str += " ";
byte[] Bytes = new byte[str.Length / 2];
for (int i = 0; i < Bytes.Length; i++)
Bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
return Bytes;
}
4 在3中相应地方替换。解决转换慢的性能问题。
//string binary = byteToStr(byData);
string binary =Convert.ToBase64String(byData);
//byte[] bytes = strToByte(strPdf);
byte[] bytes =Convert.FromBase64String(strPdf);