Java 文档没有带有 itext 的页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20898322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:06:13  来源:igfitidea点击:

document has no pages with itext

javaitext

提问by Santino 'Sonny' Corleone

<%
OutputStream output=response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=details.pdf");
try{
    Document document = new Document();
PdfWriter writer=PdfWriter.getInstance(document, output);
document.open();
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/a", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
List arrlist = new ArrayList();
ResultSet rs=st.executeQuery("Select * from user_start1");
 while(rs.next()){
 arrlist.add(rs.getString("data"));
 }  
for(int i=0;i<12;i++){
  String str =(String) arrlist.get(i);
  System.out.println(str); 
  worker.parseXHtml(writer, document, new StringReader("helloworld"));
}
document.close();
writer.flush();
writer.close();
output.close();
}catch(IOException e){e.printStackTrace();} 
%>

throws an error

抛出错误

SEVERE: Servlet.service() for servlet [jsp] in context with path [/chieflegis] threw exception [ExceptionConverter: java.io.IOException: The document has no pages.] with root cause
java.io.IOException: The document has no pages.
    at com.itextpdf.text.pdf.PdfPages.writePageTree(PdfPages.java:113)
    at com.itextpdf.text.pdf.PdfWriter.close(PdfWriter.java:1217)
    at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:807)
    at com.itextpdf.text.Document.close(Document.java:416)
    at org.apache.jsp.print_jsp._jspService(print_jsp.java:112)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

I have used the same xmlworker previously but never had any errors.even hellowworld isnt displayed.please help

我以前使用过相同的 xmlworker,但从未出现任何错误。甚至没有显示地狱世界。请帮助

采纳答案by rhens

XMLWorkerHelper.parseXHtml()expects (X)HTML or (X)HTML snippets. Try this:

XMLWorkerHelper.parseXHtml()需要 (X)HTML 或 (X)HTML 片段。尝试这个:

worker.parseXHtml(writer, document, new StringReader("<p>helloworld</p>"));

回答by Leo Zhao

Try to new a page just like document.newPage()before you write something to document, hope that helps.

尝试像document.newPage()在编写文档之前一样新建一个页面,希望对您有所帮助。

回答by John K

Other answers are good. This is an alternative.

其他答案都很好。这是一个替代方案。

In general, to prevent this error which often occurs when the document contains no meaningful data for content, even despite document.open()and document.newPage()having been called, and even after stamping other pages into that document, you can add an empty chunk when the document is opened to ensure the library never considers it empty. e.g.

通常,为了防止在文档不包含任何有意义的内容数据时经常发生的错误,即使document.open()document.newPage()已被调用,甚至在将其他页面标记到该文档后,您可以在打开文档时添加一个空块以确保图书馆从不认为它是空的。例如

document.open(); 
document.add(new Chunk("")); // << this will do the trick. 

回答by fmk

I know it's a bit late to this answer, but in my case just upgraded iTextSharp to v. 5.5 from 5.0 and started to work correctly.

我知道这个答案有点晚了,但就我而言,刚刚将 iTextSharp 从 5.0 升级到 v. 5.5 并开始正常工作。