2016-12-26 10 views
1

本文には、キリル文字でitextpdfを動作させる方法がありますか?itextpdfでPDFに変換するとキリル文字が正しく表示されるようにするにはどうすればよいですか?

I have code: 

Font normal = FontFactory.getFont(String.valueOf(Font.FontFamily.HELVETICA), "CP1251", false, 13); 
Document doc = new Document(PageSize.A4, 36, 36, 36, 65); 
Paragraph paragraph = new Paragraph("ЗАПИСЬ!!!", normal); 
doc.add(paragraph); 

私はCP1251は良い動作することを見たが、テキストの単一のchar -whileのための - (私の例では "ЗАПИСЬ")。お互いに重なっているすべての文字を表示します。

私のコードで何が問題になっていますか? Thnx!

+2

は、Unicodeエンコーディングで、例えばarial.ttfのように、それらの文字をサポートするフォントを使用します。 –

+2

@PauloSoaresのコメントの上に追加の注釈があります:あなたがどこにでも働くことを保証するためにarial以外のものを使用する場合は、選択したttfフロントを埋め込むこともできます。 \t 'BaseFont bf_russian = BaseFont.createFont(" location/of/your/font.ttf "、" CP1251 "、BaseFont.EMBEDDED); フォントの標準=新しいフォント(bf_russian、12); ' – sorifiend

答えて

1

フォントソースは、ネットワーク(http://fonts4web.ru/Helvetica.html#test)からダウンロードできます。

リソースディレクトリにフォントを保存します。

法が蛇腹例にあるように:

public class HelloWorld { 

    /** Path to the resulting PDF file. */ 
    public static final String RESULT = "results/hello.pdf"; 
    public static final String FONT = "fonts/HelveticaRegular.ttf"; 

    /** 
    * Creates a PDF file: hello.pdf 
    * @param args no arguments needed 
    */ 
    public static void main(String[] args) 
      throws DocumentException, IOException { 
     new HelloWorld().createPdf(RESULT); 
    } 

    /** 
    * Creates a PDF document. 
    * @param filename the path to the new PDF document 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public void createPdf(String filename) 
      throws DocumentException, IOException { 
     Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, true); 

     Document document = new Document(); 

     PdfWriter.getInstance(document, new FileOutputStream(filename)); 

     document.open(); 

     document.add(new Paragraph("Hello World! Ты можешь использовать кирилицу.", font)); 

     document.close(); 
    } 
} 
+0

素晴らしい!作品!ありがとう。 – Lena

関連する問題