ITEXT 的功能和简介这里就不多说了,直接带大家做一遍:
一段可运行的代码比说很多废话强得多
S1 :首先下载 JAR 包
S2:创建 HTML 文档(将要使用的 HTML 文档素材)
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
長空
style="font-size:12.0pt; font-family:Times New Roman,serif">(Broken
Sword),
秦王殘劍(Flying
Snow), 飛雪
(Moon),
如月
style="font-size:12.0pt; font-family:Times New Roman,serif">(the
King), and
秦王(Sky).
S3: 引入 JAR 包 ,编写JAVA 转换代码
package com.zhouqian.itext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class D07_ParseHtmlAsian {
public static final String HTML = "E:/Dropbox/document/java/designPattern/data/hero.html";
public static final String DEST = "E:/Dropbox/document/java/designPattern/data/hero.pdf";
/**
* Creates a PDF with the words "Hello World"
* @param file
* @throws IOException
* @throws DocumentException
*/
public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new FileInputStream(HTML), Charset.forName("UTF-8"));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new D07_ParseHtmlAsian().createPdf(DEST);
}
}
S4:在指定路径下就可以得到 PDF 结果
如果大家有不明白的可以留言问我,一同进步