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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations