我正在尝试使用 iTextPDF 的 XMLWorkerHelper 生成 PDF 文件。最初,我使用 Apache Velocity 模板生成 HTML 代码(因此使用了 XMLWorkerHelper),但对于这个问题,我将只提供一个简单的 HTML 行,其中包含日文字符。
public class iTextJapChars {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileOutputStream fos = new FileOutputStream("iTextJapChars.pdf");
(new iTextJapChars()).generate(fos);
fos.close();
System.out.println("done!");
}
public void generate(OutputStream os) throws IOException
{
// create a document
Document document = new Document();
try
{
String content = "<html><head></head><body><div><p>begin てすと end</p></div></body></html>";
InputStream stream = new ByteArrayInputStream(content.getBytes("UTF-8"));
System.out.println(content);
System.out.println(Charset.defaultCharset());
// create a writer with an output stream passed as an argument
PdfWriter writer = PdfWriter.getInstance(document, os);
document.addTitle("身 元 保 証 書");
document.addAuthor("れお");
document.addSubject("");
document.addKeywords("iText, PDF");
document.addCreator("test.iTextPDF.generate() using iText");
document.open(); // open the document now
XMLWorkerHelper.getInstance().parseXHtml(writer, document, stream);
}
catch (DocumentException de) { throw new IOException(de.getMessage()); }
finally { document.close(); } // close the document
}
问题是,我无法让生成的 PDF 文件显示日文字符。它只显示空白。以下是需要注意的事项:
- CentOS 6.4
- iTextPDF 5.4.3
- XMLWorker 5.4.1
- Adobe 阅读器 9.5.5
希望您能在这里帮助我,因为我已经在网上搜索了一个多星期,但我无法使用 XMLWorkerHelper 获得任何解决方案。如果有任何我遗漏的信息,或者我提到了错误的信息,请告诉我,以便我更新。任何帮助将非常感激。
非常感谢。