2

I have a XHTML file with text fields and check boxes; when i am trying to convert my xhtml to pdf using ITextRenderer; iText is dropping form fields (like checkboxes and text boxes in pdf).

my code looks

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

Can some one provide sample code snippet to handle the above scenario using iText; i am using iText 5.1.3 version.

My XHTML looks like

<?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>
4

1 回答 1

3

最简单的方法是使用 pdfHTML。它是一个 iText7 附加组件,可将 HTML5 (+CSS3) 转换为 pdf 语法。

代码非常简单:

    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

于 2017-09-04T07:28:53.617 回答