最近有一个需求,在不打开文档的情况下,为每个pdf生成一张缩略图,显示在界面上。那么怎么做呢?
相信大家对pdf进行开发时,都会用到一个框架:pdfview。
至于pdfview如何使用,我就不班门弄斧了,使用起来也非常简单,附上使用连接:http://www.mobile-open.com/2015/38396.html
接下来就是重点,怎么截图呢?关键代码如下:
PdfContext pdfContext = new PdfContext();
PdfDocument pdfDocument = (PdfDocument) pdfContext .openDocument(path);//path为要截图的pdf的路径,String类型
PdfPage pdfPage = (PdfPage)pdfDocument.getPage(page);//page为要截取的页数,int型
RectF rf = new RectF();//使用一个矩形去截图
rf.bottom = rf.right = (float)1.0;//上方与左方不指定,下方与右方指定为1.0的位置,即截下整幅图
Bitmap bitmap = pdfPage.renderBitmap(pdfPage.getWidth(), pdfPage.getHeight(), rf); //这个bitmap就是我们要截的图
好了,代码就是这么多,其实也是非常简单,但是因为对api不熟的话也不好找。上面的注释也是比较详细,就不一一解释了。