Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
804 views
in Technique[技术] by (71.8m points)

c# - iTextSharp is producing a corrupt PDF with Response

i tryed both, but still not working

iTextSharp + FileStream = Corrupt PDF file

iTextSharp is producing a corrupt 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();
        }

What im doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...