Knowledge Base - How to create a thumbnail from a page of a PDF file

This article shows how to create a thumbnail of a required page.

The code runs with RadaeePDF SDK on Android and Server and it's written in Java.

public Bitmap getThumbnailFromPage(String m_path, String m_pwd, int pageno, int thumbWidth, int thumbHeight)
{
Bitmap bmp = null;

Document doc = new Document();
if( doc.Open(m_path, m_pwd) == 0 )
{
Page page = doc.GetPage(pageno);
try
{
bmp = Bitmap.createBitmap( thumbWidth, thumbHeight, Bitmap.Config.ARGB_8888 );
bmp.eraseColor(0);
if( !page.RenderThumb(bmp) )
{
float w = doc.GetPageWidth(0);
float h = doc.GetPageHeight(0);
float ratiox = thumbWidth/w;
float ratioy = thumbHeight/h;
if( ratiox > ratioy ) ratiox = ratioy;
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setARGB(255, 255, 255, 255);
canvas.drawRect((thumbWidth - w * ratiox)/2, (thumbHeight - h * ratiox)/2,
(thumbWidth + w * ratiox)/2, (thumbHeight + h * ratiox)/2, paint);
Matrix mat = new Matrix( ratiox, -ratiox, (thumbWidth - w * ratiox)/2, (thumbHeight + h * ratiox)/2 );
page.RenderPrepare((Bitmap)null);
page.RenderToBmp(bmp, mat);
mat.Destroy();
bmp.recycle();
bmp = null;
}
}
catch(Exception e)
{ e.getMessage(); }
page.Close();
doc.Close();
}
return bmp;
}
Applies To

RadaeePDF SDK for Android, RadaeePDF Master SDK

Details

Created : 2017-10-18 10:02:31, Last Modified : 2021-05-27 13:39:55