* @throws PDFException         an invalid stream or file encoding
     * @throws PDFSecurityException if a security provider can not be found
     *                              or there is an error decrypting the file.
     */
    private void loadDocumentViaXRefs(SeekableInput in)
            throws PDFException, PDFSecurityException, IOException {
        //if( true ) throw new RuntimeException("Fallback to linear traversal");
        int offset = skipPastAnyPrefixJunk(in);
        long xrefPosition = getInitialCrossReferencePosition(in) + offset;
        PTrailer documentTrailer = null;
        while (xrefPosition > 0L) {
            in.seekAbsolute(xrefPosition);

            Parser parser = new Parser(in);
            Object obj = parser.getObject(library);
            if (obj instanceof PObject)
                obj = ((PObject) obj).getObject();
            PTrailer trailer = (PTrailer) obj;
            //PTrailer trailer = (PTrailer) parser.getObject( library );
            if (trailer == null)
                throw new RuntimeException("Could not find trailer");
            if (trailer.getPrimaryCrossReference() == null)
                throw new RuntimeException("Could not find cross reference");
            trailer.setPosition(xrefPosition);

            if (documentTrailer == null) {
                documentTrailer = trailer;
            }else{
                documentTrailer.addPreviousTrailer(trailer);
            }

            // If this trailer has everything we need to get started,
            //   then we can lazily load other trailers later
            if (true) //TODO So should we make this not be a loop then?
                break;
            xrefPosition = trailer.getPrev();
        }
        if (documentTrailer == null)
            throw new RuntimeException("Could not find document trailer");
        if (offset > 0){
            // mark the offset, so that it can be correct for later during
            // object retrieval.
            documentTrailer.getCrossReferenceTable().setOffset(offset);
        }

        LazyObjectLoader lol = new LazyObjectLoader(
                library, in, documentTrailer.getPrimaryCrossReference());
        library.setLazyObjectLoader(lol);

        pTrailer = documentTrailer;
        catalog = documentTrailer.getRootCatalog();
        library.setCatalog(catalog);

        if (catalog == null)
            throw new NullPointerException("Loading via xref failed to find catalog");

        boolean madeSecurityManager = makeSecurityManager(documentTrailer);
        if (madeSecurityManager)
            attemptAuthorizeSecurityManager();
    }