public static byte[] ConvertToBytes(string text, string encoding) {
if (text == null)
return new byte[0];
if (encoding == null || encoding.Length == 0) {
int len = text.Length;
byte[] b = new byte[len];
for (int k = 0; k < len; ++k)
b[k] = (byte)text[k];
return b;
}
IExtraEncoding extra = (IExtraEncoding)extraEncodings[encoding.ToLower(System.Globalization.CultureInfo.InvariantCulture)];
if (extra != null) {
byte[] b = extra.CharToByte(text, encoding);
if (b != null)
return b;
}
IntHashtable hash = null;
if (encoding.Equals(BaseFont.CP1252))
hash = winansi;
else if (encoding.Equals(PdfObject.TEXT_PDFDOCENCODING))
hash = pdfEncoding;
if (hash != null) {
char[] cc = text.ToCharArray();
int len = cc.Length;
int ptr = 0;
byte[] b = new byte[len];
int c = 0;
for (int k = 0; k < len; ++k) {
char char1 = cc[k];
if (char1 < 128 || (char1 > 160 && char1 <= 255))
c = char1;
else
c = hash[char1];
if (c != 0)
b[ptr++] = (byte)c;
}
if (ptr == len)
return b;
byte[] b2 = new byte[ptr];
Array.Copy(b, 0, b2, 0, ptr);
return b2;
}
Encoding encw = IanaEncodings.GetEncodingEncoding(encoding);
byte[] preamble = encw.GetPreamble();
if (preamble.Length == 0)
return encw.GetBytes(text);
byte[] encoded = encw.GetBytes(text);
byte[] total = new byte[encoded.Length + preamble.Length];
Array.Copy(preamble, 0, total, 0, preamble.Length);
Array.Copy(encoded, 0, total, preamble.Length, encoded.Length);
return total;
}