1
   Document document = new Document(PageSize.A4, ml, mr, mt, mb);
   PdfWriter writer=PdfWriter.getInstance(document, response.getOutputStream());
   String sc_na=request.getParameter("school_name");
   String name=sc_na;
   writer.setPageEvent(new HeaderAndFooter(name));
   document.open();
   XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
   //HTMLWorker htmlWorker = new HTMLWorker(document);
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/elect", "root", "root");
   Statement st=con.createStatement();
   ResultSet rs=st.executeQuery("Select * from test3");
   List arrlist = new ArrayList();
   while(rs.next()){
   String xa =rs.getString("display");
   if(xa.equals("1")){
    arrlist.add(rs.getString("question_text"));
   }
 }
 Collections.shuffle(arrlist); 
for(int i=0;i<nb;i++){
    String str = (String) arrlist.get(i);
     worker.parseXHtml(writer, document, new StringReader("<br>"));
     worker.parseXHtml(writer, document, new StringReader(str));

}
document.close();

这是我的代码片段。使用我将所有数据放在一个列中。我应该怎么做才能进入多个列。我猜一种方法是使用 PDFptable,但我如何在此处应用它。请帮助。

4

1 回答 1

0

对于 Html 到 PDF,使用飞碟:http ://code.google.com/p/flying-saucer/

它使用 iText 2.0.8 并且尊重 CSS。它与以下代码一样多:

 org.w3c.dom.Document doc = . . .
 ITextRenderer renderer = new ITextRenderer();
 renderer.setDocument(doc, null);

 renderer.layout();
 renderer.createPDF(out);

确保您的 XHTML 链接到可访问的 CSS,例如

 <link rel="stylesheet" type="text/css" href="file://. . ./pdf.css" media="print"/>
于 2013-09-19T13:12:04.280 回答