Search in sources :

Example 6 with RandomAccessBuffer

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

the class RandomAccessSourceTest method testPositionRead.

@Test
public void testPositionRead() 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();
    randomAccessSource.read();
    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 7 with RandomAccessBuffer

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

the class RandomAccessSourceTest method testPositionReadBytes.

@Test
public void testPositionReadBytes() 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());
    byte[] buffer = new byte[4];
    randomAccessSource.read(buffer);
    Assert.assertEquals(4, randomAccessSource.getPosition());
    randomAccessSource.read(buffer, 1, 2);
    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 8 with RandomAccessBuffer

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

the class CCITTFactoryTest method testCreateFromRandomAccessSingle.

/**
 * Tests CCITTFactory#createFromRandomAccess(PDDocument document,
 * RandomAccess reader) with a single page TIFF
 */
public void testCreateFromRandomAccessSingle() throws IOException {
    String tiffG3Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg3.tif";
    String tiffG4Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4.tif";
    PDDocument document = new PDDocument();
    PDImageXObject ximage3 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG3Path)));
    validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim3 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim3, ximage3.getOpaqueImage());
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage3, 0, 0, ximage3.getWidth(), ximage3.getHeight());
    contentStream.close();
    PDImageXObject ximage4 = CCITTFactory.createFromRandomAccess(document, new RandomAccessBuffer(testContext.getAssets().open(tiffG4Path)));
    validate(ximage4, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // Bitmap bim4 = ImageIO.read(new File(tiffG3Path));
    // checkIdent(bim4, ximage4.getOpaqueImage());
    page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage4, 0, 0);
    contentStream.close();
    document.save(testResultsDir + "/singletiff.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "singletiff.pdf"));
    assertEquals(2, document.getNumberOfPages());
    document.close();
}
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)

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