One of the most required function is to convert a single PDF file or a folder of files into the collection of bitmaps of each page.
RadaeePDF for Server and the code below satisfy the need!
The code below, that runs with RadaeePDF Master on Server and on Android, generates a subset of pages from a single file programmatically.
Each image is created as base resolution of 72 dpi and stored as PNG or JPEG file.
/**
* converts the given document into images.
*
* @param filePath the input document's path
* @param password the document's password (if-any)
* @param savePath the path to save the images into
* @param size the largest side(width or height), to be used to calculate the image size (0 to use original pdf's dimension)
* @param startIndex the index (0 to page count - 1) of the first page to generate.
* @param endIndex the index (0 to page count - 1 inclusive) of the last page to generate.
* @param type the type of the generated image, 0: jpeg, otherwise:png
* @return 0: for success
* -1:need input password
* -2:unknown encryption
* -3:damaged or invalid format
* -4:unsatisfied link error
* -10:access denied or invalid file path
* others:unknown error
*/
public int pdf2Image(String filePath, String password, String savePath, int size, int startIndex, int endIndex, int type) {
int result;
try {
Global.annotTransparencyColor = 0x00000000;
Document document = new Document();
result = document.Open(filePath, password);
if(result == 0) { //success
int numOfPages = document.GetPageCount();
if(startIndex < 0 || startIndex >= numOfPages) startIndex = 0;
if(endIndex <= 0 || endIndex >= numOfPages) endIndex = numOfPages - 1;
String mFileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1, filePath.toLowerCase().indexOf(".pdf"));
Bitmap bitmap;
//generate thumbnails
for(int i = startIndex ; i <= endIndex ; i++) {
float pageWidth = document.GetPageWidth(i);
float pageHeight = document.GetPageHeight(i);
int imageWidth , imageHeight;
if(pageWidth > pageHeight) { //set the given size, original size otherwise
imageWidth = size == 0 ? (int) pageWidth : size;
imageHeight = (int) (pageHeight * imageWidth / pageWidth);
} else {
imageHeight = size == 0 ? (int) pageHeight : size;
imageWidth = (int) (pageWidth * imageHeight / pageHeight);
}
float ratio = imageWidth / pageWidth;
float ratioy = imageHeight / pageHeight;
if( ratio > ratioy ) ratio = ratioy;
imageWidth = (int) (pageWidth * ratio);
imageHeight = (int) (pageHeight * ratio);
Page page = document.GetPage(i);
if(page != null) {
bitmap = Bitmap.createBitmap( imageWidth, imageHeight, Bitmap.Config.ARGB_8888 );
bitmap.eraseColor(Color.WHITE);
Matrix mat = new Matrix(ratio, -ratio, (imageWidth - pageWidth * ratio) / 2, (imageHeight + pageHeight * ratio) / 2);
page.RenderPrepare((Bitmap)null);
if(page.RenderToBmp(bitmap, mat)) {
String outputPath = savePath + File.separator + mFileName + "_" + i;
outputPath += type == 0 ? ".jpeg" : ".png";
FileOutputStream fos = new FileOutputStream(outputPath);
if(type == 0)
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
else
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
mat.Destroy();
bitmap.recycle();
page.Close();
}
}
}
document.Close();
} catch (Exception e) {
e.printStackTrace();
result = -100;
}
return result;
}
RadaeePDF SDK for Android, RadaeePDF Master SDK
Convert PDF to images and bitmaps (iOS)
Created : 2017-08-22 12:11:03, Last Modified : 2021-06-03 08:06:27