Ejemplo n.º 1
0
        public static PdfStream Parse(PdfDictionary dictionary, Lexer lexer)
        {
            if (dictionary is null)
            {
                throw new ArgumentNullException(nameof(dictionary));
            }

            lexer.Expects("stream");
            char eol = lexer.ReadChar();

            if (eol == '\r')
            {
                eol = lexer.ReadChar();
            }

            if (eol != '\n')
            {
                throw new ParsingException($@"Stream must end with either \r\n or \n. Was '{eol}'");
            }

            IPdfObject lengthObject = dictionary["Length"];

            if (lengthObject is null)
            {
                throw new ParsingException("Stream dictionary is missing 'Length' entry");
            }

            int length = 0;

            if (lengthObject is PdfIndirectReference reference)
            {
                PdfIndirectObject lenobj = lexer.IndirectReferenceResolver
                                           .GetObject(reference.ObjectNumber, reference.GenerationNumber);

                length = ((PdfNumeric)lenobj.Object).ToInt32();
            }
            else
            {
                length = int.Parse(lengthObject.ToString(), CultureInfo.InvariantCulture);
            }

            var data = PdfData.Parse(lexer, length);

            lexer.Expects("endstream");

            return(new PdfStream(dictionary, data));
        }
Ejemplo n.º 2
0
        public static PdfObject ParseAny(Lexer lexer, string endToken)
        {
            string token = lexer.ReadToken();

            if (token is null)
            {
                return(null);
            }

            switch (token)
            {
            case "null": return(PdfNull.Null);     // null object

            case "true":
            case "false":
                return(PdfBoolean.Parse(token));

            case "/": return(PdfName.Parse(lexer));

            case "%": return(PdfComment.Parse(lexer));

            case "<": return(PdfHexadecimalString.Parse(lexer));

            case "(": return(PdfLiteralString.Parse(lexer));

            case "xref":
                return(PdfXRef.Parse(lexer));

            case "trailer":
                return(PdfTrailer.Parse(lexer));

            case "<<":
                var dic = PdfDictionary.Parse(lexer);

                // check for stream and combine put dictionary into stream object
                token = lexer.PeekToken1();

                if (token == "stream")
                {
                    return(PdfStream.Parse(dic, lexer));
                }

                return(dic);

            case "[": return(PdfArray.Parse(lexer));

            case "startxref":
                return(PdfStartXRef.Parse(lexer));

            case ")":
            case ">":
            case ">>":
            case "]":
            case "}":
            case "stream":
            case "endstream":
            case "endobj":
                if (endToken == token)
                {
                    return(null);    // expected end
                }

                throw new ParsingException("Out of sync");

            default:
                // must be an integer or double value
                PdfNumeric num = PdfNumeric.Parse(token);
                if (num.IsInteger)
                {
                    string token2 = lexer.PeekToken2();
                    switch (token2)
                    {
                    case "obj":
                        return(PdfIndirectObject.Parse(lexer, num.ToInt32()));

                    case "R":
                        PdfIndirectReference ir = PdfIndirectReference.Parse(lexer, num.ToInt32());
                        ir.Resolver = lexer.IndirectReferenceResolver;

                        return(ir);

                    default:
                        // ignore;
                        return(num);
                    }
                }
                else
                {
                    return(num);
                }
            }

            throw new ParsingException("Could not read object");
        }