Search in sources :

Example 11 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class LosslessFactoryTest method testCreateLosslessFromImageRGB.

/**
 * Tests RGB LosslessFactoryTest#createFromImage(PDDocument document,
 * BufferedImage image)
 *
 * @throws java.io.IOException
 */
@Test
public void testCreateLosslessFromImageRGB() throws IOException {
    PDDocument document = new PDDocument();
    Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/png.png"));
    PDImageXObject ximage1 = LosslessFactory.createFromImage(document, image);
    validate(ximage1, 8, image.getWidth(), image.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
    checkIdent(image, ximage1.getImage());
    // Create a grayscale image
    Bitmap grayImage = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ALPHA_8);
    Canvas canvas = new Canvas();
    canvas.setBitmap(grayImage);
    Paint paint = new Paint();
    paint.setColor(Color.TRANSPARENT);
    canvas.drawBitmap(image, 0, 0, paint);
    PDImageXObject ximage2 = LosslessFactory.createFromImage(document, grayImage);
    validate(ximage2, 8, grayImage.getWidth(), grayImage.getHeight(), "png", PDDeviceGray.INSTANCE.getName());
    checkIdent(grayImage, ximage2.getImage());
    // Create a bitonal image
    // BufferedImage bitonalImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); TODO: PdfBox-Android
    // avoid multiple of 8 to test padding
    // assertFalse(bitonalImage.getWidth() % 8 == 0);
    // g = bitonalImage.getGraphics();
    // g.drawImage(image, 0, 0, null);
    // g.dispose();
    // PDImageXObject ximage3 = LosslessFactory.createFromImage(document, bitonalImage);
    // validate(ximage3, 1, bitonalImage.getWidth(), bitonalImage.getHeight(), "png", PDDeviceGray.INSTANCE.getName());
    // checkIdent(bitonalImage, ximage3.getImage());
    // This part isn't really needed because this test doesn't break
    // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
    // if something goes wrong in the future and we want to have a PDF to open.
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, false);
    contentStream.drawImage(ximage1, 200, 300, ximage1.getWidth() / 2, ximage1.getHeight() / 2);
    contentStream.drawImage(ximage2, 200, 450, ximage2.getWidth() / 2, ximage2.getHeight() / 2);
    // contentStream.drawImage(ximage3, 200, 600, ximage3.getWidth() / 2, ximage3.getHeight() / 2);
    contentStream.close();
    File pdfFile = new File(testResultsDir, "misc.pdf");
    document.save(pdfFile);
    document.close();
    document = PDDocument.load(pdfFile, (String) null);
    new PDFRenderer(document).renderImage(0);
    document.close();
}
Also used : Bitmap(android.graphics.Bitmap) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Canvas(android.graphics.Canvas) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) Paint(android.graphics.Paint) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) FlakyTest(androidx.test.filters.FlakyTest) Test(org.junit.Test)

Example 12 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class MainActivity method renderFile.

/**
 * Loads an existing PDF and renders it to a Bitmap
 */
public void renderFile(View v) {
    // Render the page and save it to an image file
    try {
        // Load in an already created PDF
        PDDocument document = PDDocument.load(assetManager.open("Created.pdf"));
        // Create a renderer for the document
        PDFRenderer renderer = new PDFRenderer(document);
        // Render the image to an RGB Bitmap
        pageImage = renderer.renderImage(0, 1, ImageType.RGB);
        // Save the render result to an image
        String path = root.getAbsolutePath() + "/render.jpg";
        File renderFile = new File(path);
        FileOutputStream fileOut = new FileOutputStream(renderFile);
        pageImage.compress(Bitmap.CompressFormat.JPEG, 100, fileOut);
        fileOut.close();
        tv.setText("Successfully rendered image to " + path);
        // Optional: display the render result on screen
        displayRenderedImage();
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while rendering file", e);
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer)

Aggregations

PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)12 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)11 File (java.io.File)10 Bitmap (android.graphics.Bitmap)6 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)5 FileOutputStream (java.io.FileOutputStream)5 Test (org.junit.Test)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)4 Paint (android.graphics.Paint)2 IOException (java.io.IOException)2 Canvas (android.graphics.Canvas)1 FlakyTest (androidx.test.filters.FlakyTest)1 COSArray (com.tom_roush.pdfbox.cos.COSArray)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)1 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)1 AccessPermission (com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1