Click here to Skip to main content
15,888,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is my c# code for download pdf document.

C#
Byte[] bytes;
int orderID = Convert.ToInt32(e.CommandArgument);
var templateData = ordersBL.GetTemplateDetails(orderID);
using (MemoryStream ms = new MemoryStream())
{
using (Document document = new Document(PageSize.A4, 10, 10, 10, 10))
{
PdfWriter writer = PdfWriter.GetInstance(document, ms);
foreach (var temp in templateData.ToList())
{
string message = temp.Message;
string tempimage = Convert.ToBase64String(temp.logo);
string base64 = tempimage;
byte[] imageBytes = Convert.FromBase64String(base64);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageBytes);
if (image.Height > image.Width)
{
//Maximum height is 800 pixels.
float percentage = 0.0f;
percentage = 700 / image.Height;
image.ScalePercent(percentage * 100);
}
else
{
//Maximum width is 600 pixels.
float percentage = 0.0f;
percentage = 140 / image.Width;
image.ScalePercent(percentage * 100);
}
//only happens on the first run!
if (!document.IsOpen())
{
//Open the document for writing
document.Open();
}
document.Add(image);
using (var htmlWorker = new HTMLWorker(document))
{
//HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses)
using (var sr = new StringReader(message))
{
//Parse the HTML
htmlWorker.Parse(sr);
}
}
Paragraph paragraph = new Paragraph();
paragraph.SpacingBefore = 10;
paragraph.SpacingAfter = 10;
paragraph.Alignment = Element.ALIGN_LEFT;
// paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
// paragraph.Add(text);
document.Add(paragraph);
document.NewPage();
}
bytes = ms.ToArray();
document.Close();
}
}
Response.ContentType = "application/pdf";
string pdfName = "User";
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
Response.Flush();
}



C#
Pdf is download but showing  failed to load Pdf document.
 
Please tell me the where I mistake.
 
Thanks. 


What I have tried:

Failed to load pdf document in C#
Posted
Updated 21-Apr-16 21:27pm
v2
Comments
lukeer 22-Apr-16 3:18am    
You speak of "downloading" a pdf document. But your code seems to create one. Or do you try to combine downloading a pdf and then altering it?

I tried to understand what the code does. It seems there are at least two parts put together that are separated by at least a few lines in the original code. The opening and closing curly braces don't match.
What is Response? How does it relate to everything before?

Please Improve your question to clear things up.

1 solution

Do you mean the pdf is downloaded (returned to the browser) and the application associated with '.pdf' on your machine gives a message 'failed to load Pdf focument' ? - its a little unclear

I would :-

1) make sure you're generating a valid pdf - write it to disk for example and see if you can open it in anything that views pdf

2) I would have put the 'response closing' in this order - Im not sure if you need End() and Close() btw

Response.Flush();
Response.Close()
Response.End()

3) I would also add

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();

Before this line
Quote:
Response.ContentType = "application/pdf";


4) in all the examples Ive seen, Ive never seen this used
Quote:
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
 
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