Search in sources :

Example 6 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream 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 7 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class CCITTFactoryTest method testCreateFromBufferedChessImage.

public void testCreateFromBufferedChessImage() throws IOException {
    PDDocument document = new PDDocument();
    Bitmap bim = Bitmap.createBitmap(343, 287, Bitmap.Config.ALPHA_8);
    // not mult of 8
    assertTrue((bim.getWidth() / 8) * 8 != bim.getWidth());
    int col = 0;
    for (int x = 0; x < bim.getWidth(); ++x) {
        for (int y = 0; y < bim.getHeight(); ++y) {
            bim.setPixel(x, y, col & 0xFFFFFF);
            col = ~col;
        }
    }
    PDImageXObject ximage3 = CCITTFactory.createFromImage(document, bim);
    validate(ximage3, 1, 343, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // checkIdent(bim, ximage3.getOpaqueImage()); TODO: PdfBox-Android
    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();
    document.save(testResultsDir + "/singletifffromchessbi.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "singletifffromchessbi.pdf"));
    assertEquals(1, document.getNumberOfPages());
    document.close();
}
Also used : Bitmap(android.graphics.Bitmap) 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 8 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class CCITTFactoryTest method testCreateFromBitmap.

public void testCreateFromBitmap() throws IOException {
    String tiffG4Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4.tif";
    PDDocument document = new PDDocument();
    // Bitmap bim = ImageIO.read(new File(tiffG4Path));
    // PDImageXObject ximage3 = CCITTFactory.createFromImage(document, bim);
    // validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
    // checkIdent(bim, 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();
    document.save(testResultsDir + "/singletifffrombi.pdf");
    document.close();
    document = PDDocument.load(new File(testResultsDir, "singletifffrombi.pdf"));
    assertEquals(1, document.getNumberOfPages());
    document.close();
}
Also used : 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 9 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class PDInlineImageTest method testInlineImage.

/**
 * Tests PDInlineImage#PDInlineImage(COSDictionary parameters, byte[] data,
 * Map<String, PDColorSpace> colorSpaces)
 */
@Test
public void testInlineImage() throws IOException {
    COSDictionary dict = new COSDictionary();
    dict.setBoolean(COSName.IM, true);
    int width = 31;
    int height = 27;
    dict.setInt(COSName.W, width);
    dict.setInt(COSName.H, height);
    dict.setInt(COSName.BPC, 1);
    int rowbytes = width / 8;
    if (rowbytes * 8 < width) {
        // PDF spec:
        // If the number of data bits per row is not a multiple of 8,
        // the end of the row is padded with extra bits to fill out the last byte.
        ++rowbytes;
    }
    // draw a grid
    int datalen = rowbytes * height;
    byte[] data = new byte[datalen];
    for (int i = 0; i < datalen; ++i) {
        data[i] = (i / 4 % 2 == 0) ? (byte) Integer.parseInt("10101010", 2) : 0;
    }
    PDInlineImage inlineImage1 = new PDInlineImage(dict, data, null);
    assertTrue(inlineImage1.isStencil());
    assertEquals(width, inlineImage1.getWidth());
    assertEquals(height, inlineImage1.getHeight());
    assertEquals(1, inlineImage1.getBitsPerComponent());
    COSDictionary dict2 = new COSDictionary();
    dict2.addAll(dict);
    // use decode array to revert in image2
    COSArray decodeArray = new COSArray();
    decodeArray.add(COSInteger.ONE);
    decodeArray.add(COSInteger.ZERO);
    dict2.setItem(COSName.DECODE, decodeArray);
    PDInlineImage inlineImage2 = new PDInlineImage(dict2, data, null);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    Bitmap stencilImage = inlineImage1.getStencilImage(paint);
    assertEquals(width, stencilImage.getWidth());
    assertEquals(height, stencilImage.getHeight());
    Bitmap stencilImage2 = inlineImage2.getStencilImage(paint);
    assertEquals(width, stencilImage2.getWidth());
    assertEquals(height, stencilImage2.getHeight());
    Bitmap image1 = inlineImage1.getImage();
    assertEquals(width, image1.getWidth());
    assertEquals(height, image1.getHeight());
    Bitmap image2 = inlineImage2.getImage();
    assertEquals(width, image2.getWidth());
    assertEquals(height, image2.getHeight());
    // write and read
    boolean writeOk = image1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(testResultsDir.getPath() + "/inline-grid1.png"));
    assertTrue(writeOk);
    Bitmap bim1 = BitmapFactory.decodeFile(new File(testResultsDir + "/inline-grid1.png").getPath());
    assertNotNull(bim1);
    assertEquals(width, bim1.getWidth());
    assertEquals(height, bim1.getHeight());
    writeOk = image2.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(testResultsDir.getPath() + "/inline-grid2.png"));
    assertTrue(writeOk);
    Bitmap bim2 = BitmapFactory.decodeFile(new File(testResultsDir + "/inline-grid2.png").getPath());
    assertNotNull(bim2);
    assertEquals(width, bim2.getWidth());
    assertEquals(height, bim2.getHeight());
    // compare: pixels with even coordinates are white (FF), all others are black (0)
    int[] bimPixels = new int[width * height];
    bim1.getPixels(bimPixels, 0, width, 0, 0, width, height);
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            if (x % 2 == 0 && y % 2 == 0) {
                assertEquals(0xFFFFFF, bimPixels[x + width * y] & 0xFFFFFF);
            } else {
                assertEquals(0, bimPixels[x + width * y] & 0xFFFFFF);
            }
        }
    }
    // compare: pixels with odd coordinates are white (FF), all others are black (0)
    bim2.getPixels(bimPixels, 0, width, 0, 0, width, height);
    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            if (x % 2 == 0 && y % 2 == 0) {
                assertEquals(0, bimPixels[x + width * y] & 0xFFFFFF);
            } else {
                assertEquals(0xFFFFFF, bimPixels[x + width * y] & 0xFFFFFF);
            }
        }
    }
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false);
    contentStream.drawImage(inlineImage1, 150, 400);
    contentStream.drawImage(inlineImage1, 150, 500, inlineImage1.getWidth() * 2, inlineImage1.getHeight() * 2);
    contentStream.drawImage(inlineImage1, 150, 600, inlineImage1.getWidth() * 4, inlineImage1.getHeight() * 4);
    contentStream.drawImage(inlineImage2, 350, 400);
    contentStream.drawImage(inlineImage2, 350, 500, inlineImage2.getWidth() * 2, inlineImage2.getHeight() * 2);
    contentStream.drawImage(inlineImage2, 350, 600, inlineImage2.getWidth() * 4, inlineImage2.getHeight() * 4);
    contentStream.close();
    File pdfFile = new File(testResultsDir, "inline.pdf");
    document.save(pdfFile);
    document.close();
    document = PDDocument.load(pdfFile, (String) null);
    new PDFRenderer(document).renderImage(0);
    document.close();
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) Paint(android.graphics.Paint) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) COSArray(com.tom_roush.pdfbox.cos.COSArray) FileOutputStream(java.io.FileOutputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) Test(org.junit.Test)

Example 10 with PDPageContentStream

use of com.tom_roush.pdfbox.pdmodel.PDPageContentStream in project PdfBox-Android by TomRoush.

the class TestOptionalContentGroupsInstrumentationTest method testOCGGenerationSameNameCanHaveSameVisibilityOff.

/**
 * PDFBOX-4496: setGroupEnabled(String, boolean) must catch all OCGs of a name even when several
 * names are identical.
 *
 * @throws IOException
 */
@Test
public void testOCGGenerationSameNameCanHaveSameVisibilityOff() throws IOException {
    Bitmap expectedImage;
    Bitmap actualImage;
    PDDocument doc = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        // Prepare OCG functionality
        PDOptionalContentProperties ocprops = new PDOptionalContentProperties();
        doc.getDocumentCatalog().setOCProperties(ocprops);
        // ocprops.setBaseState(BaseState.ON); //ON=default
        // Create OCG for background
        PDOptionalContentGroup background = new PDOptionalContentGroup("background");
        ocprops.addGroup(background);
        assertTrue(ocprops.isGroupEnabled("background"));
        // Create OCG for enabled
        PDOptionalContentGroup enabled = new PDOptionalContentGroup("science");
        ocprops.addGroup(enabled);
        assertFalse(ocprops.setGroupEnabled("science", true));
        assertTrue(ocprops.isGroupEnabled("science"));
        // Create OCG for disabled1
        PDOptionalContentGroup disabled1 = new PDOptionalContentGroup("alternative");
        ocprops.addGroup(disabled1);
        // Create OCG for disabled2 with same name as disabled1
        PDOptionalContentGroup disabled2 = new PDOptionalContentGroup("alternative");
        ocprops.addGroup(disabled2);
        assertFalse(ocprops.setGroupEnabled("alternative", false));
        assertFalse(ocprops.isGroupEnabled("alternative"));
        // Setup page content stream and paint background/title
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        contentStream.beginMarkedContent(COSName.OC, background);
        contentStream.beginText();
        contentStream.setFont(font, 14);
        contentStream.newLineAtOffset(80, 700);
        contentStream.showText("PDF 1.5: Optional Content Groups");
        contentStream.endText();
        contentStream.endMarkedContent();
        font = PDType1Font.HELVETICA;
        // Paint enabled layer
        contentStream.beginMarkedContent(COSName.OC, enabled);
        contentStream.setNonStrokingColor(AWTColor.GREEN);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 600);
        contentStream.showText("The earth is a sphere");
        contentStream.endText();
        contentStream.endMarkedContent();
        // Paint disabled layer1
        contentStream.beginMarkedContent(COSName.OC, disabled1);
        contentStream.setNonStrokingColor(AWTColor.RED);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 500);
        contentStream.showText("Alternative 1: The earth is a flat circle");
        contentStream.endText();
        contentStream.endMarkedContent();
        // Paint disabled layer2
        contentStream.beginMarkedContent(COSName.OC, disabled2);
        contentStream.setNonStrokingColor(AWTColor.BLUE);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 450);
        contentStream.showText("Alternative 2: The earth is a flat parallelogram");
        contentStream.endText();
        contentStream.endMarkedContent();
        contentStream.close();
        doc.getDocumentCatalog().setPageMode(PageMode.USE_OPTIONAL_CONTENT);
        File targetFile = new File(testResultsDir, "ocg-generation-same-name-off.pdf");
        doc.save(targetFile.getAbsolutePath());
        doc.close();
        // render PDF with science disabled and alternatives with same name enabled
        doc = PDDocument.load(new File(testResultsDir, "ocg-generation-same-name-off.pdf"));
        doc.getDocumentCatalog().getOCProperties().setGroupEnabled("background", false);
        doc.getDocumentCatalog().getOCProperties().setGroupEnabled("science", false);
        doc.getDocumentCatalog().getOCProperties().setGroupEnabled("alternative", true);
        actualImage = new PDFRenderer(doc).renderImage(0, 2);
        actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-actual.png")));
    } finally {
        doc.close();
    }
    // create PDF without OCGs to created expected rendering
    PDDocument doc2 = new PDDocument();
    try {
        // Create new page
        PDPage page = new PDPage();
        doc2.addPage(page);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
            page.setResources(resources);
        }
        PDPageContentStream contentStream = new PDPageContentStream(doc2, page, AppendMode.OVERWRITE, false);
        PDFont font = PDType1Font.HELVETICA;
        contentStream.setNonStrokingColor(AWTColor.RED);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 500);
        contentStream.showText("Alternative 1: The earth is a flat circle");
        contentStream.endText();
        contentStream.setNonStrokingColor(AWTColor.BLUE);
        contentStream.beginText();
        contentStream.setFont(font, 12);
        contentStream.newLineAtOffset(80, 450);
        contentStream.showText("Alternative 2: The earth is a flat parallelogram");
        contentStream.endText();
        contentStream.close();
        expectedImage = new PDFRenderer(doc2).renderImage(0, 2);
        actualImage.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(testResultsDir, "ocg-generation-same-name-off-expected.png")));
    } finally {
        doc2.close();
    }
    // compare images
    int height = expectedImage.getHeight();
    int width = expectedImage.getWidth();
    int[] expectedImagePixels = new int[width * height];
    expectedImage.getPixels(expectedImagePixels, 0, width, 0, 0, width, height);
    int[] actualImagePixels = new int[width * height];
    actualImage.getPixels(actualImagePixels, 0, width, 0, 0, width, height);
    Assert.assertArrayEquals(expectedImagePixels, actualImagePixels);
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) Test(org.junit.Test)

Aggregations

PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)27 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)24 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)22 File (java.io.File)18 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 Test (org.junit.Test)7 Bitmap (android.graphics.Bitmap)5 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 Matrix (com.tom_roush.pdfbox.util.Matrix)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Paint (android.graphics.Paint)2 RandomAccessBuffer (com.tom_roush.pdfbox.io.RandomAccessBuffer)2 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2