Esempio n. 1
0
        private long XrefPosition(Tokenizer tokenizer)
        {
            tokenizer.MoveToEnd();
            tokenizer.MoveToPrevious('s');

            Token token = tokenizer.Token();

            if (!TokenValidator.Validate(token, CharacterSetType.Regular, "startxref"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "Expected startxref");
            }

            token = tokenizer.Token();
            if (!TokenValidator.IsWhiteSpace(token))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "Expected a whitespace between starxref and position");
            }

            token = tokenizer.Token();
            if (!TokenValidator.IsRegularNumber(token))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "startxref position expected");
            }

            long xrefPosition = token.ToLong();

            token = tokenizer.Token();
            if (!TokenValidator.IsDelimiter(token))
            {
                token = tokenizer.Token();
            }

            if (!TokenValidator.IsDelimiter(token, "%"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "Expected %%EOF at end of the file");
            }

            token = tokenizer.Token();
            if (!TokenValidator.IsDelimiter(token, "%"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "Expected %%EOF at end of the file");
            }

            token = tokenizer.Token();
            if (!TokenValidator.Validate(token, CharacterSetType.Regular, "EOF"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_EOF, "Expected %%EOF at end of the file");
            }

            return(xrefPosition);
        }
Esempio n. 2
0
        private DocumentCatalog ReadXRef(Tokenizer tokenizer)
        {
            long xrefPosition = XrefPosition(tokenizer);

            tokenizer.MoveToPosition(xrefPosition);

            Token token = tokenizer.Token();

            if (!TokenValidator.Validate(token, CharacterSetType.Regular, "xref"))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_XREF, "Expected xref");
            }

            token = tokenizer.Token();
            if (!TokenValidator.IsWhiteSpace(token))
            {
                throw new PdfException(PdfExceptionCodes.INVALID_XREF, "after xref must be a whitspace");
            }

            tokenizer.GetInteger();                         // objectNumber
            tokenizer.TokenExcludedComments();
            int numberOfEntries = tokenizer.GetInteger();

            tokenizer.TokenExcludedComments();

            PDFObjects pdfObjects = new PDFObjects();

            for (int i = 0; i < numberOfEntries; i++)
            {
                int pos = tokenizer.GetInteger();           // position
                tokenizer.TokenExcludedComments();          // whitespace
                tokenizer.GetInteger();                     // generation
                tokenizer.TokenExcludedComments();          // whitespace
                string type = tokenizer.Token().ToString(); // f or n
                tokenizer.TokenExcludedComments();          // whitespace

                if (type != "f" && type != "n")
                {
                    throw new PdfException(PdfExceptionCodes.INVALID_XREF, "only xref f or n entries allowed");
                }

                if (type == "f")      // free element
                {
                    continue;
                }

                tokenizer.SavePosition();
                tokenizer.MoveToPosition(pos);

                IndirectObject obj = new IndirectObject(tokenizer);
                pdfObjects.AddObject(obj);
                tokenizer.RestorePosition();
            }

            if (tokenizer.Token().ToString() != "trailer")
            {
                throw new PdfException(PdfExceptionCodes.INVALID_TRAILER, "expected trailer");
            }

            var trailer      = new DictionaryObject(tokenizer);
            var rootIndirect = (IndirectReferenceObject)trailer.Dictionary["Root"];

            return(pdfObjects.GetDocument <DocumentCatalog>(rootIndirect));
        }