当前位置:  开发笔记 > 编程语言 > 正文

加载PDF文档时出错-iTextSharp PDF

如何解决《加载PDF文档时出错-iTextSharpPDF》经验,为你挑选了1个好方法。

我在ASP.NET C#中使用iTextSharp生成PDF文件。

我尝试为几个元素生成一个标题+表。在每个元素之后,我想开始一个新页面。我这样做,doc.NewPage()但是当我生成PDF文件时,打开文件时出现以下错误:

加载PDF文档时出错

尝试在最新版本的Google Chrome浏览器中打开PDF文件时收到此错误。当我尝试在Adobe Reader中打开文件时,出现以下错误消息:

打开此文档时出错。该文件已损坏,无法修复。

PDF档案的大小也只有1kb ...

这是我如何生成文件的代码:

//Create a byte array that will eventually hold our final PDF
//must be outside of the foreach loop (and everything else), because we store every single generated table in here for the final pdf!!
Byte[] bytes;

List myTables = getTables();
TableObject currentTable = new TableObject();

//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (MemoryStream ms = new MemoryStream())
{
    //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
    using (Document doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f))
    {
        //Create a writer that's bound to our PDF abstraction and our stream
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);

        //Open the document for writing
        doc.Open();

        //writer.CloseStream = false;
        //loop all tableobjects inside the document & the instance of PDFWriter itself! 
        foreach (TableObject to in myTables.ToList())
        {
            //Get the data from database corresponding to the current tableobject and fill all the stuff we need!
            DataTable dt = getDTFromID(to._tableID);
            Object[] genObjects = new Object[5];
            genObjects = gen.generateTable(dt, currentTable._tableName, currentTable._tableID.ToString(), currentTable, true);

            StringBuilder sb = (StringBuilder)genObjects[1];
            String tableName = sb.ToString();
            Table myGenTable = (Table)genObjects[0];
            //String table contains a valid html string, which works in the generate function for a single page document
            String table = genObjects[2].ToString();

            using (StringReader srHtml = new StringReader(table))
            {
                //Parse the HTML
                iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
            }

            //this will probably render a whole new page at the end of the file!! need to be fixed later!!!
            //doc.NewPage();
        }

        //After all of the PDF "stuff" above is done and closed but **before** we
        //close the MemoryStream, grab all of the active bytes from the stream
        bytes = ms.ToArray();

        doc.Close();
    } 
}

//Now we just need to do something with those bytes.
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=ShiftReport_complete.pdf");
Response.BinaryWrite(bytes);

如何解决此问题,以便在打开PDF文件时摆脱错误消息?



1> mkl..:

您的代码完成了这样的PDF创建:

    bytes = ms.ToArray();

    doc.Close();

这是错误的命令!在doc.Close()未决的PDF对象和PDF预告片中被写入。当您抓住byte[]之前的内容时,结果PDF缺少这些部分。

只需反过来做即可:

    doc.Close();
    bytes = ms.ToArray();

推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有