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