Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pdf Renderer in android converted image is Transparent background

I'm a newbie for android developing. I'm working on converting Pdf to Image and storing it in a location. I have used the PdfRenderer (API level 21) to convert the PDF to bitmap Image. The converted image is Transparent background. Please guide me to convert the image with white background. So that I can convert it to binary digits.

PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY));

        Bitmap bitmap;
        final int pageCount = renderer.getPageCount();
        System.out.println("pageCount"+pageCount);
        for (int i = 0; i < pageCount; i++) {
            PdfRenderer.Page page = renderer.openPage(i);

            int width = getResources().getDisplayMetrics().densityDpi / 72 * page.getWidth();
            int height = getResources().getDisplayMetrics().densityDpi / 72 * page.getHeight();
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
            storeImage(bitmap,"test.bmp");//I have wrote a function here to save the image

This is the Transparent Image I get after converting

Thanks in advance.

like image 669
abdul kadhar Avatar asked Jul 21 '17 12:07

abdul kadhar


People also ask

How do I make the background of a PDF white?

Choose Tools > Edit PDF. The Edit PDF toolset is displayed in the secondary toolbar. In the secondary toolbar, choose More > Background > Update. Click OK, or make other changes to the background options and then click OK.

Can PDF images have transparent background?

Make PDF Background Transparent Click on the "Edit" tab, then select "Background" and choose "Edit Background" from the drop-down menu. Now a window pops up, and you need to click the Pencil-like icon to edit the background. Then you can customize the background. Select 0% in the "Opacity" option.

How to convert PDF to transparent PNG on Windows 10?

On launching the software, click on the "Open files" button and upload your desired PDF file to the application. Step 2. Click "To Image" After that, go to the main menu and click on "Convert" and select "To Image" from the convert output options. Step 3. Convert PDF to Transparent PNG

Is it possible to render a PDF on Android?

Developers or not, most of us are familiar with PDFs and are used to work with them all the time, so rendering PDFs may seem like an ordinary task that any platform should be able to perform with minimal effort. Surprisingly, that's not exactly the case on Android.

How to give 20% transparent yellow background for text view in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code we have give 20% transparent yellow background for text view. Step 3 − Add the following code to src/MainActivity.java.

What is pdfrenderer in Java?

What is PdfRenderer? The PdfRenderer allows us to create a Bitmap from a page in a PDF document so that we can display it on the screen. PdfRenderer class is not thread-safe. If we want to render a PDF, we first need to get a ParcelFileDescriptor from the file and then create a renderer instance.


2 Answers

I have used canvas and it worked

Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(bitmap, 0, 0, null);

Thank you.

like image 172
abdul kadhar Avatar answered Nov 15 '22 06:11

abdul kadhar


Using Abdul's answer, this is the complete implementation that worked for me to avoid having a transparent background in a bitmap that came from a Pdf.

int pageCount = renderer.getPageCount();
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
    PdfRenderer.Page page = renderer.openPage(pageIndex);
    Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888);
    // Paint bitmap before rendering
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bitmap, 0, 0, null);
    // Render Pdf page into bitmap
    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    page.close();
    bitmaps.add(bitmap);
}
like image 40
Sebastian Avatar answered Nov 15 '22 07:11

Sebastian