如何使用asp.net mvc.?

时间:2015-12-30 16:40:32

标签: asp.net-web-api2

如何使用asp.net mvc通过webapi控制器使用iTextsharp创建和下载pdf文件。

System.Web.HttpContext.Current.Response.ContentType = "application/pdf";

System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=RecommendationPage.pdf");
using (var stringWriter = new System.IO.StringWriter(fileBuilder))
{
  using (var htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter))
  {
    htmlTextWriter.Write(fileBuilder.ToString());
    using (var document = new iTextSharp.text.Document())
    {
      var pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, System.Web.HttpContext.Current.Response.OutputStream);
      document.Open();
      using (var stringReader = new System.IO.StringReader(fileBuilder.ToString()))
      {
        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(
          pdfWriter, document, stringReader
          );
      }
    }
  }
}
System.Web.HttpContext.Current.Response.End();

0 个答案:

没有答案