使用iTextSharp发送到浏览器生成的PDF时出错#34;错误:无法加载PDF文档。"

时间:2017-09-27 16:01:54

标签: c# pdf asp.net-mvc-5 itext runtime

我的控制器中有一个函数,它接收一个对象并使用该对象的数据来填充PDF。我想将此PDF发送给客户端,以便下载它。但是当我提交表单并下载生成的PDF时,我收到一条错误,上面写着"错误:无法加载PDF文档。"

到目前为止,这是我的代码:

    private void crear_pdf_ZURICH(Seguro seguro)
    {
        var nombre_pdf = seguro.Nombre + ".pdf";

        string oldFile = Server.MapPath("pdf") + "\\" + nombre_pdf;
        string newFile = Server.MapPath("pdf") + "\\" + "nuevo_" + nombre_pdf;
        using (var reader = new PdfReader(oldFile))
        {
            using (var fileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write))
            {
                var document = new Document(reader.GetPageSizeWithRotation(1));
                var writer = PdfWriter.GetInstance(document, fileStream);

                document.Open();

                //VARIABLES

                for (var i = 1; i <= reader.NumberOfPages; i++)
                {
                    document.NewPage();

                    var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    var importedPage = writer.GetImportedPage(reader, i);

                    var contentByte = writer.DirectContent;
                    contentByte.AddTemplate(importedPage, 0, 0);
                    contentByte.BeginText();
                    contentByte.SetFontAndSize(baseFont, 10);

                    if (reader.NumberOfPages - (reader.NumberOfPages - i) == 1)
                    {
                        //WRITE SOME STUFF
                    }

                    else if (reader.NumberOfPages - (reader.NumberOfPages - i) == 2)
                    {
                       //WRITE SOME OTHER STUFF
                    }
                    contentByte.EndText();
                }

                document.Close();
                writer.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename="+newFile));
            }
        }
    }

这是我的代码正常工作,它创建了PDF,我可以毫无问题地打开它,但是我将它保存在服务器上,如果多个用户试图创建和下载生成的内容,可能会导致问题PDF直接来自服务器。 这是我的代码:

    private void crear_pdf_ZURICH(Seguro seguro)
    {
        var nombre_pdf = seguro.Nombre + ".pdf";

        string oldFile = Server.MapPath("pdf") + "\\" + nombre_pdf;
        string newFile = Server.MapPath("pdf") + "\\" + "nuevo_" + nombre_pdf;


        using (var reader = new PdfReader(oldFile))
        {
            using (var fileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write))
            {
                var document = new Document(reader.GetPageSizeWithRotation(1));
                var writer = PdfWriter.GetInstance(document, fileStream);

                document.Open();

                //VARIABLES

                for (var i = 1; i <= reader.NumberOfPages; i++)
                {
                    document.NewPage();

                    var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    var importedPage = writer.GetImportedPage(reader, i);

                    var contentByte = writer.DirectContent;
                    contentByte.AddTemplate(importedPage, 0, 0);
                    contentByte.BeginText();
                    contentByte.SetFontAndSize(baseFont, 10);

                    if (reader.NumberOfPages - (reader.NumberOfPages - i) == 1)
                    {
                       //WRITE SOME STUFF
                    }

                    else if (reader.NumberOfPages - (reader.NumberOfPages - i) == 2)
                    {
                        //WRITE SOME STUFF
                    }



                    contentByte.EndText();
                }

                document.Close();
                writer.Close();
            }
        }
    }

如果有人可以帮助/解释我如何解决这个问题,请提前致谢。

0 个答案:

没有答案