【问题标题】:After Generating pdf using Itext Sharp on a button click, Another button click on the same page,won't trigger在单击按钮上使用 Itext Sharp 生成 pdf 后,在同一页面上单击另一个按钮,不会触发
【发布时间】:2012-01-01 16:31:34
【问题描述】:

我正在使用 Itext Sharp 通过单击按钮为我网站上的现有订单创建 pdf。它已作为 Web 部件添加到 Sharepoint 网站。

在同一页面上,还有另一个包含许多服务器控件的 Web 部件。但是,当我单击任何按钮时,不会触发按钮单击事件。只有当我刷新页面时,才会触发事件。

我无法同时创建一个 pdf,然后在不刷新页面的情况下立即单击另一个按钮。我不明白我做错了什么。

下面是我的代码。

  protected void PdfGenerate_OnClick(object sender, EventArgs e)
  {
      Font Arial = FontFactory.GetFont("Arial", 12, BaseColor.BLACK);
      string imagename = "AAlogo.gif";
      string imagepath = Page.Server.MapPath("/_layouts/Images/ImagesFolder/Image1.gif" ) ;

      using (var ms = new MemoryStream())
      {
          using (var document = new Document(PageSize.A4,50,50,15,15))
          {
              PdfWriter.GetInstance(document, ms); 
              document.Open();
              Paragraph img = new Paragraph();
              Image aaImg = iTextSharp.text.Image.GetInstance(imagepath);
              img.Add(aaImg);
              img.IndentationRight = 5f;
              //code to add all the elements here  
              PdfPTable table = new PdfPTable(3);
              PdfPCell col1 = new PdfPCell(new Phrase("oRDER Number"));
              table.AddCell(col1);
              //  code for the table...
                  document.Add(table);
              }
             document.Close();
          }
          Response.Clear();
          Response.ContentType = "application/octet-stream";
          Response.AddHeader("content-disposition", "attachment;filename= Order Number" + .pdf");
          Response.Buffer = true; 
          Response.Clear();
          var bytes = ms.ToArray();
          Response.OutputStream.Write(bytes, 0, bytes.Length);
          Response.OutputStream.Flush();
      } 

  }

【问题讨论】:

    标签: asp.net sharepoint itextsharp itext


    【解决方案1】:

    当您清除和刷新响应时,您正在中断/中止 asp.net 的页面生命周期。最好将该下载部分放在另一个文件中(单独文件的 Page_Load)并从当前页面调用它。更多信息:++

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-23
      • 1970-01-01
      • 2011-04-03
      • 2020-02-11
      相关资源
      最近更新 更多