Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.4k points)

I realized that Android has no built-in method of displaying PDF files.

How can I render a PDF file using Java on Android?

1 Answer

0 votes
by (46k points)

Since API Level 21 (Lollipop) Android provides a PdfRenderer class:

// Make a new renderer

 PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor());

 // let us just render all pages

 final int pageCount = renderer.getPageCount();

 for (int i = 0; i < pageCount; i++) {

     Page page = renderer.openPage(i);

     // say we render for showing on the screen

     page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);

     // do stuff with the bitmap

     // close the page

     page.close();

 }

 // close the renderer

 renderer.close();

For added erudition check the sample app.

For older APIs I promote Android PdfViewer library, it is pretty fast and easy to apply, authorized under Apache License 2.0:

pdfView.fromAsset(String)

  .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default

  .enableSwipe(true)

  .swipeHorizontal(false)

  .enableDoubletap(true)

  .defaultPage(0)

  .onDraw(onDrawListener)

  .onLoad(onLoadCompleteListener)

  .onPageChange(onPageChangeListener)

  .onPageScroll(onPageScrollListener)

  .onError(onErrorListener)

  .enableAnnotationRendering(false)

  .password(null)

  .scrollHandle(null)

  .load();

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jan 2, 2021 in Python by laddulakshana (16.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...