Beispiel #1
0
        /**
         * Returns the content of this string as a PDF <i>text string</i>.
         */
        public string GetString()
        {
            // text string - BOM indicates Unicode
            if (Bytes.Length >= 2)
            {
                if ((Bytes[0] & 0xff) == 0xFE && (Bytes[1] & 0xff) == 0xFF)
                {
                    Encoding.BigEndianUnicode.GetString(Bytes, 2, Bytes.Length - 2);
                }
                else if ((Bytes[0] & 0xff) == 0xFF && (Bytes[1] & 0xff) == 0xFE)
                {
                    Encoding.Unicode.GetString(Bytes, 2, Bytes.Length - 2);
                }
            }

            // otherwise use PDFDocEncoding
            return(PdfDocEncoding.ToString(Bytes));
        }
Beispiel #2
0
        /**
         * Creates a new <i>text string</i> from a Java string.
         *
         * @param text The string value of the object.
         */
        public CosString(string text)
        {
            // check whether the string uses only characters available in PDFDocEncoding
            bool isOnlyPdfDocEncoding = true;

            for (int i = 0; i < text.Length; i++)
            {
                if (!PdfDocEncoding.ContainsChar(text[i]))
                {
                    isOnlyPdfDocEncoding = false;
                    break;
                }
            }

            if (isOnlyPdfDocEncoding)
            {
                // PDFDocEncoded string
                Bytes = PdfDocEncoding.GetBytes(text);
            }
            else
            {
                // UTF-16BE encoded string with a leading byte order marker
                byte[] data = Encoding.BigEndianUnicode.GetBytes(text);

                using (var outBytes = new MemoryStream(data.Length + 2))
                    using (var w = new StreamWriter(outBytes))
                    {
                        w.Write(0xFE); // BOM
                        w.Write(0xFF); // BOM

                        try
                        {
                            w.Write(data);
                        }
                        catch (IOException e)
                        {
                            // should never happen
                            throw new InvalidOperationException("Fatal Error", e);
                        }

                        Bytes = outBytes.ToArray();
                    }
            }
        }