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.
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="panelCMD" runat="server">
<asp:LinkButton ID="btnFirstPDF_ResponseOutput" runat="server" CssClass="btn btn-small btn-primary fullwidth" OnClick="btnFirstPDF_ResponseOutput_Click"><i class="icon icon-ok"></i>PDF on Disk </asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn-small btn-primary fullwidth" OnClick="LinkButton1_Click"><i class="icon icon-ok"></i>PDF Download</asp:LinkButton>
</asp:Panel>
</ContentTemplate>
<Triggers >
<asp:PostBackTrigger ControlID="btnFirstPDF_ResponseOutput"/>
<asp:PostBackTrigger ControlID="LinkButton1"/>
</Triggers>
</asp:UpdatePanel>