2

iTextPDF の XMLWorkerHelper を使用して PDF ファイルを生成しようとしています。もともと、私は Apache Velocity Template を使用して 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
  • XML ワーカー 5.4.1
  • アドビ リーダー 9.5.5

1 週間以上ネットを検索しており、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"));

「 http://supportdownloads.adobe.com/detail.jsp?ftpID=5877 」から言語サポートをインストールしていない場合は、Adobe Reader 9.5.5 で日本語の文字を赤字にできることを確認してください。

Adobe Reader は、OS / マシンに存在する表示言語を使用します。以下のリンクを指定すると、Windows 7 の言語設定が表示されます https://www.wikihow.com/Change-the-Language-in-Windows-7

于 2017-11-24T08:05:41.773 に答える