Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
actually I am converting one pdf file into byte array and again convert that byte array into UTF8 string format and encrypt using some function what i'm going to display below and at the other hand i am decrypting the same data and convert to byte array and writing the code for pdf download but my pdf is downloaded and opening perfectly but data is not displaying, please tell me possible solution as soon as possible
C#
string constr = ConfigurationManager.ConnectionStrings["DbConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
string fileid = Request.QueryString["Id"].ToString();

string query = "select Name,Extension,EncryptedData From EncryptedFile where Id='" + fileid + "'";
SqlCommand com = new SqlCommand(query, con);
com.CommandType = CommandType.Text;
SqlDataReader dr1 = com.ExecuteReader();



while (dr1.Read())
{
    //FileStream fs1 = new FileStream("C:\\Users\\20405\\Desktop\\testpdf.pdf", FileMode.Open, FileAccess.Read);
    //BinaryReader br = new BinaryReader(fs1);
    //Document document = new Document(br);
    string file = dr1["Name"].ToString();
    string odata = Decrypt(dr1["EncryptedData"].ToString());
    string Dext = dr1["Extension"].ToString();
    //byte[] data = System.Text.Encoding.UTF8.GetBytes(odata);
    //byte[] data = Convert.FromBase64String(odata);
    byte[] data = Encoding.UTF8.GetBytes(odata);

    Response.Clear();
    MemoryStream ms = new MemoryStream(data);
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=" + file.ToString());
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Buffer = true;
    ms.WriteTo(Response.OutputStream);
    Response.Flush();
    Response.End();
Posted
Updated 6-Aug-15 0:36am
v2
Comments
ZurdoDev 6-Aug-15 7:19am    
Your pdf is empty? Sounds like you aren't opening the same pdf you think you are.
Member 11890253 7-Aug-15 0:14am    
No, the original pdf is 83 kb and the downloaded pdf is 150 kb. I already checked encryption and the decryption data is also same and the the byte array at both end also same . please give me any solution.
I want to convert the whole file into byte array and convert into string and encrypt it and store in database and decrypt it in other end display the same file and it is working perfectly for text file but pdf is not working and excel and document is corrupted
Joan Magnet 6-Aug-15 8:31am    
how big could [EncryptedData] be?
Member 11890253 7-Aug-15 0:34am    
does Matter?

1 solution

USE 3RD PARTY DLL "iTextSharp.text;iTextSharp.text.pdf;",

USE THE BELOW CODE

public FileStreamResult ExportDataToPdf()
{
string exportData = "HELLO WORLD";//exportData IS STRING, THE DATA OF PDF FILE WHICH YOU WANT TO SEE
var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
using (var input = new MemoryStream(bytes))
{
var output = new MemoryStream();
var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
var writer = PdfWriter.GetInstance(document, output);
try
{
writer.CloseStream = false;
document.Open();
var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
document.Close();
output.Position = 0;
}
catch (Exception ex)
{
document.Close();
document.CloseDocument();
}
return new FileStreamResult(output, "application/pdf");
}
}
 
Share this answer
 
Comments
Member 11890253 10-Aug-15 0:42am    
STILL PDF IS OPENING BUT NOT DISPLAYING DATA AND IT IS WORKING PERFECTLY FOR TEXT FILE AND WHILE OPENING EXCEL SHEET AND WORD DOCUMENT SHOWING CORRUPTED FILE.ALSO I CAN'T USE ABOVE CAUSE MY TL TOLD ME TO USE ONLY OWN CODE.

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