Esempio n. 1
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();
            }
        }
Esempio n. 2
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");
            }
        }