Beispiel #1
0
 private void FillEncoding(PdfName encoding)
 {
     if (PdfName.MAC_ROMAN_ENCODING.Equals(encoding) || PdfName.WIN_ANSI_ENCODING.Equals(encoding))
     {
         byte[] b = new byte[256];
         for (int k = 0; k < 256; ++k)
         {
             b[k] = (byte)k;
         }
         String enc = WINANSI;
         if (PdfName.MAC_ROMAN_ENCODING.Equals(encoding))
         {
             enc = MACROMAN;
         }
         String cv  = PdfEncodings.ConvertToString(b, enc);
         char[] arr = cv.ToCharArray();
         for (int k = 0; k < 256; ++k)
         {
             uni2byte[arr[k]] = k;
         }
     }
     else
     {
         for (int k = 0; k < 256; ++k)
         {
             uni2byte[stdEnc[k]] = k;
         }
     }
 }
Beispiel #2
0
 private String DecodeString(PdfString ps)
 {
     if (ps.IsHexWriting())
     {
         return(PdfEncodings.ConvertToString(ps.GetBytes(), "UnicodeBigUnmarked"));
     }
     else
     {
         return(ps.ToUnicodeString());
     }
 }
Beispiel #3
0
        /**
         * Returns the length of the PDF representation of the <CODE>PdfObject</CODE>.
         * <P>
         * In some cases, namely for <CODE>PdfString</CODE> and <CODE>PdfStream</CODE>,
         * this method differs from the method <CODE>length</CODE> because <CODE>length</CODE>
         * returns the length of the actual content of the <CODE>PdfObject</CODE>.</P>
         * <P>
         * Remark: the actual content of an object is in most cases identical to its representation.
         * The following statement is always true: Length() &gt;= PdfLength().</P>
         *
         * @return        a length
         */

        //    public int PdfLength() {
        //        return ToPdf(null).length;
        //    }

        /**
         * Returns the <CODE>String</CODE>-representation of this <CODE>PdfObject</CODE>.
         *
         * @return        a <CODE>String</CODE>
         */

        public override string ToString()
        {
            if (bytes == null)
            {
                return("");
            }
            else
            {
                return(PdfEncodings.ConvertToString(bytes, null));
            }
        }
Beispiel #4
0
        internal void Decrypt(PdfReader reader)
        {
            PdfEncryption decrypt = reader.Decrypt;

            if (decrypt != null)
            {
                originalValue = value;
                decrypt.SetHashKey(objNum, objGen);
                bytes = PdfEncodings.ConvertToBytes(value, null);
                bytes = decrypt.DecryptByteArray(bytes);
                value = PdfEncodings.ConvertToString(bytes, null);
            }
        }
Beispiel #5
0
 public String ToUnicodeString()
 {
     if (encoding != null && encoding.Length != 0)
     {
         return(value);
     }
     GetBytes();
     if (bytes.Length >= 2 && bytes[0] == (byte)254 && bytes[1] == (byte)255)
     {
         return(PdfEncodings.ConvertToString(bytes, PdfObject.TEXT_UNICODE));
     }
     else
     {
         return(PdfEncodings.ConvertToString(bytes, PdfObject.TEXT_PDFDOCENCODING));
     }
 }
Beispiel #6
0
        private static void IterateItems(PdfDictionary dic, Hashtable items)
        {
            PdfArray nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.NAMES));

            if (nn != null)
            {
                for (int k = 0; k < nn.Size; ++k)
                {
                    PdfString s = (PdfString)PdfReader.GetPdfObjectRelease(nn[k++]);
                    items[PdfEncodings.ConvertToString(s.GetBytes(), null)] = nn[k];
                }
            }
            else if ((nn = (PdfArray)PdfReader.GetPdfObjectRelease(dic.Get(PdfName.KIDS))) != null)
            {
                for (int k = 0; k < nn.Size; ++k)
                {
                    PdfDictionary kid = (PdfDictionary)PdfReader.GetPdfObjectRelease(nn[k]);
                    IterateItems(kid, items);
                }
            }
        }
Beispiel #7
0
        static GlyphList()
        {
            Stream istr = null;

            try {
                istr = BaseFont.GetResourceStream(BaseFont.RESOURCE_PATH + "glyphlist.txt");
                if (istr == null)
                {
                    String msg = "glyphlist.txt not found as resource.";
                    throw new Exception(msg);
                }
                byte[]       buf  = new byte[1024];
                MemoryStream outp = new MemoryStream();
                while (true)
                {
                    int size = istr.Read(buf, 0, buf.Length);
                    if (size == 0)
                    {
                        break;
                    }
                    outp.Write(buf, 0, size);
                }
                istr.Close();
                istr = null;
                String          s  = PdfEncodings.ConvertToString(outp.ToArray(), null);
                StringTokenizer tk = new StringTokenizer(s, "\r\n");
                while (tk.HasMoreTokens())
                {
                    String line = tk.NextToken();
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }
                    StringTokenizer t2   = new StringTokenizer(line, " ;\r\n\t\f");
                    String          name = null;
                    String          hex  = null;
                    if (!t2.HasMoreTokens())
                    {
                        continue;
                    }
                    name = t2.NextToken();
                    if (!t2.HasMoreTokens())
                    {
                        continue;
                    }
                    hex = t2.NextToken();
                    int num = int.Parse(hex, NumberStyles.HexNumber);
                    unicode2names[num]  = name;
                    names2unicode[name] = new int[] { num };
                }
            }
            catch (Exception e) {
                Console.Error.WriteLine("glyphlist.txt loading error: " + e.Message);
            }
            finally {
                if (istr != null)
                {
                    try {
                        istr.Close();
                    }
                    catch {
                        // empty on purpose
                    }
                }
            }
        }
Beispiel #8
0
        private static ArrayList BookmarkDepth(PdfReader reader, PdfDictionary outline, IntHashtable pages)
        {
            ArrayList list = new ArrayList();

            while (outline != null)
            {
                Hashtable map   = new Hashtable();
                PdfString title = (PdfString)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.TITLE));
                map["Title"] = title.ToUnicodeString();
                PdfArray color = (PdfArray)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.C));
                if (color != null && color.Size == 3)
                {
                    ByteBuffer outp = new ByteBuffer();
                    outp.Append(color.GetAsNumber(0).FloatValue).Append(' ');
                    outp.Append(color.GetAsNumber(1).FloatValue).Append(' ');
                    outp.Append(color.GetAsNumber(2).FloatValue);
                    map["Color"] = PdfEncodings.ConvertToString(outp.ToByteArray(), null);
                }
                PdfNumber style = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.F));
                if (style != null)
                {
                    int    f = style.IntValue;
                    String s = "";
                    if ((f & 1) != 0)
                    {
                        s += "italic ";
                    }
                    if ((f & 2) != 0)
                    {
                        s += "bold ";
                    }
                    s = s.Trim();
                    if (s.Length != 0)
                    {
                        map["Style"] = s;
                    }
                }
                PdfNumber count = (PdfNumber)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.COUNT));
                if (count != null && count.IntValue < 0)
                {
                    map["Open"] = "false";
                }
                try {
                    PdfObject dest = PdfReader.GetPdfObjectRelease(outline.Get(PdfName.DEST));
                    if (dest != null)
                    {
                        MapGotoBookmark(map, dest, pages); //changed by ujihara 2004-06-13
                    }
                    else
                    {
                        PdfDictionary action = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.A));
                        if (action != null)
                        {
                            if (PdfName.GOTO.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    MapGotoBookmark(map, dest, pages);
                                }
                            }
                            else if (PdfName.URI.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "URI";
                                map["URI"]    = ((PdfString)PdfReader.GetPdfObjectRelease(action.Get(PdfName.URI))).ToUnicodeString();
                            }
                            else if (PdfName.GOTOR.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                dest = PdfReader.GetPdfObjectRelease(action.Get(PdfName.D));
                                if (dest != null)
                                {
                                    if (dest.IsString())
                                    {
                                        map["Named"] = dest.ToString();
                                    }
                                    else if (dest.IsName())
                                    {
                                        map["NamedN"] = PdfName.DecodeName(dest.ToString());
                                    }
                                    else if (dest.IsArray())
                                    {
                                        PdfArray      arr = (PdfArray)dest;
                                        StringBuilder s   = new StringBuilder();
                                        s.Append(arr[0].ToString());
                                        s.Append(' ').Append(arr[1].ToString());
                                        for (int k = 2; k < arr.Size; ++k)
                                        {
                                            s.Append(' ').Append(arr[k].ToString());
                                        }
                                        map["Page"] = s.ToString();
                                    }
                                }
                                map["Action"] = "GoToR";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObject(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                                PdfObject newWindow = PdfReader.GetPdfObjectRelease(action.Get(PdfName.NEWWINDOW));
                                if (newWindow != null)
                                {
                                    map["NewWindow"] = newWindow.ToString();
                                }
                            }
                            else if (PdfName.LAUNCH.Equals(PdfReader.GetPdfObjectRelease(action.Get(PdfName.S))))
                            {
                                map["Action"] = "Launch";
                                PdfObject file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.F));
                                if (file == null)
                                {
                                    file = PdfReader.GetPdfObjectRelease(action.Get(PdfName.WIN));
                                }
                                if (file != null)
                                {
                                    if (file.IsString())
                                    {
                                        map["File"] = ((PdfString)file).ToUnicodeString();
                                    }
                                    else if (file.IsDictionary())
                                    {
                                        file = PdfReader.GetPdfObjectRelease(((PdfDictionary)file).Get(PdfName.F));
                                        if (file.IsString())
                                        {
                                            map["File"] = ((PdfString)file).ToUnicodeString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch  {
                    //empty on purpose
                }
                PdfDictionary first = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.FIRST));
                if (first != null)
                {
                    map["Kids"] = BookmarkDepth(reader, first, pages);
                }
                list.Add(map);
                outline = (PdfDictionary)PdfReader.GetPdfObjectRelease(outline.Get(PdfName.NEXT));
            }
            return(list);
        }
Beispiel #9
0
        /**
         * Constructs a <CODE>PdfString</CODE>-object.
         *
         * @param        bytes    an array of <CODE>byte</CODE>
         */

        public PdfString(byte[] bytes) : base(STRING)
        {
            value    = PdfEncodings.ConvertToString(bytes, null);
            encoding = NOTHING;
        }
Beispiel #10
0
        /**
         * Outputs a <CODE>double</CODE> into a format suitable for the PDF.
         * @param d a double
         * @param buf a ByteBuffer
         * @return the <CODE>String</CODE> representation of the <CODE>double</CODE> if
         * <CODE>buf</CODE> is <CODE>null</CODE>. If <CODE>buf</CODE> is <B>not</B> <CODE>null</CODE>,
         * then the double is appended directly to the buffer and this methods returns <CODE>null</CODE>.
         */
        public static string FormatDouble(double d, ByteBuffer buf)
        {
            if (HIGH_PRECISION)
            {
                String sform = d.ToString("0.######", CultureInfo.InvariantCulture);
                if (buf == null)
                {
                    return(sform);
                }
                else
                {
                    buf.Append(sform);
                    return(null);
                }
            }
            bool negative = false;

            if (Math.Abs(d) < 0.000015)
            {
                if (buf != null)
                {
                    buf.Append(ZERO);
                    return(null);
                }
                else
                {
                    return("0");
                }
            }
            if (d < 0)
            {
                negative = true;
                d        = -d;
            }
            if (d < 1.0)
            {
                d += 0.000005;
                if (d >= 1)
                {
                    if (negative)
                    {
                        if (buf != null)
                        {
                            buf.Append((byte)'-');
                            buf.Append((byte)'1');
                            return(null);
                        }
                        else
                        {
                            return("-1");
                        }
                    }
                    else
                    {
                        if (buf != null)
                        {
                            buf.Append((byte)'1');
                            return(null);
                        }
                        else
                        {
                            return("1");
                        }
                    }
                }
                if (buf != null)
                {
                    int v = (int)(d * 100000);

                    if (negative)
                    {
                        buf.Append((byte)'-');
                    }
                    buf.Append((byte)'0');
                    buf.Append((byte)'.');

                    buf.Append((byte)(v / 10000 + ZERO));
                    if (v % 10000 != 0)
                    {
                        buf.Append((byte)((v / 1000) % 10 + ZERO));
                        if (v % 1000 != 0)
                        {
                            buf.Append((byte)((v / 100) % 10 + ZERO));
                            if (v % 100 != 0)
                            {
                                buf.Append((byte)((v / 10) % 10 + ZERO));
                                if (v % 10 != 0)
                                {
                                    buf.Append((byte)((v) % 10 + ZERO));
                                }
                            }
                        }
                    }
                    return(null);
                }
                else
                {
                    int x = 100000;
                    int v = (int)(d * x);

                    StringBuilder res = new StringBuilder();
                    if (negative)
                    {
                        res.Append('-');
                    }
                    res.Append("0.");

                    while (v < x / 10)
                    {
                        res.Append('0');
                        x /= 10;
                    }
                    res.Append(v);
                    int cut = res.Length - 1;
                    while (res[cut] == '0')
                    {
                        --cut;
                    }
                    res.Length = cut + 1;
                    return(res.ToString());
                }
            }
            else if (d <= 32767)
            {
                d += 0.005;
                int v = (int)(d * 100);

                if (v < byteCacheSize && byteCache[v] != null)
                {
                    if (buf != null)
                    {
                        if (negative)
                        {
                            buf.Append((byte)'-');
                        }
                        buf.Append(byteCache[v]);
                        return(null);
                    }
                    else
                    {
                        string tmp = PdfEncodings.ConvertToString(byteCache[v], null);
                        if (negative)
                        {
                            tmp = "-" + tmp;
                        }
                        return(tmp);
                    }
                }
                if (buf != null)
                {
                    if (v < byteCacheSize)
                    {
                        //create the cachebyte[]
                        byte[] cache;
                        int    size = 0;
                        if (v >= 1000000)
                        {
                            //the original number is >=10000, we need 5 more bytes
                            size += 5;
                        }
                        else if (v >= 100000)
                        {
                            //the original number is >=1000, we need 4 more bytes
                            size += 4;
                        }
                        else if (v >= 10000)
                        {
                            //the original number is >=100, we need 3 more bytes
                            size += 3;
                        }
                        else if (v >= 1000)
                        {
                            //the original number is >=10, we need 2 more bytes
                            size += 2;
                        }
                        else if (v >= 100)
                        {
                            //the original number is >=1, we need 1 more bytes
                            size += 1;
                        }

                        //now we must check if we have a decimal number
                        if (v % 100 != 0)
                        {
                            //yes, do not forget the "."
                            size += 2;
                        }
                        if (v % 10 != 0)
                        {
                            size++;
                        }
                        cache = new byte[size];
                        int add = 0;
                        if (v >= 1000000)
                        {
                            cache[add++] = bytes[(v / 1000000)];
                        }
                        if (v >= 100000)
                        {
                            cache[add++] = bytes[(v / 100000) % 10];
                        }
                        if (v >= 10000)
                        {
                            cache[add++] = bytes[(v / 10000) % 10];
                        }
                        if (v >= 1000)
                        {
                            cache[add++] = bytes[(v / 1000) % 10];
                        }
                        if (v >= 100)
                        {
                            cache[add++] = bytes[(v / 100) % 10];
                        }

                        if (v % 100 != 0)
                        {
                            cache[add++] = (byte)'.';
                            cache[add++] = bytes[(v / 10) % 10];
                            if (v % 10 != 0)
                            {
                                cache[add++] = bytes[v % 10];
                            }
                        }
                        byteCache[v] = cache;
                    }

                    if (negative)
                    {
                        buf.Append((byte)'-');
                    }
                    if (v >= 1000000)
                    {
                        buf.Append(bytes[(v / 1000000)]);
                    }
                    if (v >= 100000)
                    {
                        buf.Append(bytes[(v / 100000) % 10]);
                    }
                    if (v >= 10000)
                    {
                        buf.Append(bytes[(v / 10000) % 10]);
                    }
                    if (v >= 1000)
                    {
                        buf.Append(bytes[(v / 1000) % 10]);
                    }
                    if (v >= 100)
                    {
                        buf.Append(bytes[(v / 100) % 10]);
                    }

                    if (v % 100 != 0)
                    {
                        buf.Append((byte)'.');
                        buf.Append(bytes[(v / 10) % 10]);
                        if (v % 10 != 0)
                        {
                            buf.Append(bytes[v % 10]);
                        }
                    }
                    return(null);
                }
                else
                {
                    StringBuilder res = new StringBuilder();
                    if (negative)
                    {
                        res.Append('-');
                    }
                    if (v >= 1000000)
                    {
                        res.Append(chars[(v / 1000000)]);
                    }
                    if (v >= 100000)
                    {
                        res.Append(chars[(v / 100000) % 10]);
                    }
                    if (v >= 10000)
                    {
                        res.Append(chars[(v / 10000) % 10]);
                    }
                    if (v >= 1000)
                    {
                        res.Append(chars[(v / 1000) % 10]);
                    }
                    if (v >= 100)
                    {
                        res.Append(chars[(v / 100) % 10]);
                    }

                    if (v % 100 != 0)
                    {
                        res.Append('.');
                        res.Append(chars[(v / 10) % 10]);
                        if (v % 10 != 0)
                        {
                            res.Append(chars[v % 10]);
                        }
                    }
                    return(res.ToString());
                }
            }
            else
            {
                StringBuilder res = new StringBuilder();
                if (negative)
                {
                    res.Append('-');
                }
                d += 0.5;
                long v = (long)d;
                return(res.Append(v).ToString());
            }
        }