Search in sources :

Example 1 with ScratchFile

use of org.apache.pdfbox.io.ScratchFile in project pdfbox by apache.

the class TestPDFParser method executeParserTest.

private void executeParserTest(RandomAccessRead source, MemoryUsageSetting memUsageSetting) throws IOException {
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    PDFParser pdfParser = new PDFParser(source, scratchFile);
    pdfParser.parse();
    try (COSDocument doc = pdfParser.getDocument()) {
        assertNotNull(doc);
    }
    source.close();
    // number tmp file must be the same
    assertEquals(numberOfTmpFiles, getNumberOfTempFile());
}
Also used : ScratchFile(org.apache.pdfbox.io.ScratchFile) COSDocument(org.apache.pdfbox.cos.COSDocument)

Example 2 with ScratchFile

use of org.apache.pdfbox.io.ScratchFile in project pdfbox by apache.

the class PDDocument method load.

/**
 * Parses a PDF. Depending on the memory settings parameter the given input
 * stream is either copied to memory or to a temporary file to enable
 * random access to the pdf.
 *
 * @param input stream that contains the document.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams
 *
 * @return loaded document
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException {
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try {
        RandomAccessRead source = scratchFile.createBuffer(input);
        PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    } catch (IOException ioe) {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
Also used : RandomAccessRead(org.apache.pdfbox.io.RandomAccessRead) ScratchFile(org.apache.pdfbox.io.ScratchFile) PDFParser(org.apache.pdfbox.pdfparser.PDFParser) IOException(java.io.IOException)

Example 3 with ScratchFile

use of org.apache.pdfbox.io.ScratchFile in project pdfbox by apache.

the class PDDocument method load.

/**
 * Parses a PDF.
 *
 * @param input byte array that contains the document.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams
 *
 * @return loaded document
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(byte[] input, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException {
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    RandomAccessRead source = new RandomAccessBuffer(input);
    PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
    parser.parse();
    return parser.getPDDocument();
}
Also used : RandomAccessRead(org.apache.pdfbox.io.RandomAccessRead) RandomAccessBuffer(org.apache.pdfbox.io.RandomAccessBuffer) ScratchFile(org.apache.pdfbox.io.ScratchFile) PDFParser(org.apache.pdfbox.pdfparser.PDFParser)

Example 4 with ScratchFile

use of org.apache.pdfbox.io.ScratchFile in project pdfbox by apache.

the class PDDocument method load.

/**
 * Parses a PDF.
 *
 * @param file file to be loaded
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering PDF streams
 *
 * @return loaded document
 *
 * @throws IOException in case of a file reading or parsing error
 */
public static PDDocument load(File file, String password, InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting) throws IOException {
    RandomAccessBufferedFileInputStream raFile = new RandomAccessBufferedFileInputStream(file);
    try {
        ScratchFile scratchFile = new ScratchFile(memUsageSetting);
        try {
            PDFParser parser = new PDFParser(raFile, password, keyStore, alias, scratchFile);
            parser.parse();
            return parser.getPDDocument();
        } catch (IOException ioe) {
            IOUtils.closeQuietly(scratchFile);
            throw ioe;
        }
    } catch (IOException ioe) {
        IOUtils.closeQuietly(raFile);
        throw ioe;
    }
}
Also used : ScratchFile(org.apache.pdfbox.io.ScratchFile) PDFParser(org.apache.pdfbox.pdfparser.PDFParser) IOException(java.io.IOException) RandomAccessBufferedFileInputStream(org.apache.pdfbox.io.RandomAccessBufferedFileInputStream)

Aggregations

ScratchFile (org.apache.pdfbox.io.ScratchFile)4 PDFParser (org.apache.pdfbox.pdfparser.PDFParser)3 IOException (java.io.IOException)2 RandomAccessRead (org.apache.pdfbox.io.RandomAccessRead)2 COSDocument (org.apache.pdfbox.cos.COSDocument)1 RandomAccessBuffer (org.apache.pdfbox.io.RandomAccessBuffer)1 RandomAccessBufferedFileInputStream (org.apache.pdfbox.io.RandomAccessBufferedFileInputStream)1