14
嘿家伙我有这个字节数组我想要转换为PDF并使其可供下载。任何人都知道这是如何完成的?如何转换PDF字节[]数组使用iTextSharp可下载的文件
这里是我的动作控制器
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");
}
如果你有PDF文件在磁盘上,你可以直接用FileStream读取它。 – 2010-09-02 04:45:15
即使你需要PdfStamper它看起来像你有很多的流,你不应该需要“ms”蒸汽,“流”流应该是足够的,但你不应该在使用它之前关闭它,你可以尝试“ ms.Seek(0,SeekOrigin.Begin);“然后将其添加到OutputStream中。 – 2010-09-02 04:53:18
哦,我可以随时从本地磁盘读取当然,但我只是想模拟这里的东西,即时只提供字节数组,我的工作是将其转换为可下载的文件,不知道如何做到这一点嘿嘿= p – Ari 2010-09-02 05:48:47