Hi,
Hi Sai prasad,
Thanks for your reply.
Hi Sai prasad,
Thanks for sharing the details.
Am trying to get option to Open/Save/Cancel generated pdf instead of printing it.
Could you please help me out for getting option to open/save/cancel it. Below is the code that am using.
String pdfHtmlTemplate =
Document doc = New Document(new MemoryStream(Encoding.UTF8.GetBytes(pdfHtmlTemplate)), objLoadoptions);
doc.save();
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=FileName.pdf”, “FileName”));
Response.ContentType = “application/pdf”;
Response.Flush();
Response.End();
Hi Sai prasad,
Thanks for sharing the details.
From above statement, my understanding is that you need to render the output PDF in browser and want to show the dialog with option to Save/Open or Cancel the PDF display. Please note that in order to render the output in Web-browser, you need to save the output in Stream instance and then pass the stream to Response object. Or you can use a save overload of Document object which takes Response object as an argument.
Thanks for your response.
What you understand is correct. I want to get a dialog box with option to save/open or cancel. I have tried to implement the method that you recommended. Am getting exceptional saying that "cannot access a closed stream "
Could you please suggest the way to get download PDF.
Below is the code that am using.
String pdfHtmlTemplate =
Byte[] inputBytes = Encoding.UTF8.GetBytes(pdfHtmlTemplate);
MemoryStream stream = new MemoryStream (inputBytes );
Document doc = new Document(stream , new htmlloadoptions);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF-8”;
Response.AddHeader(“content-length”,stream.length.tostring());
Response.AddHeader(“content-disposition”, String.Format(“attachment;filename=FileName.pdf”, “FileName”));
Response.ContentType = “application/pdf”;
Response.BinaryWrite(stream.toarray());
Response.Flush();
Response.End()
Could you please have a look into it and help me out with working code ASAP.
Hi Sai prasad,
Thanks for sharing the details.
I think the reason you encountered above issue is because you tried to pass the same MemoryStream object to Response instance whereas in actual you have to save the Document output to a separate MemoryStream instance and then use that new object in Response instance. I have tested the scenario with a slight modified code snippet and did not notice any issue.
For your reference, I have attached the image file showing intended dialog box and I have also attached a sample project which I have used for testing purposes. Should you have any related query, please feel free to contact.
[C#]
String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);
System.IO.MemoryStream stream = new MemoryStream(inputBytes);
Document doc = new Document(stream, new HtmlLoadOptions());
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = "UTF-8";
Response.AddHeader("content-length", ms.Length.ToString());
Response.AddHeader("content-disposition", String.Format("attachment;filename=FileName.pdf", "FileName"));
Response.ContentType = "application/pdf";
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
Thanks for your fast response.
I have changed the code and tried. New code is executed without exception but Dialog box is not coming for me. I have tried in both IE and chrome
Actually on click of print I have written a javascript function and from that function am calling below method using ajax call .
Public string PDFprint(int noteId, Notemodel model)
{
String pdfHtmlTemplate = “< table >< tr >< td >…</ td ></ tr ></ table >”;
< o:p > </ o:p >
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);
[System.IO](http://system.io/).MemoryStream stream = new MemoryStream(inputBytes);<o:p></o:p>
Document doc = new Document(stream, new HtmlLoadOptions());
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = “UTF - 8”;
Response.AddHeader(“content - length”, ms.Length.ToString());
Response.AddHeader(“content - disposition”, String.Format(“attachment; filename = FileName.pdf”, “FileName”));
Response.ContentType = “application / pdf”;
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
}
Please you please help me out .
Thanks
Hi Sai prasad,
Thanks for sharing the details.
In my earlier attempt, I tested the scenario with FireFox and in recent attempt, I have used Internet Explorer 11.0.9600 I am still unable to notice any issue. For your reference, I have also attached the image showing desired behavior.
Please note that Internet Explorer is currently set as my default browser for default.aspx (which contains following code snippet).
[C#]
String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);
System.IO.MemoryStream stream = new MemoryStream(inputBytes);
Document doc = new Document(stream, new HtmlLoadOptions());
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.Charset = "UTF-8";
Response.AddHeader("Accept-Header", ms.Length.ToString());
Response.AddHeader("Content-Length", ms.Length.ToString());
Response.AddHeader("Expires", "0″");
Response.AddHeader("Pragma", "cache");
Response.AddHeader("Cache-Control", "private");
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", "article" + "docId" + ".pdf"));
Response.ContentType = "application/pdf";
Response.AddHeader("Accept-Ranges", "bytes");
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
Hi Sai prasad,
Adding more to my previous comments, in case you still face any issue, please try using following code snippet.
[C#]
String pdfHtmlTemplate = “<
table >< tr >< td >…</ td ></ tr ></
table >”;
Byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(pdfHtmlTemplate);
System.IO.MemoryStream stream = new MemoryStream(inputBytes);
Document doc = new Document(stream, new HtmlLoadOptions());
MemoryStream ms = new MemoryStream();
PdfSaveOptions saveoption = new PdfSaveOptions();
doc.Save(Response,“sample.pdf”, ContentDisposition.Attachment,saveoption
);
Save Editcancel
Hi
Hi Sai prasad,
Hi team,
Hi Sai prasad,
Hi,
Hi Sai prasad,