iTextSharp正在使用Response生成损坏的PDF [英] iTextSharp is producing a corrupt PDF with Response

查看:134
本文介绍了iTextSharp正在使用Response生成损坏的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过两次,但仍然无法正常工作

i tryed both, but still not working

iTextSharp + FileStream =损坏的PDF文件

iTextSharp正在生成一个损坏的PDF

using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            //abre o documento para poder editar
            document.Open();

            //Adiciona os campos de assinatura
            document.Add(Assinatura());

            //fecha o documento ao finalizar a edição
            document.Close();

            //Prepara o download
            byte[] bytes = memoryStream.ToArray();
            memoryStream.Close();
            Response.Clear();
            Response.ContentType = "image/pdf";
            //Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment;
            filename=ControleDePonto.pdf");
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes);
            Response.End();
            Response.Close();
        }

我做错了什么?

推荐答案

使用PdfWriter将PDF写入MemoryStream。

Use PdfWriter to write the PDF to the MemoryStream.

PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();

//Adiciona os campos de assinatura
document.Add(Assinatura());

//fecha o documento ao finalizar a edição
document.Close();

//Prepara o download
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=ControleDePonto.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();

这篇关于iTextSharp正在使用Response生成损坏的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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