为iTextSharp增加CID-keyed 字体(简称CIDFont)支持,让你在没有中文(CJK)TrueType字体(.TTF,.TTC)环境下一样完美显示中文(CJK)...

CID-keyed 字体(简称CIDFont)。这种字体是Adobe公司为大字符集语言设计的,其中包含了一象形文字,由字符ID(CID)进行索引。
为使这种文字有意义,Adobe提供了一套CMap文件,从CIDFont文件中产生的PS字体名词由CIDFont和CMap共同组成,中间用两个短横线相连,举例来说,由CIDFont`Munhwa-Regular'生成,使用CMap`UniKS-UCS2-H'的字体就叫做:

Munhwa-Regular--UniKS-UCS2-H
Java版本的iText为了实现对CJK字体的支持需要以下两个Jar包:

http://prdownloads.sourceforge.net/itextpdf/iTextAsian.jar
http://prdownloads.sourceforge.net/itextpdf/iTextAsianCmaps.jar

可是iTextSharp如何利用这两个包的资源呢?
比如,我们如何成功执行下面这条语句:
None.gif BaseFont bf  =  BaseFont.CreateFont( " STSong-Light " , " UniGB-UCS2-H " ,BaseFont.NOT_EMBEDDED); 
None.gif

经过分析iTextSharp的源代码,可以发现,如果要在iTextSharp中使用CIDFont(上面两个包中的资源),需要做如下工作(具体查看CJKFont.cs的"internal CJKFont(string fontName, string enc, bool emb)"方法):

1.LoadProperties,先将加载cjkfonts.properties,cjkencodings.properties属性

ContractedBlock.gif ExpandedBlockStart.gif LoadProperties
None.gif    private static void LoadProperties() 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
if (propertiesLoaded)
InBlock.gif            
return;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
lock (allFonts) dot.gif{
InBlock.gif            
if (propertiesLoaded)
InBlock.gif                
return;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                Stream isp 
= GetResourceStream(RESOURCE_PATH + "cjkfonts.properties");
InBlock.gif                cjkFonts.Load(isp);
InBlock.gif                isp.Close();
InBlock.gif                isp 
= GetResourceStream(RESOURCE_PATH + "cjkencodings.properties");
InBlock.gif                cjkEncodings.Load(isp);
InBlock.gif                isp.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch dot.gif{
InBlock.gif                cjkFonts 
= new Properties();
InBlock.gif                cjkEncodings 
= new Properties();
ExpandedSubBlockEnd.gif            }

InBlock.gif            propertiesLoaded 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

2.加载所需的CMap(比如,UniGB-UCS2-H)

ExpandedBlockStart.gif ContractedBlock.gif      internal   static   char [] ReadCMap( string  name)  dot.gif {
InBlock.gif        Stream istr 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            name 
= name + ".cmap";
InBlock.gif            istr 
= GetResourceStream(RESOURCE_PATH + name);
InBlock.gif            
char[] c = new char[0x10000];
InBlock.gif            
for (int k = 0; k < 0x10000++k)
InBlock.gif                c[k] 
= (char)((istr.ReadByte() << 8+ istr.ReadByte());
InBlock.gif            
return c;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
catch dot.gif{
InBlock.gif            
// empty on purpose
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
finally dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
trydot.gif{istr.Close();}catchdot.gif{}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return null;
ExpandedBlockEnd.gif    }

3.读取字体信息(比如,STSong-Light)
ExpandedBlockStart.gif ContractedBlock.gif      internal   static  Hashtable ReadFontProperties(String name)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            name 
+= ".properties";
InBlock.gif            Stream isp 
= GetResourceStream(RESOURCE_PATH + name);
InBlock.gif            Properties p 
= new Properties();
InBlock.gif            p.Load(isp);
InBlock.gif            isp.Close();
InBlock.gif            IntHashtable W 
= CreateMetric(p["W"]);
InBlock.gif            p.Remove(
"W");
InBlock.gif            IntHashtable W2 
= CreateMetric(p["W2"]);
InBlock.gif            p.Remove(
"W2");
InBlock.gif            Hashtable map 
= new Hashtable();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
foreach (string key in p) dot.gif{
InBlock.gif                map[key] 
= p[key];
ExpandedSubBlockEnd.gif            }

InBlock.gif            map[
"W"= W;
InBlock.gif            map[
"W2"= W2;
InBlock.gif            
return map;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
catch dot.gif{
InBlock.gif            
// empty on purpose
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
return null;
ExpandedBlockEnd.gif    }

None.gif

不知道大家有没有注意,上面三个方法(函数)都是从Embeded Resource(嵌入资源)里加载的,而iTextSharp的资源文件里并没有上面两个Jar包中的资源(只有一些AFM的字体资源),那我们接下来要做的事情就是把CIDFont和CMAP文件嵌入到iTextSharp,但是该放在那个目录呢,自然是RESOURCE_PATH常量所指的地方了,看下RESOURCE_PATH的定义(在BaseFont.cs):
        /** The path to the font resources. */   
        public const string RESOURCE_PATH = "iTextSharp.text.pdf.fonts.";
下面该怎么做,也许不用我说了吧(稍微提醒下,这些文件需以资源嵌入的方式加入iTextSharp?

下面附上简单的利用代码:
 1 None.gif using  System;
 2 None.gif using  iTextSharp.text;
 3 None.gif using  iTextSharp.text.pdf;
 4 None.gif using  System.IO;
 5 None.gif
 6 None.gif namespace  cjk
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// CIDFont Demo 的摘要说明。
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class CIDFontDemo
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
14InBlock.gif        /// 应用程序的主入口点。
15ExpandedSubBlockEnd.gif        /// </summary>

16InBlock.gif        [STAThread]
17InBlock.gif        static void Main(string[] args)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            Console.WriteLine("Japanese characters.");
20InBlock.gif
21InBlock.gif            Document document = new Document();
22InBlock.gif            Document.Compress = false;
23InBlock.gif            try 
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                PdfWriter.GetInstance(document, new FileStream(@"E:\java\Japanese.pdf",FileMode.Create));
26InBlock.gif                document.Open();
27InBlock.gif
28InBlock.gif                BaseFont bf = BaseFont.CreateFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
29InBlock.gif                
30InBlock.gif                Font font1=new Font(bf,12,Font.NORMAL);
31InBlock.gif                document.Add(new Paragraph("顽石",font1));
32ExpandedSubBlockEnd.gif            }

33InBlock.gif            catch (Exception ee)
34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
35InBlock.gif                Console.WriteLine(ee.Message);
36ExpandedSubBlockEnd.gif            }

37InBlock.gif            document.Close();
38ExpandedSubBlockEnd.gif        }

39ExpandedSubBlockEnd.gif    }

40ExpandedBlockEnd.gif}

41 None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值