Uses pdfium library from AOSP
The demo app (for not modified lib) is here
Forked for use with AndroidPdfViewer project.
API is highly compatible with original version, only additional methods were created.
- Updated Pdfium library to 7.1.2_r36
- Changed
gnustl_statictoc++_shared - Update Gradle plugins
- Update compile SDK and support library to 26
- Change minimum SDK to 14
- Add support for mips64
Add to build.gradle:
compile 'com.github.barteksc:pdfium-android:1.9.0'
Library is available in jcenter and Maven Central repositories.
Version 1.8.0 added method for getting page size - PdfiumCore#getPageSize(...).
It is important to note, that this method does not require page to be opened. However, there are also
old PdfiumCore#getPageWidth(...), PdfiumCore#getPageWidthPoint(...), PdfiumCore#getPageHeight()
and PdfiumCore#getPageHeightPoint() which require page to be opened.
This inconsistency will be resolved in next major version, which aims to redesign API.
Version 1.8.0 introduces PdfiumCore#getPageLinks(PdfDocument, int) method, which allows to get list
of links from given page. Links are returned as List of type PdfDocument.Link.
PdfDocument.Link holds destination page (may be null), action URI (may be null or empty)
and link bounds in document page coordinates. To map page coordinates to screen coordinates you may use
PdfiumCore#mapRectToDevice(...). See PdfiumCore#mapPageCoordsToDevice(...) for parameters description.
Sample usage:
PdfiumCorecore = ...;
PdfDocumentdocument = ...;
intpageIndex = 0;
core.openPage(document, pageIndex);
List<PdfDocument.Link> links = core.getPageLinks(document, pageIndex);
for (PdfDocument.Linklink : links) {
RectFmappedRect = core.mapRectToDevice(document, pageIndex, ..., link.getBounds())
if (clickedArea(mappedRect)) {
Stringuri = link.getUri();
if (link.getDestPageIdx() != null) {
// jump to page
} elseif (uri != null && !uri.isEmpty()) {
// open URI using Intent
}
}
}voidopenPdf() {
ImageViewiv = (ImageView) findViewById(R.id.imageView);
ParcelFileDescriptorfd = ...;
intpageNum = 0;
PdfiumCorepdfiumCore = newPdfiumCore(context);
try {
PdfDocumentpdfDocument = pdfiumCore.newDocument(fd);
pdfiumCore.openPage(pdfDocument, pageNum);
intwidth = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
intheight = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);
// ARGB_8888 - best quality, high memory usage, higher possibility of OutOfMemoryError// RGB_565 - little worse quality, twice less memory usageBitmapbitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
width, height);
//if you need to render annotations and form fields, you can use//the same method above adding 'true' as last paramiv.setImageBitmap(bitmap);
printInfo(pdfiumCore, pdfDocument);
pdfiumCore.closeDocument(pdfDocument); // important!
} catch(IOExceptionex) {
ex.printStackTrace();
}
}
publicvoidprintInfo(PdfiumCorecore, PdfDocumentdoc) {
PdfDocument.Metameta = core.getDocumentMeta(doc);
Log.e(TAG, "title = " + meta.getTitle());
Log.e(TAG, "author = " + meta.getAuthor());
Log.e(TAG, "subject = " + meta.getSubject());
Log.e(TAG, "keywords = " + meta.getKeywords());
Log.e(TAG, "creator = " + meta.getCreator());
Log.e(TAG, "producer = " + meta.getProducer());
Log.e(TAG, "creationDate = " + meta.getCreationDate());
Log.e(TAG, "modDate = " + meta.getModDate());
printBookmarksTree(core.getTableOfContents(doc), "-");
}
publicvoidprintBookmarksTree(List<PdfDocument.Bookmark> tree, Stringsep) {
for (PdfDocument.Bookmarkb : tree) {
Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));
if (b.hasChildren()) {
printBookmarksTree(b.getChildren(), sep + "-");
}
}
}Go to PROJECT_PATH/src/main/jni and run command $ ndk-build.
This step may be executed only once, every future .aar build will use generated libs.