pdf转byte[]再转pdf文件进行保存
/// <summary> /// pdf转byte[]再转pdf文件进行保存 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { string cesi1 = AppDomain.CurrentDomain.BaseDirectory + "FilePath\\" + 11 + ".pdf";//测试pdf保存文件 byte[] bytes = File.ReadAllBytes("C:\\Users\\Administrator\\Desktop\\测试.pdf");//测试pdf文件 string strResult = byteToHexStr(bytes);//将pdf byte[]数组转16进制字符串 this.richTextBox1.Text = strResult; byte[] byteArray = StrToHexByte(strResult);//将16进制的字符串 转为byte[] System.IO.File.WriteAllBytes(@cesi1, byteArray);//根据byte[]数组输出保存pdf文件 } /// <summary> /// 字节数组转16进制字符串 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static string byteToHexStr(byte[] bytes) { string returnStr = ""; if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { returnStr += bytes[i].ToString("X2"); } } return returnStr; } /// <summary> /// 将16进制的字符串转为byte[] /// </summary> /// <param name="hexString"></param> /// <returns></returns> public static byte[] StrToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; }
pdf base64数据流转pdf文件 /// <summary> ///Base64数据流转pdf /// </summary> /// <param name="strIn"></param> public void Base64Pdf(string strIn) { try { var filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString(); filePath = filePath + "PdfRecord"; //判断是否有对应文件夹,没有则创建 if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } filePathSet = filePath; var filePathInfo = filePath + "\\" + "cesi" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf"; base64StringToPdf(strIn, filePathInfo); } catch (Exception ex) { LogHelper.WriteLog(GetType(), "Base64Pdf异常错误为:" + ex.Message); } } /// <summary> /// base64 转PDF /// </summary> /// <param name="base64Content"></param> /// <param name="filePath"></param> public void base64StringToPdf(String base64Content, String filePath) { try { LogHelper.WriteLog(GetType(), "进入base64StringToPdf方法,入参为:" + base64Content + "路径为:" + filePath); base64Content = base64Content.Replace("data:application/pdf;filename=generated.pdf;base64,", string.Empty); string base64ContentData = base64Content.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (base64ContentData.Length % 4 > 0) { base64ContentData = base64ContentData.PadRight(base64ContentData.Length + 4 - base64ContentData.Length % 4, '='); } byte[] bytes = Convert.FromBase64String(base64ContentData); string location = filePath; System.IO.FileStream stream = new FileStream(location, FileMode.CreateNew); System.IO.BinaryWriter writer = new BinaryWriter(stream); writer.Write(bytes, 0, bytes.Length); writer.Close(); } catch (Exception ex) { LogHelper.WriteLog(GetType(), "base64StringToPdf异常错误为:" + ex.Message); } }
标签:string,filePath,base64,bytes,Length,pdf,byte From: https://www.cnblogs.com/lydj/p/17005823.html