1

我正在使用 itext7 (7.1.16) 生成 PDF。当我尝试写入磁盘时一切正常。当我试图发回客户端(不保存在磁盘上)时,什么也没有发生。

我将这段代码与 Asp 按钮相关联,但没有任何反应(没有错误也没有目标)。我见过其他线程,这段代码没有问题。我不明白我把手放在哪里了。

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);
            }
        }

    }

谢谢你的时间。

4

1 回答 1

1

已解决,但问题不是 itext7(非常感谢 mkl,因为提供了不同的阅读键),而是在 asp 按钮中。

按钮在更新面板中,对于开发的第一个按钮,我添加了一个“回发触发器”。使用 PostBackTrigger 控件启用 UpdatePanel 内的控件以导致回发而不是执行异步回发。我没有在触发器中添加用于测试的按钮。当添加按钮下载是好的。

 <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>
于 2021-09-19T07:54:23.950 回答