【问题标题】:iText 7 return Pdf from Asp.Net WebApiiText 7 从 Asp.Net WebApi 返回 Pdf
【发布时间】: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!"));

 }

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

【问题讨论】:

    标签: c# asp.net asp.net-web-api pdf-generation itext7


    【解决方案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
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 2017-07-22
      • 2019-08-05
      相关资源
      最近更新 更多