Exemple #1
0
        /** Gets the field value or <CODE>null</CODE> if the field does not
         * exist or has no value defined.
         * @param name the fully qualified field name
         * @return the field value or <CODE>null</CODE>
         */
        public String GetFieldValue(String name)
        {
            PdfDictionary field = (PdfDictionary)fields[name];

            if (field == null)
            {
                return(null);
            }
            PdfObject v = GetPdfObject(field.Get(PdfName.V));

            if (v == null)
            {
                return(null);
            }
            if (v.IsName())
            {
                return(PdfName.DecodeName(((PdfName)v).ToString()));
            }
            else if (v.IsString())
            {
                PdfString vs = (PdfString)v;
                if (encoding == null || vs.Encoding != null)
                {
                    return(vs.ToUnicodeString());
                }
                byte[] b = vs.GetBytes();
                if (b.Length >= 2 && b[0] == (byte)254 && b[1] == (byte)255)
                {
                    return(vs.ToUnicodeString());
                }
                try {
                    if (encoding.Equals(PdfName.SHIFT_JIS))
                    {
                        return(Encoding.GetEncoding(932).GetString(b));
                    }
                    else if (encoding.Equals(PdfName.UHC))
                    {
                        return(Encoding.GetEncoding(949).GetString(b));
                    }
                    else if (encoding.Equals(PdfName.GBK))
                    {
                        return(Encoding.GetEncoding(936).GetString(b));
                    }
                    else if (encoding.Equals(PdfName.BIGFIVE))
                    {
                        return(Encoding.GetEncoding(950).GetString(b));
                    }
                }
                catch  {
                }
                return(vs.ToUnicodeString());
            }
            return(null);
        }
Exemple #2
0
        private static String GetCOName(PdfReader reader, PRIndirectReference refi)
        {
            String name = "";

            while (refi != null)
            {
                PdfObject obj = PdfReader.GetPdfObject(refi);
                if (obj == null || obj.Type != PdfObject.DICTIONARY)
                {
                    break;
                }
                PdfDictionary dic = (PdfDictionary)obj;
                PdfString     t   = dic.GetAsString(PdfName.T);
                if (t != null)
                {
                    name = t.ToUnicodeString() + "." + name;
                }
                refi = (PRIndirectReference)dic.Get(PdfName.PARENT);
            }
            if (name.EndsWith("."))
            {
                name = name.Substring(0, name.Length - 1);
            }
            return(name);
        }
Exemple #3
0
        protected virtual void ReadFields()
        {
            catalog = trailer.GetAsDict(PdfName.ROOT);
            PdfDictionary fdf = catalog.GetAsDict(PdfName.FDF);

            if (fdf == null)
            {
                return;
            }
            PdfString fs = fdf.GetAsString(PdfName.F);

            if (fs != null)
            {
                fileSpec = fs.ToUnicodeString();
            }
            PdfArray fld = fdf.GetAsArray(PdfName.FIELDS);

            if (fld == null)
            {
                return;
            }
            encoding = fdf.GetAsName(PdfName.ENCODING);
            PdfDictionary merged = new PdfDictionary();

            merged.Put(PdfName.KIDS, fld);
            KidNode(merged, "");
        }
Exemple #4
0
        protected virtual void KidNode(PdfDictionary merged, String name)
        {
            PdfArray kids = merged.GetAsArray(PdfName.KIDS);

            if (kids == null || kids.Size == 0)
            {
                if (name.Length > 0)
                {
                    name = name.Substring(1);
                }
                fields[name] = merged;
            }
            else
            {
                merged.Remove(PdfName.KIDS);
                for (int k = 0; k < kids.Size; ++k)
                {
                    PdfDictionary dic = new PdfDictionary();
                    dic.Merge(merged);
                    PdfDictionary newDic  = kids.GetAsDict(k);
                    PdfString     t       = newDic.GetAsString(PdfName.T);
                    String        newName = name;
                    if (t != null)
                    {
                        newName += "." + t.ToUnicodeString();
                    }
                    dic.Merge(newDic);
                    dic.Remove(PdfName.T);
                    KidNode(dic, newName);
                }
            }
        }
Exemple #5
0
 private String DecodeString(PdfString ps)
 {
     if (ps.IsHexWriting())
     {
         return(PdfEncodings.ConvertToString(ps.GetBytes(), "UnicodeBigUnmarked"));
     }
     else
     {
         return(ps.ToUnicodeString());
     }
 }
Exemple #6
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);
        }