iTextSharp生成pdf打开文件有错误。文件显示已损坏,无法修复

时间:2017-08-05 04:29:34

标签: c# asp.net pdf itext

我生成pdf并打开pdf文件时遇到的问题是显示文件已损坏。在我使用notepad ++打开文件后,我发现内部文件最后获得了html代码。但是,如果我尝试删除HTML代码,则该文件可以打开。我能知道为什么会出现这个错误

这是我用来生成pdf的代码

        try
        {
            Session["ExportDetail"] = "viewnormal";
            string urlEncFolderName = Server.UrlEncode("uploads");
            string errMsg = "";

            NameTextSharp.Document doc = new NameTextSharp.Document(iTextSharp.text.PageSize.A4, 30, 30, 50, 50);
            MemoryStream outstream = new MemoryStream();
            string filename = null;
            PdfWriter wri = PdfWriter.GetInstance(doc, outstream);
            doc.Open();
            doc = BindingData(doc);

            //start - attach file if exist
            List<string> attachment = GetAttachment(Session["QUESTIONSETID"].ToString());

            if (attachment.Count() > 0)
            {
                for (int i = 0; i < attachment.Count(); i++)
                {
                    string strFilePath = "";
                    string fileExt = "";
                    strFilePath = Request.PhysicalApplicationPath + urlEncFolderName + "/" + attachment[i];
                    fileExt = Path.GetExtension(strFilePath);

                    // check if file exist
                    if (File.Exists(strFilePath))
                    {
                        PdfFileSpecification pdfAttch = PdfFileSpecification.FileEmbedded(wri, strFilePath, "Attachment " + (i + 1) + fileExt, null);
                        wri.AddFileAttachment(strFilePath, pdfAttch);
                    }
                    else
                    {
                        errMsg = "Attachment file(s) not found.";
                    }
                }
            }

            if (errMsg == "")
            {
                filename = "Sub" + Session["SUBMISSIONID"].ToString() + "_ViewAnswer";
                doc.Close();
                Response.BinaryWrite(outstream.ToArray());
                Response.Buffer = true;
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename=" + filename + ".pdf"));                  
            }
            else
            {
                Messagebox("There was an error converting to PDF.");
            }
        }
        catch (Exception ex)
        {
            mag.WriteToLogFile("Error at export to PDF : " + ex.ToString());
        }

此代码是在我打开pdf文件时使用notepad ++的最终代码

                    trailer
                    <</Size 59/Root 57 0 R/Info 58 0 R/ID [<eb8dd247b7cfa23df5a2399a7d53dd86><eb8dd247b7cfa23df5a2399a7d53dd86>]>>
                    %iText-5.5.9
                    startxref
                    3043799
                    %%EOF

                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head id="Head1"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /><title>
                        Question
                    </title><link href="../../Stylesheet.css" rel="stylesheet" type="text/css" />

0 个答案:

没有答案