C# 将 base64Binary 转换为 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1626982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:31:18  来源:igfitidea点击:

convert base64Binary to pdf

c#pdfbinary

提问by Novice Developer

I have raw data of base64Binary.

我有 base64Binary 的原始数据。

string base64BinaryStr = "J9JbWFnZ......"

How can I make pdf file? I know it need some conversion. Please help me.

如何制作pdf文件?我知道它需要一些转换。请帮我。

回答by Mike Clark

All you need to do is run it through any Base64 decoder which will take your data as a string and pass back an array of bytes. Then, simply write that file out with pdf in the file name. Or, if you are streaming this back to a browser, simple write the bytes to the output stream, marking the appropriate mime-type in the headers.

您需要做的就是通过任何 Base64 解码器运行它,它将您的数据作为字符串并传回一个字节数组。然后,只需在文件名中使用 pdf 写出该文件。或者,如果您将其流式传输回浏览器,只需将字节写入输出流,并在标头中标记适当的 MIME 类型。

Most languages either have built in methods for converted to/from Base64. Or a simple Google with your specific language will return numerous implementations you can use. The process of going back and forth to Base64 is pretty straightforward and can be implemented by even novice developers.

大多数语言都内置了用于转换为 Base64 或从 Base64 转换的方法。或者使用您的特定语言的简单 Google 将返回您可以使用的许多实现。来回转换到 Base64 的过程非常简单,即使是新手开发人员也可以实现。

回答by MusiGenesis

Step 1 is converting from your base64 string to a byte array:

第 1 步是将 base64 字符串转换为字节数组:

byte[] bytes = Convert.FromBase64String(base64BinaryStr);

Step 2 is saving the byte array to disk:

第 2 步是将字节数组保存到磁盘:

System.IO.FileStream stream = 
    new FileStream(@"C:\file.pdf", FileMode.CreateNew);
System.IO.BinaryWriter writer = 
    new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();

回答by Ralf de Kleine

using (System.IO.FileStream stream = System.IO.File.Create("c:\temp\file.pdf"))
{
    System.Byte[] byteArray = System.Convert.FromBase64String(base64BinaryStr);
    stream.Write(byteArray, 0, byteArray.Length);
}

回答by BinaryTank

base64BinaryStr - from webservice SOAP message

base64BinaryStr - 来自 webservice SOAP 消息

byte[] bytes = Convert.FromBase64String(base64BinaryStr); 

回答by Synap Sen

This code does not write any file on the hard drive.

此代码不会在硬盘驱动器上写入任何文件。

Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", base64Result.Length.ToString());
Response.AddHeader("Content-Disposition", "inline;");
Response.AddHeader("Cache-Control", "private, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "public");
Response.BinaryWrite(Convert.FromBase64String(base64Result));

Note: the variable base64Resultcontains the Base64-String: "JVBERi0xLjMgCiXi48/TIAoxI..."

注意:该变量base64Result包含 Base64 字符串:“JVBERi0xLjMgCiXi48/TIAoxI...”

回答by dush88c

First convert the Bas64 string to byte[] and write it into a file.

首先将 Bas64 字符串转换为 byte[] 并写入文件。

byte[] bytes = Convert.FromBase64String(base64BinaryStr); 
File.WriteAllBytes(@"FolderPath\pdfFileName.pdf", bytes );