将Html表单转换为Pdf以及java代码中的单选按钮和复选框

时间:2016-12-01 05:56:53

标签: java html css pdf itext

enter code here我有一个包含100多个字段的表单以及收音机和复选框。什么是从中创建PDF的最佳方式。我使用过iText v5.2.1,但它只打印PDF文本而不是任何输入字段。因为iText不会选择任何HTML表单(单选按钮和复选框输入字段)或css。

main.html中

<!DOCTYPE html>  
<html lang="en">
   <head>
      <meta charset="utf-8">
      <title>JavaScript Form Validation using a sample registration form</title>
      <meta name="keywords" content="example, JavaScript Form Validation, Sample registration form" />
      <meta name="description" content="This document is an example of JavaScript Form Validation using a sample registration form. " />
      <link rel='stylesheet' href='js-form-validation.css' type='text/css' />
      <script src="sample-registration-form-validation.js"></script>
   </head>
   <body onload="document.registration.userid.focus();">
      <h1>Registration Form</h1>
      <p>Use tab keys to move from one input field to the next.</p>
      <form name='registration' onSubmit="return formValidation();">
         <ul>
            <li><label for="userid">User id:</label></li>
            <li><input type="text" name="userid" size="12" /></li>
            <li><label for="passid">Password:</label></li>
            <li><input type="password" name="passid" size="12" /></li>
            <li><label for="username">Name:</label></li>
            <li><input type="text" name="username" size="50" /></li>
            <li><label for="address">Address:</label></li>
            <li><input type="text" name="address" size="50" /></li>
            <li><label for="country">Country:</label></li>
            <li>
               <select name="country">
                  <option selected="" value="Default">(Please select a country)</option>
                  <option value="AF">Australia</option>
                  <option value="AL">Canada</option>
                  <option value="DZ">India</option>
                  <option value="AS">Russia</option>
                  <option value="AD">USA</option>
               </select>
            </li>
            <li><label for="zip">ZIP Code:</label></li>
            <li><input type="text" name="zip" /></li>
            <li><label for="email">Email:</label></li>
            <li><input type="text" name="email" size="50" /></li>
            <li><label id="gender">Sex:</label></li>
            <li><input type="radio" name="msex" value="Male" /><span>Male</span></li>
            <li><input type="radio" name="fsex" value="Female" /><span>Female</span></li>
            <li><label>Language:</label></li>
            <li><input type="checkbox" name="en" value="en" checked /><span>English</span></li>
            <li><input type="checkbox" name="nonen" value="noen" /><span>Non English</span></li>
            <li><label for="desc">About:</label></li>
            <li><textarea name="desc" id="desc"></textarea></li>
            <li><input type="submit" name="submit" value="Submit" /></li>
         </ul>
      </form>
   </body>
</html>

App.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class App 
{
    public static void main( String[] args ) throws DocumentException, IOException
    {
      // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream("main.html")); 
        //step 5
         document.close();

        System.out.println( "PDF Created!" );
    }
}

0 个答案:

没有答案