2

我正在尝试使用 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 获得任何解决方案。如果有任何我遗漏的信息,或者我提到了错误的信息,请告诉我,以便我更新。任何帮助将非常感激。

非常感谢。

4

2 回答 2

0

尝试这个

XMLWorkerHelper.getInstance().parseXHtml(writer, document, stream,Charset.forName("ISO-8859-1"));
于 2013-09-16T07:00:01.073 回答
0

尝试使用以下逻辑它应该可以工作

XMLWorkerHelper.getInstance().parseXHtml(writer, document, stream, Charset.forName("UTF-8"));

请检查 Adob​​e Reader 9.5.5 是否能够红色日文字符,如果没有从“ http://supportdownloads.adobe.com/detail.jsp?ftpID=5877 ”安装语言支持。

Adobe Reader 将采用您的操作系统/机器中存在的显示语言。下面给出的链接将显示为 Windows 7 设置的语言 https://www.wikihow.com/Change-the-Language-in-Windows-7

于 2017-11-24T08:05:41.773 回答