Beispiel #1
0
        private ObjectCollection TraversePagesTree(Object top)
        {
            ObjectCollection retval = new ObjectCollection();

            if (top.Type == "Page")
            {
                Utility.TraceLine("Found a page: " + top.ToString());
                retval.Add(top);
                return(retval);
            }

            if (top.Type == "Pages")
            {
                DictionaryItem kids = top.Dictionary.Get("Kids");
                foreach (Object kid in kids.ValueAsObjects(m_objects))
                {
                    retval.Add(TraversePagesTree(kid));
                }
                return(retval);
            }

            if (top.Type == "")
            {
                foreach (Object kid in top.DirectValueAsObjects(m_objects))
                {
                    retval.Add(TraversePagesTree(kid));
                }

                return(retval);
            }

            throw new RuntimeException("Unknown object type \"" + top.Type + "\" in object " + top.Number +
                                       " " + top.Generation + " pages tree");
        }
Beispiel #2
0
        public Content(Pdf Document, Object PageObject)
        {
            if (PageObject.Number == -1)
            {
                throw new RuntimeException("Page object is invalid");
            }

            DictionaryItem di = PageObject.Dictionary.Get("Contents");

            if (!di.Valid)
            {
                throw new RuntimeException("Page object does not refer to a content object");
            }

            m_objects = di.ValueAsObjects(Document.Objects);
            if (m_objects.Count == 0)
            {
                throw new RuntimeException("Page object " + PageObject.Number + " " + PageObject.Generation +
                                           " does not refer to a content object which is valid");
            }
        }
Beispiel #3
0
        private Object GetTrailerObject(string name)
        {
            if (m_trailer.Count == 0)
            {
                throw new RuntimeException("Empty trailer");
            }

            DictionaryItem rootValue = m_trailer.Get(name);

            if (!rootValue.Valid)
            {
                return(new Object());
            }

            if (rootValue.Type != DictionaryItem.ValueType.Objects)
            {
                throw new RuntimeException(name + " entry in trailer does not point to an object: " + rootValue.ValueAsString());
            }

            return(rootValue.ValueAsObject(m_objects));
        }
Beispiel #4
0
        public ObjectCollection DirectValueAsObjects(ObjectCollection objs)
        {
            DictionaryItem di = new DictionaryItem("Direct", DirectValue);

            return(di.ValueAsObjects(objs));
        }
Beispiel #5
0
        public void ReadDictionary(PdfStream st)
        {
            if (st.Expect("<<", false))
            {
                // Until we get to the end of this dictionary
                while (st.PeekBlock(2) != ">>")
                {
                    // Consume any leading whitespace
                    while (Utility.IsWhite(st.PeekBlock(1)))
                    {
                        st.ReadBlock(1);
                    }

                    // Name
                    Utility.TraceLine("Checking for a name");
                    string name = st.RegexMatch("^(/[^ \t/\\[\\]\\(\\)\\<\\>]+)[ \t]*");
                    if (name == "")
                    {
                        throw new ParseException("Dictionary items must have a name");
                    }

                    st.ConsumeWhitespace();

                    // Value
                    if (st.PeekBlock(2) == "<<")
                    {
                        Utility.TraceLine("Traversing subdictionary");
                        Dictionary dict = new Dictionary();
                        dict.ReadDictionary(st);
                        DictionaryItem di = new DictionaryItem(name, dict);
                        Add(di);
                    }
                    else
                    {
                        Utility.TraceLine("Finding the value");
                        string nameval   = st.RegexMatch("^(/[^ \t/\\[\\]\\(\\)\\<\\>]+)", false);
                        string objrefval = st.RegexMatch("^([0-9]+ [0-9]+ R)", false);
                        string numval    = st.RegexMatch("^(-{0,1}[0-9]+)", false);
                        string floatval  = st.RegexMatch("^(-{0,1}[0-9]+\\.[0-9]+)", false);

                        // The old version of these:
                        //string rdbrackets = st.RegexMatch("^(\\([^\\)]*\\)+)[ \t]*", false);

                        string sqbrackets = st.RegexMatch(@"^(\[.*?[^\\]+?\])", false);
                        string rdbrackets = st.RegexMatch(@"^(\(.*?[^\\]+?\))", false);
                        string anbrackets = st.RegexMatch(@"^(\<.*?[^\\]+?\>)", false);
                        string singleword = st.RegexMatch("^([^ \t]+)", false);

                        if (nameval != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, nameval);
                            Add(di);
                            st.ReadBlock(nameval.Length);
                        }
                        else if (objrefval != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, objrefval);
                            Add(di);
                            st.ReadBlock(objrefval.Length);
                        }
                        else if (floatval != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, floatval);
                            Add(di);
                            st.ReadBlock(floatval.Length);
                        }
                        else if (numval != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, numval);
                            Add(di);
                            st.ReadBlock(numval.Length);
                        }
                        else if (sqbrackets != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, sqbrackets);
                            Add(di);
                            st.ReadBlock(sqbrackets.Length);
                        }
                        else if (rdbrackets != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, rdbrackets);
                            Add(di);
                            st.ReadBlock(rdbrackets.Length);
                        }
                        else if (anbrackets != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, anbrackets);
                            Add(di);
                            st.ReadBlock(anbrackets.Length);
                        }
                        else if (singleword != "")
                        {
                            DictionaryItem di = new DictionaryItem(name, singleword);
                            Add(di);
                            st.ReadBlock(singleword.Length);
                        }
                        else
                        {
                            throw new ParseException("Unknown value format: " + st.PeekLine());
                        }
                    }

                    st.ConsumeWhitespace();
                }

                // Skip over the >> at the end of the dictionary
                st.ReadBlock(2);
                st.ConsumeWhitespace();
            }
        }
Beispiel #6
0
 public void Add(DictionaryItem di)
 {
     Utility.TraceLine("Adding " + di.ToString());
     m_hash[di.Name] = di;
 }
Beispiel #7
0
        public Pdf(string filename)
        {
            PdfStream st = new PdfStream();

            st.FillFromFile(filename);

            long pos = st.Position;

            ReadHeader(st);

            // While we're still advancing through the stream, then all is good...
            bool eof = false;

            while (!eof && (st.Position != pos))
            {
                Utility.TraceLine("Starting read pass");
                pos = st.Position;
                ReadComment(st);

                Object obj = ReadObject(st);
                if (obj.Valid)
                {
                    m_objects.Add(obj);

                    if (m_objects.Count == 1)
                    {
                        DictionaryItem di = obj.Dictionary.Get("Linearized");
                        if (di.Valid)
                        {
                            if (di.Type == DictionaryItem.ValueType.Number)
                            {
                                if (di.ValueAsInteger() == 1)
                                {
                                    m_linflag    = true;
                                    m_linearized = true;
                                    Utility.TraceLine("Linearized PDF document found");
                                    Utility.TraceLine("PDF/A: Linearization of document is ignored (section 5.10)");
                                }
                            }
                            else
                            {
                                throw new ParseException("Linearized dictionary item is not a number");
                            }
                        }
                    }
                }

                ReadXref(st);
                if (ReadTrailer(st))
                {
                    if (m_linflag == true)
                    {
                        m_linflag = false;
                        Utility.TraceLine("Linearization trailer");
                    }
                    else
                    {
                        eof = true;
                    }
                }
                ReadStartXref(st);

                if (!eof && (st.Position == pos))
                {
                    st.ReadLine();
                }
            }

            if (st.Position != st.Length)
            {
                m_pdfa = false;
                Utility.TraceLine("PDF/A: Extraneous content after EOF marker breaches section 5.3 requirements");
            }
        }