2016-12-26 29 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的作品不錯,但對於單個字符-while的文本 - (「ЗАПИСЬ」在我的例子)。它顯示所有重疊在一起的字符。

我的代碼有什麼問題? Thnx!

+2

使用字體爲那些字符,如ARIAL.TTF支持,與Unicode編碼。 –

+2

@PauloSoares頂端的額外註釋評論:如果您使用的不是arial以外的東西來確保它可以在任何地方運作,您可能還想嵌入您選擇的ttf前端。 \t'BaseFont bf_russian = BaseFont.createFont(「location/of/your/font.ttf」,「CP1251」,BaseFont.EMBEDDED); Font normal = new Font(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

相關問題