示例#1
0
        protected virtual void WriteProperty(TextWriter writer, object value, string key)
        {
            if (value is string[])
            {
                writer.WriteLine("/{0} [/{1}]", key, string.Join(" /", ((string[])value)));
                return;
            }
            if (value is IPDFDictionary)
            {
                IPDFDictionary d = value as IPDFDictionary;
                if (d.HasValues)
                {
                    writer.Write("/{0} ", key);
                    d.Write(writer);
                }
                return;
            }
            if (value is IPDFInlineObject)
            {
                writer.Write("/{0} ", key);
                PDFObject pdfobj = value as PDFObject;
                pdfobj.WriteHeader(writer);
                return;
            }
            if (value is PDFObject)
            {
                writer.WriteLine("/{0} {1}", key, ((PDFObject)value).Ref);
                return;
            }
            if (value is DateTime)
            {
                writer.WriteLine("/{0} (D:{1:yyyyMMddHHmmss})", key, value);
                return;
            }
            if (value.GetType().IsEnum)
            {
                Type t = value.GetType();
                if (t.GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0)
                {
                    int val = (int)value;
                    writer.WriteLine("/{0} {1}", key, val);
                    return;
                }
                writer.WriteLine("/{0} /{1}", key, value.ToString());
                return;
            }
            if (value is string)
            {
                writer.WriteLine("/{0} /{1}", key, value);
                return;
            }

            if (value is Uri)
            {
                writer.WriteLine("/{0} ({1})", key, value);
                return;
            }

            writer.WriteLine("/{0} {1}", key, value);
        }
示例#2
0
        protected static PDFDocument ParseDocument(ByteStreamReader reader)
        {
            PDFDocument doc = new PDFDocument();

            doc.Load(reader);

            foreach (IPDFDictionary page in doc.Pages)
            {
                IPDFElement content;
                if (page.Dict.TryGet <IPDFElement>("Contents", out content))
                {
                    IPDFList   clist    = content as IPDFList;
                    IPDFStream cstream  = content as IPDFStream;
                    PDFContent pcontent = null;

                    if (clist != null && clist.List != null)
                    {
                        List <byte> data = new List <byte>();
                        foreach (IPDFStream elem in clist.List.OfType <IPDFStream>())
                        {
                            if (elem.Stream != null)
                            {
                                data.AddRange(elem.Stream.Data);
                            }
                        }
                        pcontent = new PDFContent(data.ToArray(), page);
                    }
                    else if (cstream != null && cstream.Stream != null)
                    {
                        pcontent = new PDFContent(cstream.Stream.Data, page);
                    }

                    if (pcontent != null)
                    {
                        page.Dict["PageContent"] = pcontent;
                        Dictionary <long, PDFContentBlock> blocks = new Dictionary <long, PDFContentBlock>();
                        doc.ContentBlocks[((IPDFObjRef)page).ObjRef] = blocks;
                        doc.ProcessPageContentBlocks(pcontent, blocks);
                    }
                }
            }

            IPDFDictionary stree = doc.StructTreeRoot;

            if (stree != null)
            {
                doc.StructTree = doc.ProcessTreeNode(stree, (PDFName)stree.Dict["Type"]);
            }

            return(doc);
        }
示例#3
0
        public PDFContent(byte[] data, IPDFDictionary page)
        {
            this.Data    = data;
            this.Options = page.Dict;
            ByteStreamReader reader    = new ByteStreamReader(Data);
            PDFTokenizer     tokenizer = new PDFTokenizer(reader);

            PDFContentTokenStack stack = new PDFContentTokenStack();

            foreach (IPDFToken token in tokenizer)
            {
                stack.ProcessToken(token);
            }

            this.Tokens = stack.Reverse().ToList();
        }
示例#4
0
        protected static IEnumerable <IPDFDictionary> GetPages(IPDFDictionary root)
        {
            PDFName  type;
            IPDFList kids;

            if (root.Dict.TryGet("Type", out type) && type.Name == "Pages" && root.Dict.TryGet("Kids", out kids))
            {
                foreach (IPDFDictionary node in kids.List.OfType <IPDFDictionary>())
                {
                    foreach (IPDFDictionary leaf in GetPages(node))
                    {
                        yield return(leaf);
                    }
                }
            }
            else
            {
                yield return(root);
            }
        }
示例#5
0
        protected PDFContentBlock ProcessTreeNode(IPDFDictionary node, PDFName type)
        {
            PDFContentBlock cb = new PDFContentBlock
            {
                StartMarker = new PDFContentOperator
                {
                    Name      = "BDC",
                    Arguments = new List <IPDFToken>
                    {
                        type,
                        node
                    }
                },
                Content = new List <PDFContentOperator>()
            };

            if (node.Dict.ContainsKey("K"))
            {
                IPDFElement K = node.Dict["K"];
                if (K is IPDFList && ((IPDFList)K).List != null)
                {
                    foreach (IPDFElement elem in ((IPDFList)K).List)
                    {
                        if (elem is IPDFDictionary)
                        {
                            IPDFDictionary v = (IPDFDictionary)elem;
                            PDFName        vtype;
                            if (v.Dict.TryGet("S", out vtype))
                            {
                                PDFContentBlock blk = ProcessTreeNode(v, vtype);
                                blk.Parent = cb;
                                cb.Content.Add(blk);
                            }
                        }
                        else if (elem is PDFInteger)
                        {
                            long       mcid = ((PDFInteger)elem).Value;
                            IPDFObjRef objref;

                            if (node.Dict.TryGet("Pg", out objref))
                            {
                                if (ContentBlocks.ContainsKey(objref.ObjRef))
                                {
                                    Dictionary <long, PDFContentBlock> blocksByMcid = ContentBlocks[objref.ObjRef];
                                    if (blocksByMcid.ContainsKey(mcid))
                                    {
                                        PDFContentBlock blk = blocksByMcid[mcid];
                                        blk.Parent = cb;
                                        cb.Content.Add(blk);
                                    }
                                }
                            }
                        }
                    }
                }
                else if (K is IPDFDictionary && ((IPDFDictionary)K).Dict != null)
                {
                    PDFName vtype;
                    if (((IPDFDictionary)K).Dict.TryGet("S", out vtype))
                    {
                        PDFContentBlock blk = ProcessTreeNode((IPDFDictionary)K, vtype);
                        blk.Parent = cb;
                        cb.Content.Add(blk);
                    }
                    else
                    {
                        System.Diagnostics.Debugger.Break();
                    }
                }
                else if (K is PDFInteger && node.Dict.ContainsKey("Pg"))
                {
                    long       mcid = ((PDFInteger)K).Value;
                    IPDFObjRef objref;

                    if (node.Dict.TryGet("Pg", out objref))
                    {
                        if (ContentBlocks.ContainsKey(objref.ObjRef))
                        {
                            Dictionary <long, PDFContentBlock> blocksByMcid = ContentBlocks[objref.ObjRef];
                            if (blocksByMcid.ContainsKey(mcid))
                            {
                                PDFContentBlock blk = blocksByMcid[mcid];
                                blk.Parent = cb;
                                cb.Content.Add(blk);
                            }
                        }
                    }
                }
            }

            return(cb);
        }