Ejemplo n.º 1
0
        /// <summary>
        /// Converts a raw string into a raw hexadecimal string literal, possibly encrypted.
        /// </summary>
        public static string ToHexStringLiteral(string text, PdfStringEncoding encoding, PdfStandardSecurityHandler securityHandler)
        {
            if (String.IsNullOrEmpty(text))
            {
                return("<>");
            }

            byte[] bytes;
            switch (encoding)
            {
            case PdfStringEncoding.RawEncoding:
                bytes = RawEncoding.GetBytes(text);
                break;

            case PdfStringEncoding.WinAnsiEncoding:
                bytes = WinAnsiEncoding.GetBytes(text);
                break;

            case PdfStringEncoding.PDFDocEncoding:
                bytes = DocEncoding.GetBytes(text);
                break;

            case PdfStringEncoding.Unicode:
                //bytes = UnicodeEncoding.GetBytes(text);
                bytes = RawUnicodeEncoding.GetBytes(text);
                break;

            default:
                throw new NotImplementedException(encoding.ToString());
            }

            byte[] agTemp = FormatStringLiteral(bytes, encoding == PdfStringEncoding.Unicode, true, true, securityHandler);
            return(RawEncoding.GetString(agTemp, 0, agTemp.Length));
        }
Ejemplo n.º 2
0
        ///// <summary>
        ///// Encodes a string from a byte array. Each character gets the code of the corresponding byte.
        ///// </summary>
        //public static string RawString(byte[] bytes, int offset, int length)
        //{
        //  char[] chars = new char[length];
        //  for (int idx = offset, ch = 0; idx < offset +  length; idx++, ch++)
        //    chars[ch] = (char)bytes[idx];
        //  return new string(chars, 0, length);
        //}
        //
        //public static string RawString(byte[] bytes)
        //{
        //  return RawString(bytes, 0, bytes.Length);
        //}

#if true_
        public static string EncodeAsLiteral(string text, bool unicode)
        {
            if (text == null || text == "")
            {
                return("<>");
            }

            StringBuilder pdf = new StringBuilder("");

            if (!unicode)
            {
                byte[] bytes = WinAnsiEncoding.GetBytes(text);
                int    count = bytes.Length;
                pdf.Append("(");
                for (int idx = 0; idx < count; idx++)
                {
                    char ch = (char)bytes[idx];
                    if (ch < 32)
                    {
                        switch (ch)
                        {
                        case '\n':
                            pdf.Append("\\n");
                            break;

                        case '\r':
                            pdf.Append("\\r");
                            break;

                        case '\t':
                            pdf.Append("\\t");
                            break;

                        case '\f':
                            pdf.Append("\\f");
                            break;

                        default:
                            pdf.Append(InvalidChar);     // TODO
                            break;
                        }
                    }
                    else
                    {
                        switch (ch)
                        {
                        case '(':
                            pdf.Append("\\(");
                            break;

                        case ')':
                            pdf.Append("\\)");
                            break;

                        case '\\':
                            pdf.Append("\\\\");
                            break;

                        default:
                            pdf.Append(ch);
                            break;
                        }
                    }
                }
                pdf.Append(')');
            }
            else
            {
                pdf.Append("<");
                byte[] bytes = UnicodeEncoding.GetBytes(text);
                int    count = bytes.Length;
                for (int idx = 0; idx < count; idx += 2)
                {
                    pdf.AppendFormat("{0:X2}{1:X2}", bytes[idx + 1], bytes[idx]);
                    if (idx != 0 && (idx % 48) == 0)
                    {
                        pdf.Append("\n");
                    }
                }
                pdf.Append(">");
            }
            return(pdf.ToString());
        }