Search in sources :

Example 1 with RandomAccessBuffer

use of com.tom_roush.pdfbox.io.RandomAccessBuffer in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.io.RandomAccessRead) RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) ScratchFile(com.tom_roush.pdfbox.io.ScratchFile) PDFParser(com.tom_roush.pdfbox.pdfparser.PDFParser)

Example 2 with RandomAccessBuffer

use of com.tom_roush.pdfbox.io.RandomAccessBuffer in project PdfBox-Android by TomRoush.

the class CCITTFactoryTest method testCreateFromRandomAccessMulti.

/**
 * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
 * RandomAccess reader) with a multi page TIFF
 */
public void testCreateFromRandomAccessMulti() throws IOException {
    String tiffPath = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4multi.tif";
    // ImageInputStream is = ImageIO.createImageInputStream(new File(tiffPath));
    // ImageReader imageReader = ImageIO.getImageReaders(is).next();
    // imageReader.setInput(is);
    // int countTiffImages = imageReader.getNumImages(true);
    // assertTrue(countTiffImages > 1); TODO: PdfBox-Android
    PDDocument document = new PDDocument();
    int pdfPageNum = 0;
    while (true) {
        PDImageXObject ximage = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffPath)), pdfPageNum);
        if (ximage == null) {
            break;
        }
        // Bitmap bim = imageReader.read(pdfPageNum);
        // validate(ximage, 1, bim.getWidth(), bim.getHeight(), "tiff", PDDeviceGray.INSTANCE.getName());
        // checkIdent(bim, ximage.getOpaqueImage());
        PDPage page = new PDPage(PDRectangle.A4);
        float fX = ximage.getWidth() / page.getMediaBox().getWidth();
        float fY = ximage.getHeight() / page.getMediaBox().getHeight();
        float factor = Math.max(fX, fY);
        document.addPage(page);
        PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
        contentStream.drawImage(ximage, 0, 0, ximage.getWidth() / factor, ximage.getHeight() / factor);
        contentStream.close();
        ++pdfPageNum;
    }
    // assertEquals(countTiffImages, pdfPageNum);
    document.save(testResultsDir + "/multitiff.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "multitiff.pdf"), (String) null);
    // assertEquals(countTiffImages, document.getNumberOfPages());
    document.close();
// imageReader.dispose();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 3 with RandomAccessBuffer

use of com.tom_roush.pdfbox.io.RandomAccessBuffer in project PdfBox-Android by TomRoush.

the class RandomAccessSourceTest method testPositionUnreadBytes.

@Test
public void testPositionUnreadBytes() throws IOException {
    byte[] inputValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    ByteArrayInputStream bais = new ByteArrayInputStream(inputValues);
    RandomAccessSource randomAccessSource = new RandomAccessSource(new RandomAccessBuffer(bais));
    Assert.assertEquals(0, randomAccessSource.getPosition());
    randomAccessSource.read();
    randomAccessSource.read();
    byte[] readBytes = randomAccessSource.readFully(6);
    Assert.assertEquals(8, randomAccessSource.getPosition());
    randomAccessSource.unread(readBytes);
    Assert.assertEquals(2, randomAccessSource.getPosition());
    randomAccessSource.read();
    Assert.assertEquals(3, randomAccessSource.getPosition());
    randomAccessSource.read(readBytes, 2, 4);
    Assert.assertEquals(7, randomAccessSource.getPosition());
    randomAccessSource.unread(readBytes, 2, 4);
    Assert.assertEquals(3, randomAccessSource.getPosition());
    randomAccessSource.close();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 4 with RandomAccessBuffer

use of com.tom_roush.pdfbox.io.RandomAccessBuffer in project PdfBox-Android by TomRoush.

the class RandomAccessSourceTest method testPositionPeek.

@Test
public void testPositionPeek() throws IOException {
    byte[] inputValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    ByteArrayInputStream bais = new ByteArrayInputStream(inputValues);
    RandomAccessSource randomAccessSource = new RandomAccessSource(new RandomAccessBuffer(bais));
    Assert.assertEquals(0, randomAccessSource.getPosition());
    randomAccessSource.readFully(6);
    Assert.assertEquals(6, randomAccessSource.getPosition());
    randomAccessSource.peek();
    Assert.assertEquals(6, randomAccessSource.getPosition());
    randomAccessSource.close();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 5 with RandomAccessBuffer

use of com.tom_roush.pdfbox.io.RandomAccessBuffer in project PdfBox-Android by TomRoush.

the class RandomAccessSourceTest method testPositionReadFully.

@Test
public void testPositionReadFully() throws IOException {
    byte[] inputValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    ByteArrayInputStream bais = new ByteArrayInputStream(inputValues);
    RandomAccessSource randomAccessSource = new RandomAccessSource(new RandomAccessBuffer(bais));
    Assert.assertEquals(0, randomAccessSource.getPosition());
    randomAccessSource.readFully(5);
    Assert.assertEquals(5, randomAccessSource.getPosition());
    try {
        randomAccessSource.readFully(10);
        Assert.fail("readFully beyond EOF should have triggered an EOFException");
    } catch (EOFException exception) {
    }
    randomAccessSource.close();
}
Also used : RandomAccessBuffer(com.tom_roush.pdfbox.io.RandomAccessBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) Test(org.junit.Test)

Aggregations

RandomAccessBuffer (com.tom_roush.pdfbox.io.RandomAccessBuffer)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.Test)5 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)2 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)2 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)2 File (java.io.File)2 RandomAccessRead (com.tom_roush.pdfbox.io.RandomAccessRead)1 ScratchFile (com.tom_roush.pdfbox.io.ScratchFile)1 PDFParser (com.tom_roush.pdfbox.pdfparser.PDFParser)1 EOFException (java.io.EOFException)1