Convert base64Binary to pdf

I have base64Binary source data.

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

How can I make a pdf file? I know that this requires some conversion. Please help me.

+11
c # binary pdf
source share
6 answers

Step 1 converts your base64 string to an array of bytes:

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

Step 2 saves an array of bytes to disk:

 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(); 
+28
source share
 using (FileStream stream = System.IO.File.Create("c:\\file.pdf")) { byte[] byteArray = Convert.FromBase64String(base64BinaryStr); stream.Write(byteArray, 0, byteArray.Length); } 
+14
source share

base64BinaryStr - from SOAP web service message

 byte[] bytes = Convert.FromBase64String(base64BinaryStr); 
+3
source share

All you have to do is run it through any Base64 decoder that will receive your data as a string and pass an array of bytes. Then just write this file with pdf in the file name. Or, if you pass this back to the browser, just write the bytes to the output stream, noting the corresponding mime type in the headers.

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

+2
source share

This code does not write files to 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 base64Result variable contains base64Result Base64: "JVBERi0xLjMgCiXi48 / TIAoxI ..."

+1
source share

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

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

All Articles