您可以使用以下
HTMLWorker类(不建议使用)来实现:
import com.itextpdf.text.html.simpleparser.HTMLWorker;//...try { String k = "<html><body> This is my Project </body></html>"; OutputStream file = new FileOutputStream(new File("C:\Test.pdf")); document document = new document(); PdfWriter.getInstance(document, file); document.open(); HTMLWorker htmlWorker = new HTMLWorker(document); htmlWorker.parse(new StringReader(k)); document.close(); file.close();} catch (Exception e) { e.printStackTrace();}或使用
XMLWorker,(从此jar下载)使用以下代码:
import com.itextpdf.tool.xml.XMLWorkerHelper;//...try { String k = "<html><body> This is my Project </body></html>"; OutputStream file = new FileOutputStream(new File("C:\Test.pdf")); document document = new document(); PdfWriter writer = PdfWriter.getInstance(document, file); document.open(); InputStream is = new ByteArrayInputStream(k.getBytes()); XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); document.close(); file.close();} catch (Exception e) { e.printStackTrace();}


