2012-06-07 98 views
2

我有一個帶有文本字段和複選框的XHTML文件;當我試圖將我的xhtml轉換爲使用ITextRenderer的pdf; iText正在放棄表單域(如pdf中的複選框和文本框)。itext將xhtml轉換爲pdf

我的代碼看起來

ITextRenderer renderer = new ITextRenderer(); 
renderer.setDocument(new File("input.xhtml").toURI().toURL.toString()); 
renderer.layout(); 
renderer.createPDF("outputstream"); 

有人能提供的示例代碼段來處理利用iText上面的場景;我正在使用iText 5.1.3版本。

我的XHTML看起來像

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>FULL</title> 
     <style media="screen" type="text/css"> 
        fieldset table { width:98%;border: 1px groove; margin:0 auto; } 
        fieldset table tr { border: 1px groove; } 
        fieldset table th { border: 1px groove; } 
        fieldset table td { border: 1px groove; } 
        fieldset &gt; table [id=inspectionStatus] thead {border: 1px groove;background:#446BEC;text-align:center;} 
        fieldset &gt; table [id=inspectionStatus] tr { border: 1px groove; } 
        fieldset &gt; table [id=inspectionStatus] th { border: 1px groove; } 
        fieldset &gt; table [id=inspectionStatus] td { border: 1px groove; } 
        fieldset table thead {border: 1px groove;background:#446BEC;text-align:center;} 
        fieldset table tbody tr th {text-align:left;background:#C6DEFF;width:28%} 
        fieldset table tbody tr td {text-align:center} [type=text] {margin-left:5%;} *[type=text]{width:90%;} 
        fieldset table tbody tr td [type=checkbox] {margin:0 auto;} *[type=checkbox]{width:90%;} 
        fieldset table caption {font-weight:bold;color:#0840F8;} 
        fieldset {width:98%;font-weight:bold;border:1px solid #446BEC;} 
       </style> 
    </head> 
    <body> 
    <fieldset id="PersonTable"> 
     <legend>Person Information</legend> 
     <table id="PersonDisplay"> 
      <thead> 
       <tr> 
        <th>Identifier</th> 
        <th>Name</th> 
        <th>Comment</th> 
        <th>Pass</th> 
        <th>Fail</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>1234</td> 
        <td> William Jones</td> 
        <td> 
        <input type="text"/> 
        </td> 
        <td> 
        <input type="checkbox"/> 
        </td> 
        <td> 
        <input type="checkbox"/> 
        </td> 
       </tr> 
      </tbody> 
     </table> 
     </fieldset> 
    </body> 
</html> 
+0

那麼你的'input.xhtml'文件是什麼樣子? –

+0

我已經更新了上面的描述以顯示我想要轉換爲PDF的示例XHTML –

+0

另外,請注意renderer.setDocument(new File(「input.xhtml」).toURI()。toURL.toString()); 不會爲您提供文件內容。它只是給你文件路徑(在這種情況下作爲URI)。 –

回答

3

這樣做的最簡單的方法是使用pdfHTML。 這是一個將HTML5(+ CSS3)轉換爲pdf語法的iText7插件。

的代碼非常簡單:

HtmlConverter.convertToPdf(
     "<b>This text should be written in bold.</b>", // html to be converted 
     new PdfWriter(
      new File("C://users/lsamudrala/output.pdf") // destination file 
     ) 
    ); 

要了解更多信息,請訪問http://itextpdf.com/itext7/pdfHTML