iText 7 从 Asp.Net WebApi 返回 Pdf

Posted

技术标签:

【中文标题】iText 7 从 Asp.Net WebApi 返回 Pdf【英文标题】:iText 7 return Pdf from Asp.Net WebApi 【发布时间】:2019-01-10 11:36:02 【问题描述】:

所以我整天都在搜索这个,我只是不知道如何让它工作。基本上我想在我的 ASP.Net WebApi 中使用 iText 7 创建一个 PDF 服务器端。非常简单明了的 PDF 创建:

[HttpGet]
public HttpResponseMessage CreateLieferschein()

    MemoryStream stream = new MemoryStream();

    PdfWriter writer = new PdfWriter(stream);
    var pdf = new PdfDocument(writer);
    var document = new Document(pdf);
    document.Add(new Paragraph("Hello World!"));

 

从这里开始,我不知道如何从控制器返回文件。我真的很感激任何帮助,因为我刚刚迷路了。

【问题讨论】:

【参考方案1】:

试试这个:

 [HttpGet]
public HttpResponseMessage CreateLieferschein()


    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
    byte[] buffer;
  HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest);
     using(MemoryStream stream = new MemoryStream())
     
     using(PdfWriter wri = PdfWriter.GetInstance(doc, mem)) 
     
           PdfWriter writer = new PdfWriter(stream);
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf);
        document.Add(new Paragraph("Hello World!"));
     
     buffer = stream.ToArray();
       var contentLength = buffer.Length;

      var statuscode = HttpStatusCode.OK;
        response = Request.CreateResponse(statuscode);
        response.Content = new StreamContent(new MemoryStream(buffer));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Content.Headers.ContentLength = contentLength;
        ContentDispositionHeaderValue contentDisposition = null;
        if (ContentDispositionHeaderValue.TryParse("inline; filename=" + document.Name + ".pdf", out contentDisposition)) 
            response.Content.Headers.ContentDisposition = contentDisposition;
        
     else 
        var statuscode = HttpStatusCode.NotFound;
        var message = String.Format("Unable to find file. file \"0\" may not exist.", docid);
        var responseData = responseDataFactory.CreateWithOnlyMetadata(statuscode, message);
        response = Request.CreateResponse((HttpStatusCode)responseData.meta.code, responseData);
    

    return response;

     

【讨论】:

【参考方案2】:

我还没有尝试过,只是徒手做的,所以请耐心等待,但我认为您可以了解这里发生了什么。

public HttpResponseMessage CreateLieferschein() 

  // Create the itext pdf
  MemoryStream stream = new MemoryStream();            
  PdfWriter writer    = new PdfWriter(stream);
  var pdf             = new PdfDocument(writer);
  var document        = new Document(pdf);
  document.Add(new Paragraph("Hello World!"));
  document.Close();  // don't forget to close or the doc will be corrupt! ;)

  // Load the mem stream into a StreamContent
  HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
  
    Content = new StreamContent(stream)
  ;

  // Prep the response with headers, filenames, etc.
  httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
  
    FileName = "WebApi2GeneratedFile.pdf"
  ;

  httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

  ResponseMessageResult responseMessageResult = ResponseMessage(httpResponseMessage);

  // Cross your fingers...
  return responseMessageResult;


【讨论】:

完美! @Anokrize /highfive :D

以上是关于iText 7 从 Asp.Net WebApi 返回 Pdf的主要内容,如果未能解决你的问题,请参考以下文章

如何从 ASP.NET Core WebAPI 获取 int 值?

从 ASP.NET MVC Web API 中的多个表单数据键接收文件

将Asp.Net WebAPI从AngularJS App的Asp.Net MVC站点移动到一个单独的站点

从 Asp.net WEBAPI 显式返回 JSON 字符串?

ASP.NET WebApi 从入门到"放弃"系列---WebApi 请求路由

如何将文件和 json 对象从邮递员传递到 asp.net 核心 webapi