2016-08-02 92 views
0

我有一些文字包含歐米茄字符(0x3A9),並且當我以默認的Helvetica字體顯示此字符時,PDF中不顯示任何內容。iTextSharp - 並非所有字符都出現在Helvetica字體中

我使用

BaseFont Helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 

創建我的字體,如市場預期,Helvetica.CharExists(0x3A9)返回false。

我還爲TIMES_ROMAN,SYMBOL和ZAPF_DINGBATS字體調用了CharExists(0x3A9)。所有返回false。

  1. 我創建的字體有誤嗎?有沒有更好的方法來創建包含歐米茄角色的字體?
  2. 有沒有一種通用的方式來處理這樣的缺失字符?我應該枚舉所有可用的字體,直到找到一個CharExists(0x3A9)返回true的地方?

我的文檔是英文的,但有特殊字符一知半解 - 加/減,左,右雙引號,微米等

附:我看到我可以創建一個Arial字體:

BaseFont arial = BaseFont.CreateFont("c:\\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

並且確實包含字符0x3A9的字形。但顯然我無法用IDENTITY_H編碼創建HELVETICA字體。

+1

HELVETICA,TIMES_ROMAN,SYMBOL和ZAPF_DINGBATS是標準的14種字體,每種pdf查看器都必須提供一定的有限字符集。如果你需要更多的字符,你必須自己提供一個字體,就像你爲Arial做的一樣。 – mkl

回答

0

我最終結果是有點複雜,但似乎很穩固。每次我將文本添加到我的文檔中,直到現在,我通過簡單地創建一個新的短語來完成文本,我首先枚舉字符串中的每個字符並調用BaseFont.CharExists()。如果對於任何字符的答案都是錯誤的,我會根據CharExists()的連續序列(true或false)將文本分成塊。然後,對於Helvetica中存在的塊,我在Helvetica中創建一個塊,其他人創建Arial塊,並將這些塊組合成我的短語。

我這樣做的原因(這對大多數讀者來說可能是顯而易見的,但以防萬一)是我不想總是將Arial字體添加到我的PDF文件(以保持尺寸不變),並且95我的文檔的%不包含像Omega這樣需要Arial的字符。這樣,我只將Arial字體添加到實際需要它的少數文檔中。

Phrase phrase = null; 
string s = ""; 
for (int i = 0 ; i < text.Length ; i++) 
{ 
    char c = text[i]; 
    if (fonts.Regular.BaseFont.CharExists(c)) 
     s += c; // Accumulate Helvetica characters in 's' 
    else 
    { 
     // Try adding the Arial font 
     iTextSharp.text.Font arial = fonts.Arial;  // Creates the Arial font and adds it to the document if not already there 
     if (arial == null) 
     { 
     Debug.WriteLine(" Arial font not found"); 
     s += c; 
     } 
     else if (!arial.BaseFont.CharExists(c)) 
     { 
     Debug.WriteLine(" Arial does not contain the required glyph"); 
     s += c; 
     } 
     else 
     { 
     // We have a character that Helvetica doesn't have, that Arial does 
     if (phrase == null) 
     { 
      // This is the first time we've realize that we need a split phrase 
      if (s.Length > 0) 
      { 
       // There was Helvetica text before the Arial character. 
       phrase = new Phrase(s, fonts.Regular); 
       // This code assumes that non-Helvetica characters generally come singly 
       phrase.Add(new Chunk(c, arial)); 
       s = ""; 
      } 
      else 
      { 
       // The Arial character is the first one in the string 
       phrase = new Phrase(new Chunk(c, arial)); 
      } 
     } 
     else 
     { 
      // This is not the first Arial character in the string 
      if (s != "") 
      { 
       // There were Helvetica characters before this Arial character 
       phrase.Add(new Chunk(s, fonts.Regular); 
       s = ""; 
      } 
      phrase.Add(new Chunk(c, arial)); 
     } 
     } 
    } 
} 

if (phrase == null) 
{ 
    // We did not encounter any Arial characters, create a simple Phrase 
    phrase = new Phrase(text, fonts.Regular); 
} 
else if (s.Length > 0) 
{ 
    // There were Helvetica characters at the end of the string, add them now 
    phrase.Add(new Chunk(s, fonts.Regular)); 
} 
相關問題