本文介绍了iTextSharp的错误:"该文件没有页"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置为引用的DLL 3:


  • itextsharp.dll:核心库

  • itextsharp.xtra.dll:额外的功能(!PDF 2)

  • itextsharp.pdfa.dll:PDF / A-相关功能
    该项目位于
    你可以在这里找到最新的版本:

执行此code时,我得到一个错误:

在pdfDoc.Close()的文档没有页。

 进口iTextSharp.text
进口iTextSharp.text.html.simpleparser
进口iTextSharp.text.pdf
    gv.DataBind()
    gv.AllowPaging =假
    Response.ContentType =应用程序/ PDF
    Response.AddHeader(内容处置,附件;文件名= Export.pdf)
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    昏暗SW作为新的StringWriter()
    昏暗的硬件作为新的HtmlTextWriter(SW)
    昏暗FRM作为新的HtmlForm()
    gv.Parent.Controls.Add(FRM)
    frm.Attributes(RUNAT)=服务器
    frm.Controls.Add(GV)
    frm.RenderControl(HW)
    昏暗SR作为新StringReader(sw.ToString())
    昏暗pdfDoc作为新文档(PageSize.A4,10.0F,10.0F,10.0F,0.0F)
    昏暗的HTMLParser作为新HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc,Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(SR)
    pdfDoc.Close()
    的Response.Write(pdfDoc)
    响应。[完]()


解决方案

HTMLWorker 是pcated德$ P $和不应该再被使用。 HTML到PDF的功能已被取代的技术,称为XML工人。我看到你包括itextsharp.xtra和itextsharp.pdfa这是2 DLL文件就不需要。我没有看到你包括xmlworker DLL。

至于例外:当你得到消息的文档没有页,你想没有任何内容来创建一个文档(这没有意义)。这怎么可能?那么,这一切都取决于的内容SR 。内容为空或包含HTML无法被 HTMLWorker PTED间$ P $。

附加备注:旁边itextpdf.xtra,你写(PDF 2!)。而XTRA包中包含未在PDF 1.7存在functonality,它不是PDF 2包。该PDF 2规范是要在最早是由2015年年底(更现实的估计是2016年)的预期。在iText的,我们已经实现PDF基础上,规范的草案2.0版的功能,但功能是(1)不局限于是什么在XTRA包,和(2)而不是规范的一部分,已公开发布通过ISO呢。

I set as references three dlls:

I get an error when executing this code:

On pdfDoc.Close(), "The document has no pages."

Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf


    gv.DataBind()
    gv.AllowPaging = "False"
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Export.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    Dim frm As New HtmlForm()
    gv.Parent.Controls.Add(frm)
    frm.Attributes("runat") = "server"
    frm.Controls.Add(gv)
    frm.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.[End]()
解决方案

The class HTMLWorker is deprecated and should no longer be used. HTML to PDF functionality has been replaced by technology called XML Worker. I see that you include itextsharp.xtra and itextsharp.pdfa which are 2 DLLs you don't need. I don't see you including the xmlworker DLL.

As for the exception: when you get the message "The document has no pages", you are trying to create a document without any content (and that doesn't make sense). How is this possible? Well, it all depends on the content of sr. That content is either empty or it contains HTML that can't be interpreted by HTMLWorker.

Extra remark: next to itextpdf.xtra, you wrote (PDF 2!). While the xtra package contains functonality that didn't exist in PDF 1.7, it's not the PDF 2 package. The PDF 2 specification is to be expected at the earliest by the end of 2015 (a more realistic estimation is 2016). At iText, we've already implemented PDF 2.0 functionality based on the draft of the spec, but that functionality is (1) not limited to what is in the xtra package, and (2) not part of a specification that has been publicly released by ISO yet.

这篇关于iTextSharp的错误:"该文件没有页"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:00