Guides
PDF Page to Image
You can render a specific page to a Bitmap for saving, previewing, or displaying in an ImageView.
There are usually two ways to do this:
- Use
renderPagefor manual rendering when you need to control the render region, background color, annotations, and form rendering. - Use
Glidewhen you want to generate and display PDF thumbnails more quickly.
The following examples show how to render the first page of a document as an image.
Render a Page with renderPage
This approach is suitable when you need finer control over output size, render region, and render parameters.
java
// Render the PDF page in a background thread
new Thread(() -> {
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = cpdfPage.getSize();
int pageWidth = (int) pageSize.width();
int pageHeight = (int) pageSize.height();
int patchX = 0;
int patchY = 0;
int patchW = pageWidth;
int patchH = pageHeight;
int bgColor = 0xFFFFFFFF;
boolean isDrawAnnot = true;
boolean isDrawForm = true;
Bitmap bitmap = Bitmap.createBitmap(patchW, patchH, Bitmap.Config.RGB_565);
float scale = (float) pageWidth / pageSize.width();
boolean renderResult = cpdfPage.renderPage(
bitmap,
scale,
pageWidth,
pageHeight,
patchX,
patchY,
patchW,
patchH,
bgColor,
255,
0,
isDrawAnnot,
isDrawForm
);
runOnUiThread(() -> {
if (renderResult && bitmap != null) {
ivImage.setImageBitmap(bitmap);
}
});
}).start();kotlin
Thread {
val cpdfPage = document.pageAtIndex(0)
val pageSize = cpdfPage.size
val pageWidth = pageSize.width().toInt()
val pageHeight = pageSize.height().toInt()
val patchX = 0
val patchY = 0
val patchW = pageWidth
val patchH = pageHeight
val bgColor = 0xFFFFFFFF.toInt()
val isDrawAnnot = true
val isDrawForm = true
val bitmap = Bitmap.createBitmap(patchW, patchH, Bitmap.Config.RGB_565)
val scale = pageWidth.toFloat() / pageSize.width()
val renderResult = cpdfPage.renderPage(
bitmap,
scale,
pageWidth,
pageHeight,
patchX,
patchY,
patchW,
patchH,
bgColor,
255,
0,
isDrawAnnot,
isDrawForm
)
runOnUiThread {
if (renderResult) {
ivImage.setImageBitmap(bitmap)
}
}
}.start()When using renderPage, pay attention to the following:
- Run rendering code on a background thread to avoid blocking the main thread.
- The
Bitmapsize and format directly affect image quality and memory usage. - If you only need thumbnails for display, the
Glideapproach is usually more straightforward.
Render Thumbnails with Glide
If ComPDFKit_Tools is integrated and the Glide dependency is available, you can render PDF thumbnails directly with Glide.
In the following examples, pageIndex, uri, renderImageWidth, and renderImageHeight should be provided based on your actual scenario.
java
// 1. Render from CPDFDocument and page index
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromDocument(document, pageIndex))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage);
// 2. Render from PDF file path
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromFile("filePath"))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage);
// 3. Render from Uri
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromUri(uri))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage);
// 4. Custom CPDFDocumentPageWrapper rendering
CPDFDocumentPageWrapper pageWrapper = new CPDFDocumentPageWrapper(document, pageIndex);
pageWrapper.setDrawAnnotation(true);
pageWrapper.setDrawForms(true);
pageWrapper.setBackgroundColor(0xFFFFFFFF);
CPDFWrapper wrapper = new CPDFWrapper(pageWrapper);
Glide.with(this)
.load(wrapper)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.override((int) renderImageWidth, (int) renderImageHeight)
.into(ivImage);kotlin
// 1. Render from CPDFDocument and page index
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromDocument(document, pageIndex))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage)
// 2. Render from PDF file path
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromFile("filePath"))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage)
// 3. Render from Uri
Glide.with(this)
.asBitmap()
.load(CPDFWrapper.fromUri(uri))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.into(ivImage)
// 4. Custom CPDFDocumentPageWrapper rendering
val pageWrapper = CPDFDocumentPageWrapper(document, pageIndex).apply {
drawAnnotation = true
drawForms = true
backgroundColor = 0xFFFFFFFF.toInt()
}
val wrapper = CPDFWrapper(pageWrapper)
Glide.with(this)
.load(wrapper)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.override(renderImageWidth.toInt(), renderImageHeight.toInt())
.into(ivImage)