Exemplo n.º 1
0
        public IPdfObject ReadObject(int id)
        {
            // trailer
            // <<
            // /Size 5274
            // /Root 5273 0 R
            // /Info 5272 0 R
            // >>

            // 5273 0 obj
            // <<
            // /Type /Catalog
            // /Pages 1 0 R
            // /OpenAction [3 0 R /FitH null]
            // /PageLayout /OneColumn
            // >>
            // endobj

            // 3 0 obj
            // <</Type /Page
            // /Parent 1 0 R
            // /MediaBox [0 0 907.09 1292.59]
            // /Resources 2 0 R
            // /Contents 4 0 R>>
            // endobj

            if (!gXref.ContainsKey(id))
            {
                throw new PBException("error unknow object {0}", id);
            }
            PdfXref xref = gXref[id];

            gpsr.StartReadObject(string.Format("object {0}", id));
            gpsr.Seek(xref.filePosition);
            // 5273 0 obj
            Regex rg_object_begin = new Regex("^([0-9]+) ([0-9]+) obj$", RegexOptions.Compiled);
            PdfObjectDictionary o = new PdfObjectDictionary(id, xref.generationNumber);
            string s     = gpsr.ReadLine();
            Match  match = rg_object_begin.Match(s);

            if (!match.Success || int.Parse(match.Groups[1].Value) != id || int.Parse(match.Groups[2].Value) != xref.generationNumber)
            {
                throw new PBException("error reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            }
            s = gpsr.ReadLine();

            o.value = ReadPdfValue(ref s);

            if (s == "")
            {
                s = gpsr.ReadLine();
            }
            if (s == "stream")
            {
                int       length      = 0;
                PdfNValue lengthValue = null;
                if (o.value.isObject())
                {
                    lengthValue = o.value["Length"];
                }
                if (lengthValue == null)
                {
                    throw new PBException("error stream without /Length reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
                }
                if (lengthValue.value.isInt())
                {
                    length = lengthValue.value.valueInt;
                }
                else if (lengthValue.value.isObjectRef())
                {
                    long            position     = gpsr.Position;
                    PdfObjectReader or           = new PdfObjectReader(this);
                    IPdfObject      lengthObject = or.ReadObject(lengthValue.value.valueObjectId);
                    gpsr.Seek(position);
                    if (lengthObject.value.isInt())
                    {
                        length = lengthObject.value.valueInt;
                    }
                }
                if (length != 0)
                {
                    o.stream = gpsr.ReadStream(length);
                }
                else
                {
                    o.stream = gpsr.ReadStream();
                }
                s = gpsr.ReadLine();
            }
            if (s != "endobj")
            {
                throw new PBException("error endobj not found reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            }
            o.source = gpsr.GetObjectSource();
            return(o);
        }
Exemplo n.º 2
0
        public IPdfObject ReadObject(int id)
        {
            // trailer
            // <<
            // /Size 5274
            // /Root 5273 0 R
            // /Info 5272 0 R
            // >>

            // 5273 0 obj
            // <<
            // /Type /Catalog
            // /Pages 1 0 R
            // /OpenAction [3 0 R /FitH null]
            // /PageLayout /OneColumn
            // >>
            // endobj

            // 3 0 obj
            // <</Type /Page
            // /Parent 1 0 R
            // /MediaBox [0 0 907.09 1292.59]
            // /Resources 2 0 R
            // /Contents 4 0 R>>
            // endobj

            if (!gXref.ContainsKey(id)) throw new PBException("error unknow object {0}", id);
            PdfXref xref = gXref[id];

            gpsr.StartReadObject(string.Format("object {0}", id));
            gpsr.Seek(xref.filePosition);
            // 5273 0 obj
            Regex rg_object_begin = new Regex("^([0-9]+) ([0-9]+) obj$", RegexOptions.Compiled);
            PdfObjectDictionary o = new PdfObjectDictionary(id, xref.generationNumber);
            string s = gpsr.ReadLine();
            Match match = rg_object_begin.Match(s);
            if (!match.Success || int.Parse(match.Groups[1].Value) != id || int.Parse(match.Groups[2].Value) != xref.generationNumber)
                throw new PBException("error reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            s = gpsr.ReadLine();

            o.value = ReadPdfValue(ref s);

            if (s == "")
                s = gpsr.ReadLine();
            if (s == "stream")
            {
                int length = 0;
                PdfNValue lengthValue = null;
                if (o.value.isObject())
                    lengthValue = o.value["Length"];
                if (lengthValue == null) throw new PBException("error stream without /Length reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
                if (lengthValue.value.isInt())
                    length = lengthValue.value.valueInt;
                else if (lengthValue.value.isObjectRef())
                {
                    long position = gpsr.Position;
                    PdfObjectReader or = new PdfObjectReader(this);
                    IPdfObject lengthObject = or.ReadObject(lengthValue.value.valueObjectId);
                    gpsr.Seek(position);
                    if (lengthObject.value.isInt())
                        length = lengthObject.value.valueInt;
                }
                if (length != 0)
                    o.stream = gpsr.ReadStream(length);
                else
                    o.stream = gpsr.ReadStream();
                s = gpsr.ReadLine();
            }
            if (s != "endobj") throw new PBException("error endobj not found reading {0} line {1} \"{2}\"", gpsr.ObjectName, gpsr.LineNumber, s);
            o.source = gpsr.GetObjectSource();
            return o;
        }