2013-09-22 26 views
0

我想通過eclipse.I使用itext和java來做以下事情。我需要創建一個PDF,它將包含從數據庫中檢索到的多個選擇題的no。檢索到的數據是以html標記的形式因此我我使用xml工人來解析它。我能夠從數據庫中逐一檢索問題並將其添加到pdf中。但問題是它只佔用頁面的一面,而我需要的問題覆蓋2列的頁面。如何使用itext跨越頁面的2列?

當到達第一個PDF頁面的末尾時,它應該利用第一個PDF頁面的右上角添加問題。只有當頁面的左右兩側都被完全使用時,纔會轉到下一個PDF頁面。

現在我設法使用ColumnText獲取2列中的html數據。但是我現在面臨的問題是,從數據庫中檢索的問題不會按照預期的格式顯示。每個問題都會顯示在一行上。

我進入的問題是這樣的格式:

1)什麼是2 + 2 =?

一個)2

b)中4

C)8

d)15

我想,關於PDF輸出應爲如上。

但是我得到以下的輸出:

1)什麼IS2 + 2 = A)2B)4C)8D)15

我如何保存HTML格式?????? ???

這是我到目前爲止的代碼:

import java.io.ByteArrayInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.StringReader; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.Collection; 

import com.itextpdf.text.Chunk; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Element; 
import com.itextpdf.text.List; 
import com.itextpdf.text.PageSize; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.pdf.ColumnText; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.text.pdf.draw.LineSeparator; 
import com.itextpdf.tool.xml.ElementHandler; 
import com.itextpdf.tool.xml.ElementList; 
import com.itextpdf.tool.xml.Pipeline; 
import com.itextpdf.tool.xml.Writable; 
import com.itextpdf.tool.xml.XMLWorker; 
import com.itextpdf.tool.xml.XMLWorkerHelper; 
import com.itextpdf.tool.xml.html.Tags; 
import com.itextpdf.tool.xml.parser.XMLParser; 
import com.itextpdf.tool.xml.pipeline.WritableElement; 
import com.itextpdf.tool.xml.pipeline.css.CSSResolver; 
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline; 
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline; 
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline; 
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext; 

public class ColumnTextExample { 
    public static final float[][] COLUMNS = { 
     { 36, 36, 224, 579 } , { 230, 36, 418, 579 } 

     }; 
public static void main(String[] args)throws IOException, DocumentException, ClassNotFoundException, SQLException { 
     // TODO Auto-generated method stub 
     Document document = new Document(PageSize.A4.rotate()); 

     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\columns.pdf")); 

     document.open(); 


     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3", "root", "root"); 
     Statement st=con.createStatement(); 
     ResultSet rs=st.executeQuery("select * from exam2"); 
     int size=0; 
     while (rs.next()){ 
     size++; 
     }; 

     ResultSet rs1=st.executeQuery("select * from exam2"); 
     String[] myStringArray = new String[size]; 
     int i=0; 
     while (rs1.next()){ 
      myStringArray[i]=rs1.getString("paper"); 
      i++; 
    } 

     ColumnText ct = new ColumnText(writer.getDirectContent()); 
     for (String article : myStringArray) { 

      ct.addElement(createPhrase(article,writer,document)); 

      ct.addElement(Chunk.NEWLINE); 
      document.add(Chunk.NEWLINE); 
    } 
     ct.setAlignment(Element.ALIGN_CENTER); 
     ct.setExtraParagraphSpace(55); 
     ct.setLeading(0, 1.2f); 
     ct.setFollowingIndent(27); 
     int linesWritten = 0; 
     int column = 0; 
     int status = ColumnText.START_COLUMN; 
     while (ColumnText.hasMoreText(status)) { 
      ct.setSimpleColumn(
        COLUMNS[column][0], COLUMNS[column][1], 
        COLUMNS[column][2], COLUMNS[column][3]); 
      ct.setYLine(COLUMNS[column][3]); 
      status = ct.go(); 
      linesWritten += ct.getLinesWritten(); 
      column = Math.abs(column - 1); 
      if (column == 0) 
       document.newPage(); 
     } 

     ct.addElement(new Phrase("Lines written: " + linesWritten)); 
     ct.go(); 
     document.close(); 
    } 
public static Phrase createPhrase(String myString, PdfWriter writer, Document document) throws IOException, DocumentException { 
     Phrase p = new Phrase(); 
     String myString2=myString+"<html><body><br></br></body></html>"; 
     String k="<br></br>"; 
     XMLWorkerHelper xwh = XMLWorkerHelper.getInstance(); 
     InputStream is = new ByteArrayInputStream(myString.getBytes()); 
     ElementList myList = new ElementList(); 
     ElementList myList1 = new ElementList(); 
     xwh.parseXHtml(myList,new StringReader(myString2)); 

     p.addAll(myList); 

    return p; 
} 
    } 

當我使用XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(name1));我能夠保存HTML格式等新線。

下面的代碼可以幫助我從數據庫中檢索HTML數據,並對其進行解析,同時保留它打印到PDF的formatting.However在單個列

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 
import java.io.InputStream; 
import java.io.StringReader; 

import com.itextpdf.text.Document; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.pdf.PdfWriter; 
import com.itextpdf.tool.xml.XMLWorkerHelper; 
import java.io.ByteArrayInputStream; 
import java.sql.*; 


public class GeneratePDF { 
    public static void main(String[] args) { 
     try { 
      OutputStream file = new FileOutputStream(new File("C:\\Test.pdf")); 
      Document document = new Document(); 
      PdfWriter writer = PdfWriter.getInstance(document, file); 
      document.open(); 

       Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3", "root", "root"); 
        Statement st=con.createStatement(); 

        ResultSet rs=st.executeQuery("select paper from exam2 "); 
        String name1=""; 
        while (rs.next()){ 
         String name = rs.getString("paper"); 
         //out.println(name); 
         name1=name; 


      /*String k="<h1 style='text-align: center;'><strong>Maths&nbsp;Question2 Paper</strong></h1>"+ 
"<pre><strong>1)What is the sum of 2+2??<br /></strong><strong>a)3<br /></strong><strong>b)5<br /></strong><strong>c)4<br /></strong><strong>d)1</strong></pre>"+ 
"<pre><strong>2)What is the sum of 5+2??<br /></strong><strong>a)3<br />b)5<br />c)7<br />d)1</strong></pre>"+ 
"<pre>&nbsp;</pre>";*/ 
        System.out.println(name1); 

      //InputStream is = new ByteArrayInputStream(name1.getBytes()); 
      XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(name1)); 
        } 


      document.close(); 
      file.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
}}} 

回答

0

你解析HTML爲Document。這意味着您希望iText在一頁上組織所有內容,使用由頁面大小(在您的情況A4)和頁邊距(在您的情況下,每邊36pt)定義的一列。

如果您想以不同方式組織內容,則應使用採用ElementHandler作爲參數並傳遞ElementList對象的parseXHTML()方法。然後,此列表將包含一個ListElement對象,您可以將其提供給ColumnText對象。使用ColumnText類,您可以在頁面上定義多個矩形,並使用go()方法將這些矩形填充爲ElementList中的內容。

+0

請你詳細解釋一下。我有一段艱難的時間試圖找出如何做到這一點。 – user2763660

+0

用'parseXHtml(myList,is)'替換'parseXHtml(writer,document,is)',其中'myList'被創建爲:'ElementList myList = new ElementList();'閱讀iText文檔以瞭解如何使用'ColumnText'。如果你不明白:向我們展示你到目前爲止所擁有的。 –

+0

嗨...這就是我設法得到的 – user2763660