Frequently Asked Question
Render bitmap with transparent background
Last Updated 11 months ago
To render a page into a bitmap maintaining the transparencies from the page itself, use the code below:
Document document = new Document(); PDFAssetStream m_asset_stream = new PDFAssetStream(); m_asset_stream.open(getAssets(), "transparent_mix.pdf"); document.OpenStream(m_asset_stream, ""); Global.mBitmap = Bitmap.createBitmap((int) (document.GetPageWidth(0)), (int) (document.GetPageHeight(0)), Bitmap.Config.ARGB_8888); Page page = document.GetPage(0); Global.mBitmap.eraseColor(0x00000000); page.RenderToBmp(Global.mBitmap, new Matrix( 1, -1, 0, document.GetPageHeight(0)));
// New BMP.MulAlpha() method is available starting from SDK 3.5beta6 BMP mBMP = new BMP(); Global.mBMP=mBMP; Global.mBMP.Create(Global.mBitmap); // MulAlpha is needed to renormalize RGB values in presence of alpha channel !0xFF Global.mBMP.MulAlpha();
Setting the ARGB32 color in the bold line you will alter the background your rendered page.
Then in the onDraw event draw the created bitmap over the original page.
canvas.drawBitmap(Global.mBitmap, page_x, page_y, null);
Note: the bitmap was created without considering the scale factor.