public long writeToOutputStream(OutputStream out) throws IOException {
        long documentLength = documentSeekableInput.getLength();
        SeekableInputConstrainedWrapper wrapper = new SeekableInputConstrainedWrapper(
                documentSeekableInput, 0L, documentLength, false);
        try {
            wrapper.prepareForCurrentUse();

            byte[] buffer = new byte[4096];
            int length;
            while ((length = wrapper.read(buffer, 0, buffer.length)) > 0) {
                out.write(buffer, 0, length);
            }
        }
        catch (Throwable e) {
            logger.log(Level.FINE, "Error writting PDF output stream.", e);
            throw new IOException(e.getMessage());
        }
        finally {
            try {
                wrapper.close();
            }
            catch (IOException e) {
            }
        }
        return documentLength;
    }