2

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.

Franco
  • 101
  • 9
  • You use itext to write to a `MemoryStream`, itext has no notion of what you do with the bytes in there. Have you tried writing them to a file, too? Is that pdf a valid pdf? If it is, then your issue is not related to itext but instead done other part of your setup. – mkl Sep 18 '21 at 13:36
  • Yes in a file the PDF is correct. The only difference is where i redirect. I edited the question to insert code. – Franco Sep 18 '21 at 18:05
  • With that addition it is even more clear that the issue is not related to itext - you simply use the contents of a `MemoryStream` and transfer them in two different ways, and one of these ways results in an error. If i were you, I'd edit the question to focus on that. As it is formulated now, it sounds like you need someone with itext expertise. Actually, though, you need someone with. NET web application expertise. – mkl Sep 19 '21 at 07:18

1 Answers1

2

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>
Franco
  • 101
  • 9