/**
         * If the child of a structured element is a dictionary, we inspect the
         * child; we may also draw a tag.
         *
         * @param k
         *            the child dictionary to inspect
         */
        public void InspectChildDictionary(PdfDictionary k)
        {
            if (k == null)
            {
                return;
            }
            PdfName s = k.GetAsName(PdfName.S);

            if (s != null)
            {
                String tagN = PdfName.DecodeName(s.ToString());
                String tag  = FixTagName(tagN);
                outp.Write("<");
                outp.Write(tag);
                outp.Write(">");
                PdfDictionary dict = k.GetAsDict(PdfName.PG);
                if (dict != null)
                {
                    ParseTag(tagN, k.GetDirectObject(PdfName.K), dict);
                }
                InspectChild(k.Get(PdfName.K));
                outp.Write("</");
                outp.Write(tag);
                outp.WriteLine(">");
            }
            else
            {
                InspectChild(k.Get(PdfName.K));
            }
        }
Esempio n. 2
0
        /**
         * If the child of a structured element is a dictionary, we inspect the
         * child; we may also draw a tag.
         *
         * @param k
         *            the child dictionary to inspect
         */
        virtual public void InspectChildDictionary(PdfDictionary k, bool inspectAttributes)
        {
            if (k == null)
            {
                return;
            }
            PdfName s = k.GetAsName(PdfName.S);

            if (s != null)
            {
                String tagN = PdfName.DecodeName(s.ToString());
                String tag  = FixTagName(tagN);
                outp.Write("<");
                outp.Write(tag);
                if (inspectAttributes)
                {
                    PdfDictionary a = k.GetAsDict(PdfName.A);
                    if (a != null)
                    {
                        Dictionary <PdfName, PdfObject> .KeyCollection keys = a.Keys;
                        foreach (PdfName key in keys)
                        {
                            outp.Write(' ');
                            PdfObject value = a.Get(key);
                            value = PdfReader.GetPdfObject(value);
                            outp.Write(XmlName(key));
                            outp.Write("=\"");
                            outp.Write(value.ToString());
                            outp.Write("\"");
                        }
                    }
                }
                outp.Write(">");
                PdfObject alt = k.Get(PdfName.ALT);
                if (alt != null && alt.ToString() != null)
                {
                    outp.Write("<alt><![CDATA[");
                    outp.Write(Regex.Replace(alt.ToString(), "[\\000]*", ""));
                    outp.Write("]]></alt>");
                }
                PdfDictionary dict = k.GetAsDict(PdfName.PG);
                if (dict != null)
                {
                    ParseTag(tagN, k.GetDirectObject(PdfName.K), dict);
                }
                InspectChild(k.GetDirectObject(PdfName.K));
                outp.Write("</");
                outp.Write(tag);
                outp.WriteLine(">");
            }
            else
            {
                InspectChild(k.GetDirectObject(PdfName.K));
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        private int GetXObjNum(PdfName xobjName)
        {
            String decodedPdfName = PdfName.DecodeName(xobjName.ToString());

            if (decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) == -1)
            {
                return(0);
            }

            String numStr = decodedPdfName.Substring(decodedPdfName.LastIndexOf(XOBJ_NAME_PREFIX) + XOBJ_NAME_PREFIX.Length);

            return(Int32.Parse(numStr));
        }
Esempio n. 5
0
        /** Gets the field value.
         * @param field the field name
         * @return the field value or <CODE>null</CODE> if not found
         */
        public String GetField(String field)
        {
            Hashtable       map = fields;
            StringTokenizer tk  = new StringTokenizer(field, ".");

            if (!tk.HasMoreTokens())
            {
                return(null);
            }
            while (true)
            {
                String s   = tk.NextToken();
                Object obj = map[s];
                if (obj == null)
                {
                    return(null);
                }
                if (tk.HasMoreTokens())
                {
                    if (obj is Hashtable)
                    {
                        map = (Hashtable)obj;
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    if (obj is Hashtable)
                    {
                        return(null);
                    }
                    else
                    {
                        if (((PdfObject)obj).IsString())
                        {
                            return(((PdfString)obj).ToUnicodeString());
                        }
                        else
                        {
                            return(PdfName.DecodeName(obj.ToString()));
                        }
                    }
                }
            }
        }
Esempio n. 6
0
 private static void MapGotoBookmark(Hashtable map, PdfObject dest, IntHashtable pages)
 {
     if (dest.IsString())
     {
         map["Named"] = dest.ToString();
     }
     else if (dest.IsName())
     {
         map["Named"] = PdfName.DecodeName(dest.ToString());
     }
     else if (dest.IsArray())
     {
         map["Page"] = MakeBookmarkParam((PdfArray)dest, pages); //changed by ujihara 2004-06-13
     }
     map["Action"] = "GoTo";
 }
Esempio n. 7
0
        /** Creates a new instance of DocumentFont */
        internal DocumentFont(PRIndirectReference refFont)
        {
            encoding     = "";
            fontSpecific = false;
            this.refFont = refFont;
            fontType     = FONT_TYPE_DOCUMENT;
            font         = (PdfDictionary)PdfReader.GetPdfObject(refFont);
            fontName     = PdfName.DecodeName(font.GetAsName(PdfName.BASEFONT).ToString());
            PdfName subType = font.GetAsName(PdfName.SUBTYPE);

            if (PdfName.TYPE1.Equals(subType) || PdfName.TRUETYPE.Equals(subType))
            {
                DoType1TT();
            }
            else
            {
                for (int k = 0; k < cjkNames.Length; ++k)
                {
                    if (fontName.StartsWith(cjkNames[k]))
                    {
                        fontName  = cjkNames[k];
                        cjkMirror = BaseFont.CreateFont(fontName, cjkEncs[k], false);
                        return;
                    }
                }
                String enc = PdfName.DecodeName(font.GetAsName(PdfName.ENCODING).ToString());
                for (int k = 0; k < cjkEncs2.Length; ++k)
                {
                    if (enc.StartsWith(cjkEncs2[k]))
                    {
                        if (k > 3)
                        {
                            k -= 4;
                        }
                        cjkMirror = BaseFont.CreateFont(cjkNames2[k], cjkEncs2[k], false);
                        return;
                    }
                }
                if (PdfName.TYPE0.Equals(subType) && enc.Equals("Identity-H"))
                {
                    ProcessType0(font);
                    isType0 = true;
                }
            }
        }
Esempio n. 8
0
        private void DoType1TT()
        {
            PdfObject enc = PdfReader.GetPdfObject(font.Get(PdfName.ENCODING));

            if (enc == null)
            {
                FillEncoding(null);
            }
            else
            {
                if (enc.IsName())
                {
                    FillEncoding((PdfName)enc);
                }
                else
                {
                    PdfDictionary encDic = (PdfDictionary)enc;
                    enc = PdfReader.GetPdfObject(encDic.Get(PdfName.BASEENCODING));
                    if (enc == null)
                    {
                        FillEncoding(null);
                    }
                    else
                    {
                        FillEncoding((PdfName)enc);
                    }
                    PdfArray diffs = encDic.GetAsArray(PdfName.DIFFERENCES);
                    if (diffs != null)
                    {
                        diffmap = new IntHashtable();
                        int currentNumber = 0;
                        for (int k = 0; k < diffs.Size; ++k)
                        {
                            PdfObject obj = diffs[k];
                            if (obj.IsNumber())
                            {
                                currentNumber = ((PdfNumber)obj).IntValue;
                            }
                            else
                            {
                                int[] c = GlyphList.NameToUnicode(PdfName.DecodeName(((PdfName)obj).ToString()));
                                if (c != null && c.Length > 0)
                                {
                                    uni2byte[c[0]] = currentNumber;
                                    diffmap[c[0]]  = currentNumber;
                                }
                                ++currentNumber;
                            }
                        }
                    }
                }
            }
            PdfArray  newWidths = font.GetAsArray(PdfName.WIDTHS);
            PdfNumber first     = font.GetAsNumber(PdfName.FIRSTCHAR);
            PdfNumber last      = font.GetAsNumber(PdfName.LASTCHAR);

            if (BuiltinFonts14.ContainsKey(fontName))
            {
                BaseFont bf;
                bf = BaseFont.CreateFont(fontName, WINANSI, false);
                int[] e = uni2byte.ToOrderedKeys();
                for (int k = 0; k < e.Length; ++k)
                {
                    int n = uni2byte[e[k]];
                    widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                }
                if (diffmap != null)   //widths for differences must override existing ones
                {
                    e = diffmap.ToOrderedKeys();
                    for (int k = 0; k < e.Length; ++k)
                    {
                        int n = diffmap[e[k]];
                        widths[n] = bf.GetRawWidth(n, GlyphList.UnicodeToName(e[k]));
                    }
                    diffmap = null;
                }
                Ascender    = bf.GetFontDescriptor(ASCENT, 1000);
                CapHeight   = bf.GetFontDescriptor(CAPHEIGHT, 1000);
                Descender   = bf.GetFontDescriptor(DESCENT, 1000);
                ItalicAngle = bf.GetFontDescriptor(ITALICANGLE, 1000);
                llx         = bf.GetFontDescriptor(BBOXLLX, 1000);
                lly         = bf.GetFontDescriptor(BBOXLLY, 1000);
                urx         = bf.GetFontDescriptor(BBOXURX, 1000);
                ury         = bf.GetFontDescriptor(BBOXURY, 1000);
            }
            if (first != null && last != null && newWidths != null)
            {
                int f = first.IntValue;
                for (int k = 0; k < newWidths.Size; ++k)
                {
                    widths[f + k] = newWidths.GetAsNumber(k).IntValue;
                }
            }
            FillFontDesc(font.GetAsDict(PdfName.FONTDESCRIPTOR));
        }
Esempio n. 9
0
        private static void ParseCid(String cmapName, AbstractCMap cmap, ICidLocation location, int level)
        {
            if (level >= MAXLEVEL)
            {
                return;
            }
            PRTokeniser inp = location.GetLocation(cmapName);

            try {
                List <PdfObject> list = new List <PdfObject>();
                PdfContentParser cp   = new PdfContentParser(inp);
                int maxExc            = 50;
                while (true)
                {
                    try {
                        cp.Parse(list);
                    }
                    catch {
                        if (--maxExc < 0)
                        {
                            break;
                        }
                        continue;
                    }
                    if (list.Count == 0)
                    {
                        break;
                    }
                    String last = list[list.Count - 1].ToString();
                    if (level == 0 && list.Count == 3 && last.Equals(DEF))
                    {
                        PdfObject key = list[0];
                        if (PdfName.REGISTRY.Equals(key))
                        {
                            cmap.Registry = list[1].ToString();
                        }
                        else if (PdfName.ORDERING.Equals(key))
                        {
                            cmap.Ordering = list[1].ToString();
                        }
                        else if (CMAPNAME.Equals(key))
                        {
                            cmap.Name = list[1].ToString();
                        }
                        else if (PdfName.SUPPLEMENT.Equals(key))
                        {
                            try {
                                cmap.Supplement = ((PdfNumber)list[1]).IntValue;
                            }
                            catch {}
                        }
                    }
                    else if ((last.Equals(ENDCIDCHAR) || last.Equals(ENDBFCHAR)) && list.Count >= 3)
                    {
                        int lmax = list.Count - 2;
                        for (int k = 0; k < lmax; k += 2)
                        {
                            if (list[k] is PdfString)
                            {
                                cmap.AddChar((PdfString)list[k], list[k + 1]);
                            }
                        }
                    }
                    else if ((last.Equals(ENDCIDRANGE) || last.Equals(ENDBFRANGE)) && list.Count >= 4)
                    {
                        int lmax = list.Count - 3;
                        for (int k = 0; k < lmax; k += 3)
                        {
                            if (list[k] is PdfString && list[k + 1] is PdfString)
                            {
                                cmap.AddRange((PdfString)list[k], (PdfString)list[k + 1], list[k + 2]);
                            }
                        }
                    }
                    else if (last.Equals(USECMAP) && list.Count == 2 && list[0] is PdfName)
                    {
                        ParseCid(PdfName.DecodeName(list[0].ToString()), cmap, location, level + 1);
                    }
                }
            }
            finally {
                inp.Close();
            }
        }
Esempio n. 10
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);
        }