I'm using itext7 (7.1.16) to produce PDF. When i try to write to disk all is ok. When i'm trying to send back client (without saving on disk) nothing happens.
I associated this piece of code to Asp button but nothing happens (no errors and no goal). I've seen other thread where this piece of code has no problem. I can't understand where i put my hands.
protected void mtdCreatePDF(string TypeOutput, object sender, EventArgs e)
{
byte[] content = null;
string suffix = @"Pdf_Produced\Print.pdf";
string nameTGT = HttpContext.Current.Server.MapPath("~") + suffix;
var stream = new MemoryStream();
var writer = new PdfWriter(stream);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
document.Add(new Paragraph("Hello world!"));
document.Close();
if (TypeOutput == "RESPONSE")
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=print.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
//writer.SetCloseStream(false);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
else
{
content = stream.ToArray();
using (FileStream fs = File.Create(nameTGT))
{
fs.Write(content, 0, (int)content.Length);
}
}
}
Thanks for time.
Solved, but issue is not itext7 (thanks so much to mkl because give a different reading key) but in the asp button.
Buttons are in update Panel and for first button developed i added a "postbacktriggers". Use the PostBackTrigger control to enable controls inside an UpdatePanel to cause a postback instead of performing an asynchronous postback. I've not added the button used for test in the Triggers. When added button download is ok.