如何PDF byte []数组转换为使用iTextSharp的下载文件 [英] How to convert pdf Byte[] Array to downloadable file using iTextSharp

查看:184
本文介绍了如何PDF byte []数组转换为使用iTextSharp的下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜家伙我有这样的字节数组,我想转换为PDF格式,并使其可供下载。任何人有任何想法如何做到这一点?

这是我的动作控制器

 公众的ActionResult DownloadLabTestResult(字符串labTestResultID)
{
            PdfReader pdfReader =新PdfReader(Xue_Tang.pdf);            MemoryStream的流=新的MemoryStream();
            PdfStamper模子=新PdfStamper(pdfReader,流);            pdfReader.Close();
            stamper.Close();
            stream.Flush();
            stream.Close();
            字节[] = pdfByte stream.ToArray();            //所以我拿到原始的PDF在这一点上的字节数组。现在,我如何把这个
            //字节数组到一个下载的PDF?我想下面,但无济于事方法。            MemoryStream的毫秒=新的MemoryStream(pdfByte);            Response.ContentType =应用程序/ PDF
            Response.AddHeader(内容处置,附件;文件名= labtest.pdf);
            将Response.Buffer =真;
            Response.Clear();
            Response.OutputStream.Write(ms.GetBuffer(),0,ms.GetBuffer()长。);
            Response.OutputStream.Flush();
            到Response.End();            返回新FileStreamResult(Response.OutputStream,应用程序/ PDF格式); }


解决方案

我使用类似code有一些不同:

  Response.Clear();
MemoryStream的毫秒=新的MemoryStream(pdfByte);
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容处置,附件;文件名= labtest.pdf);
将Response.Buffer =真;
ms.WriteTo(Response.OutputStream);
到Response.End();


  1. 呼叫Reponse.Clear()前面。

  2. 使用MemoryStream.WriteTo写Response.OutputStream。

编辑:对不起,我没看到你正在使用ASP.NET MVC,上述code是一个WebForms的aspx页面

有关ASP.NET MVC中,你能不能只是做

 返回新FileStreamResult(MS,应用程序/ PDF格式);

Hei guys I have this byte array i want to convert to pdf and make it available for download. Anybody has any idea how this is done?

here is my Action Controller

public ActionResult DownloadLabTestResult(string labTestResultID)
{
            PdfReader pdfReader = new PdfReader("Xue_Tang.pdf");

            MemoryStream stream = new MemoryStream();
            PdfStamper stamper = new PdfStamper(pdfReader, stream);

            pdfReader.Close();
            stamper.Close();
            stream.Flush();
            stream.Close();
            byte[] pdfByte = stream.ToArray();

            // So i got the byte array of the original pdf at this point. Now how do i convert this
            // byte array to a downloadable pdf? i tried the method below but to no avail.

            MemoryStream ms = new MemoryStream(pdfByte);

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
            Response.Buffer = true;
            Response.Clear();
            Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.End();

            return new FileStreamResult(Response.OutputStream, "application/pdf");

 }

解决方案

I am using similar code with a few differences:

Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();

  1. Call Reponse.Clear() earlier.
  2. Use MemoryStream.WriteTo to write to Response.OutputStream.

Edit: sorry, I didn't see that you are using ASP.NET MVC, the above code is in a WebForms aspx page.

For ASP.NET MVC, couldn't you just do

return new FileStreamResult(ms, "application/pdf");

?

这篇关于如何PDF byte []数组转换为使用iTextSharp的下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆