1

中国語/韓国語の文字を含む HTML 文字列があります。iTextを使用してHTMLをPDFに変換したい。Unicode 文字を PDF に表示するには、FONT を PDF に埋め込む必要があることを読みました。

wts11.ttf (エンコーディング IDENTITY_H を使用) または STSong-Light (エンコーディング UniGB-UCS2-H を使用) を埋め込もうとすると、中国語の文字しか表示されず、韓国語の文字が表示されません。arialuni.ttf (エンコーディング IDENTITY_H) を使用してみましたが、まだ中国語しか見えず、韓国語は見えません。

正確なフォントを教えてください。または、何か不足している場合。

以下はコード スニペットです。

Document document = new Document();
Paragraph paragraph=new Paragraph();
PdfWriter.getInstance(document, baos);
document.open();
BaseFont bff = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Font f = new Font(bff);

// FontFactory.registerDirectories(); 
// Font f = FontFactory.getFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

document.add(new Paragraph());
HTMLWorker htmlWorker = new HTMLWorker(document);

List<Element> objects=htmlWorker.parseToList(new StringReader(message),null);
paragraph.setFont(f);
for (Element elem : objects) {
    paragraph.add(elem);
}
document.add(paragraph);
4

2 に答える 2

1

XML Worker を使用するようにアップグレードする場合、この問題を解決するさまざまな方法があります。

公式の例、より具体的にはParseHtmlAsianの例のコードを再利用し、この例のソースとして使用されている HTML を次のように適合させました。

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p><span style="font-size:12.0pt; font-family:MS Mincho">長空</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Broken Sword),</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">秦王殘劍</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Flying Snow),</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">飛雪</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Moon), </span>
    <span style="font-size:12.0pt; font-family:MS Mincho">如月</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(the King), and</span>
    <span style="font-size:12.0pt; font-family:MS Mincho">秦王</span>
    <span style="font-size:12.0pt; font-family:Times New Roman,serif">(Sky).</span></p>
    <p style="font-size: 12.0pt; font-family:Batang">빈집</p>
    <p>Test</p>
    </body>
</html>

結果は次のようになります。

ここに画像の説明を入力

ご覧のとおり、すべてのテキストが正しくレンダリングされているため、「iText が中国語/韓国語の文字をレンダリングしていません」などの誤ったメッセージを広めないでください ;-)

古い iText バージョンに時間を費やすことは、新しい iText バージョンを使用するためのライセンスを購入するよりも費用がかかることを CTO が理解できるように、この回答を経営陣に転送してください。

于 2015-01-09T18:44:03.480 に答える