Click here to Skip to main content
16,018,347 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I am in a need to convert PDF to bytes[] and bytes[] to string and vice versa and i am using the following code for this.

This works perfectlty for html documents but not for pdf's.
C#
//convert pdf to bytes
FileStream fs;
fs = File.Open(fileName, FileMode.Open);
byte[] bytBytes1 = new byte[fs.Length];
fs.Read(bytBytes1, 0, bytBytes1.Length);
fs.Close();

//convert bytes to string
string strModified = Encoding.Unicode.GetString(bytBytes1);

//convert string to bytes
byte[] b =Encoding.Unicode.GetBytes(strModified);

//to open the attachment
Response.AddHeader("Content-disposition", "attachment;filename=filename.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(b);
Response.End();

can anyone help me in this issue.

Thanks in advance
Posted
Updated 1-Jun-18 5:57am
v3

Why are you doing that, just use;
Response.BinaryWrite(bytBytes1);

You do not need to convert it to a string then to a byte again.
 
Share this answer
 
Please don't push 'Answer' unless you are posting an answer. Edit your post to add detail.

Your client is asking for this ? Then they are an idiot. HTML IS a string. A PDF is NOT a string. So, it can't be done. You should not be dealing with clients unless you know enough about the technology you're using to tell your client if the thing they are imagining is actually not possible.
 
Share this answer
 
Comments
Foyzul Karim 7-Jun-10 8:03am    
I am sorry for my ignorance. But could you please elaborate why it is not possible?
Thanks.
No i have a situation like i have to send an attachment as a string and in the other end our client will get the string and convert it to attachments. so i have to convert it to string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900