在《为iTextSharp增加CID-keyed 字体(简称CIDFont)支持,让你在没有中文(CJK)TrueType字体(.TTF,.TTC)环境下一样完美显示中文(CJK)》一文中介绍的方法是把所有(或者想要的)CIDFont和Cmap嵌入到iTextSharp中,这时只要留心就会发现,iText的体积翻倍了,而且如果有了新的CIDFont,你还要重新编译iTextSharp,有没有别的解决办法呢?
下面介绍的就是不使用iTextSharp的嵌入资源的方式,而是根据需要从文件中直接读取(当然你也可以把所需资源嵌入到你的程序中,不是iTextSharp中),解决办法当然还是从上文提到的三个方法(函数)动手了,那就是修改CJKFont.cs类,下面就把我修改好的贴在下面,有兴趣的自己研究吧,有什么好的建议也望不吝赐教。
下面介绍的就是不使用iTextSharp的嵌入资源的方式,而是根据需要从文件中直接读取(当然你也可以把所需资源嵌入到你的程序中,不是iTextSharp中),解决办法当然还是从上文提到的三个方法(函数)动手了,那就是修改CJKFont.cs类,下面就把我修改好的贴在下面,有兴趣的自己研究吧,有什么好的建议也望不吝赐教。
CJKFont.cs
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.util;
/**//*
* $Id: CJKFont.cs,v 1.1 2005/03/15 21:07:01 geraldhenson Exp $
* $Name: $
*
* Copyright 2000, 2001, 2002 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**//**
* Creates a CJK font compatible with the fonts in the Adobe Asian font Pack.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public class CJKFont : BaseFont {
/**//** The encoding used in the PDF document for CJK fonts
*/
internal const string CJK_ENCODING = "UTF-16BE";
private const int FIRST = 0;
private const int BRACKET = 1;
private const int SERIAL = 2;
private const int V1Y = 880;
internal static Properties cjkFonts = new Properties();
internal static Properties cjkEncodings = new Properties();
internal static Hashtable allCMaps = Hashtable.Synchronized(new Hashtable());
internal static Hashtable allFonts = Hashtable.Synchronized(new Hashtable());
private static bool propertiesLoaded = false;
/**//** The font name */
private string fontName;
/**//** The style modifier */
private string style = "";
/**//** The CMap name associated with this font */
private string CMap;
private bool cidDirect = false;
private char[] translationMap;
private IntHashtable vMetrics;
private IntHashtable hMetrics;
private Hashtable fontDesc;
private bool vertical = false;
新增方法#region 新增方法
public static void LoadProperties(string CJK_RESOURCE_PATH)
{
if (propertiesLoaded)
return;
lock (allFonts)
{
if (propertiesLoaded)
return;
try
{
Stream isp=new FileStream(CJK_RESOURCE_PATH + "cjkfonts.properties",FileMode.Open);
//Stream isp = GetResourceStream(resource_path + "cjkfonts.properties");
cjkFonts.Load(isp);
isp.Close();
//isp = GetResourceStream(resource_path + "cjkencodings.properties");
isp=new FileStream(CJK_RESOURCE_PATH + "cjkencodings.properties",FileMode.Open);
cjkEncodings.Load(isp);
isp.Close();
propertiesLoaded = true;
}
catch (Exception ex)
{
throw;
}
}
}
public static void AddCMap(string CJK_RESOURCE_PATH,string name)
{
if (allCMaps.ContainsKey(name))
return;
string key=name;
Stream istr = null;
try
{
name = name + ".cmap";
//istr = GetResourceStream(RESOURCE_PATH + name);
istr=new FileStream(CJK_RESOURCE_PATH + name,FileMode.Open);
char[] c = new char[0x10000];
for (int k = 0; k < 0x10000; ++k)
c[k] = (char)((istr.ReadByte() << 8) + istr.ReadByte());
allCMaps.Add(key,c);
}
catch (Exception ex)
{
throw;
}
finally
{
try{istr.Close();}
catch{}
}
}
public static void AddFontProperties(string CJK_RESOURCE_PATH,String name)
{
if (allFonts.ContainsKey(name))
return;
string fontName=name;
try
{
name += ".properties";
Stream isp = new FileStream(CJK_RESOURCE_PATH + name,FileMode.Open);
Properties p = new Properties();
p.Load(isp);
isp.Close();
IntHashtable W = CreateMetric(p["W"]);
p.Remove("W");
IntHashtable W2 = CreateMetric(p["W2"]);
p.Remove("W2");
Hashtable map = new Hashtable();
foreach (string key in p)
{
map[key] = p[key];
}
map["W"] = W;
map["W2"] = W2;
allFonts.Add(fontName,map);
}
catch (Exception ex)
{
throw;
}
}
#endregion
private static void LoadProperties()
{
if (propertiesLoaded)
return;
lock (allFonts) {
if (propertiesLoaded)
return;
try {
Stream isp = GetResourceStream(RESOURCE_PATH + "cjkfonts.properties");
cjkFonts.Load(isp);
isp.Close();
isp = GetResourceStream(RESOURCE_PATH + "cjkencodings.properties");
cjkEncodings.Load(isp);
isp.Close();
}
catch {
cjkFonts = new Properties();
cjkEncodings = new Properties();
}
propertiesLoaded = true;
}
}
/**//** Creates a CJK font.
* @param fontName the name of the font
* @param enc the encoding of the font
* @param emb always <CODE>false</CODE>. CJK font and not embedded
* @throws DocumentException on error
* @throws IOException on error
*/
internal CJKFont(string fontName, string enc, bool emb) {
LoadProperties();
this.FontType = FONT_TYPE_CJK;
string nameBase = GetBaseName(fontName);
if (!IsCJKFont(nameBase, enc))
throw new DocumentException("Font '" + fontName + "' with '" + enc + "' encoding is not a CJK font.");
if (nameBase.Length < fontName.Length) {
style = fontName.Substring(nameBase.Length);
fontName = nameBase;
}
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.util;
/**//*
* $Id: CJKFont.cs,v 1.1 2005/03/15 21:07:01 geraldhenson Exp $
* $Name: $
*
* Copyright 2000, 2001, 2002 by Paulo Soares.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text.pdf {
/**//**
* Creates a CJK font compatible with the fonts in the Adobe Asian font Pack.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public class CJKFont : BaseFont {
/**//** The encoding used in the PDF document for CJK fonts
*/
internal const string CJK_ENCODING = "UTF-16BE";
private const int FIRST = 0;
private const int BRACKET = 1;
private const int SERIAL = 2;
private const int V1Y = 880;
internal static Properties cjkFonts = new Properties();
internal static Properties cjkEncodings = new Properties();
internal static Hashtable allCMaps = Hashtable.Synchronized(new Hashtable());
internal static Hashtable allFonts = Hashtable.Synchronized(new Hashtable());
private static bool propertiesLoaded = false;
/**//** The font name */
private string fontName;
/**//** The style modifier */
private string style = "";
/**//** The CMap name associated with this font */
private string CMap;
private bool cidDirect = false;
private char[] translationMap;
private IntHashtable vMetrics;
private IntHashtable hMetrics;
private Hashtable fontDesc;
private bool vertical = false;
新增方法#region 新增方法
public static void LoadProperties(string CJK_RESOURCE_PATH)
{
if (propertiesLoaded)
return;
lock (allFonts)
{
if (propertiesLoaded)
return;
try
{
Stream isp=new FileStream(CJK_RESOURCE_PATH + "cjkfonts.properties",FileMode.Open);
//Stream isp = GetResourceStream(resource_path + "cjkfonts.properties");
cjkFonts.Load(isp);
isp.Close();
//isp = GetResourceStream(resource_path + "cjkencodings.properties");
isp=new FileStream(CJK_RESOURCE_PATH + "cjkencodings.properties",FileMode.Open);
cjkEncodings.Load(isp);
isp.Close();
propertiesLoaded = true;
}
catch (Exception ex)
{
throw;
}
}
}
public static void AddCMap(string CJK_RESOURCE_PATH,string name)
{
if (allCMaps.ContainsKey(name))
return;
string key=name;
Stream istr = null;
try
{
name = name + ".cmap";
//istr = GetResourceStream(RESOURCE_PATH + name);
istr=new FileStream(CJK_RESOURCE_PATH + name,FileMode.Open);
char[] c = new char[0x10000];
for (int k = 0; k < 0x10000; ++k)
c[k] = (char)((istr.ReadByte() << 8) + istr.ReadByte());
allCMaps.Add(key,c);
}
catch (Exception ex)
{
throw;
}
finally
{
try{istr.Close();}
catch{}
}
}
public static void AddFontProperties(string CJK_RESOURCE_PATH,String name)
{
if (allFonts.ContainsKey(name))
return;
string fontName=name;
try
{
name += ".properties";
Stream isp = new FileStream(CJK_RESOURCE_PATH + name,FileMode.Open);
Properties p = new Properties();
p.Load(isp);
isp.Close();
IntHashtable W = CreateMetric(p["W"]);
p.Remove("W");
IntHashtable W2 = CreateMetric(p["W2"]);
p.Remove("W2");
Hashtable map = new Hashtable();
foreach (string key in p)
{
map[key] = p[key];
}
map["W"] = W;
map["W2"] = W2;
allFonts.Add(fontName,map);
}
catch (Exception ex)
{
throw;
}
}
#endregion
private static void LoadProperties()
{
if (propertiesLoaded)
return;
lock (allFonts) {
if (propertiesLoaded)
return;
try {
Stream isp = GetResourceStream(RESOURCE_PATH + "cjkfonts.properties");
cjkFonts.Load(isp);
isp.Close();
isp = GetResourceStream(RESOURCE_PATH + "cjkencodings.properties");
cjkEncodings.Load(isp);
isp.Close();
}
catch {
cjkFonts = new Properties();
cjkEncodings = new Properties();
}
propertiesLoaded = true;
}
}
/**//** Creates a CJK font.
* @param fontName the name of the font
* @param enc the encoding of the font
* @param emb always <CODE>false</CODE>. CJK font and not embedded
* @throws DocumentException on error
* @throws IOException on error
*/
internal CJKFont(string fontName, string enc, bool emb) {
LoadProperties();
this.FontType = FONT_TYPE_CJK;
string nameBase = GetBaseName(fontName);
if (!IsCJKFont(nameBase, enc))
throw new DocumentException("Font '" + fontName + "' with '" + enc + "' encoding is not a CJK font.");
if (nameBase.Length < fontName.Length) {
style = fontName.Substring(nameBase.Length);
fontName = nameBase;
}