PdfiumCore

Core class for interacting with the Pdfium library.

This class provides a Java interface to the native Pdfium library, allowing you to open, read, render, and interact with PDF documents.

Key functionalities:

  • Opening PDF documents from files or byte arrays.

  • Retrieving document metadata, such as title, author, and page count.

  • Accessing and navigating through the document's table of contents (bookmarks).

  • Rendering pages to surfaces or bitmaps for display.

  • Extracting text from pages, including character positions and bounding boxes.

  • Searching for text within pages.

  • Handling annotations (limited functionality).

  • Managing native resources and closing documents.

Usage:

  1. Create a new PdfiumCore instance.

  2. Open a PDF document using newDocument().

  3. Perform desired operations, such as rendering pages or extracting text.

  4. Close the document using closeDocument() to release native resources.

Example:

PdfiumCore pdfiumCore = new PdfiumCore();
try (ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)) {
pdfiumCore.newDocument(fd);
int pageCount = pdfiumCore.getPageCount();
// ... perform operations on the document ...
} finally {
pdfiumCore.closeDocument();
}

Note: This class interacts with native code through JNI. Ensure that the necessary native libraries (pdfium and pdfium_jni) are loaded before using this class.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard

Get metadata for given document

Link copied to clipboard

Get total number of pages in document

Functions

Link copied to clipboard
open override fun close()

Closes the PDF document and releases resources.

Link copied to clipboard
fun closePage(pageIndex: Int)

Release native page resources of given page

Link copied to clipboard
fun countCharactersOnPage(pageIndex: Int): Int
Link copied to clipboard
fun countTextRect(pageIndex: Int, charIndex: Int, count: Int): Int

Count number of rectangular areas occupied by a segment of texts.

Link copied to clipboard
fun extractCharacter(pageIndex: Int, index: Int): Char

Get Unicode of a character in a page.

Link copied to clipboard
fun extractCharacters(pageIndex: Int, startIndex: Int, length: Int): String?

Extract unicode text string from the page.

Link copied to clipboard
fun extractText(pageIndex: Int, rect: RectF): String?

Extract unicode text within a rectangular boundary on the page. If the buffer is too small, as much text as will fit is copied into it.

Link copied to clipboard
fun getCharacterIndex(pageIndex: Int, x: Double, y: Double, xTolerance: Double, yTolerance: Double): Int

Get the index of a character at or nearby a certain position on the page

Link copied to clipboard

Gets the error message from a specific error code.

Link copied to clipboard

Gets the last occurred error code. Should be called after a native function has failed.

Link copied to clipboard
fun getPageHeight(index: Int): Int

Get page height in pixels.

This method requires page to be opened.

Link copied to clipboard

Get page height in PostScript points (1/72th of an inch).

This method requires page to be opened.

Link copied to clipboard
fun getPageLinks(pageIndex: Int): List<Link>

Get all links from given page

Link copied to clipboard
fun getPageRotation(index: Int): Int

Get the rotation of page

Link copied to clipboard
fun getPageSize(index: Int): Size

Get size of page in pixels.

This method does not require given page to be opened.

Link copied to clipboard
fun getPageWidth(index: Int): Int

Get page width in pixels.

This method requires page to be opened.

Link copied to clipboard

Get page width in PostScript points (1/72th of an inch).

This method requires page to be opened.

Link copied to clipboard

Get table of contents (bookmarks) for given document

Link copied to clipboard
fun getTextRect(pageIndex: Int, rectIndex: Int): RectF?

Get a rectangular area from the result generated by FPDFText_CountRects.

Link copied to clipboard
fun hasPage(index: Int): Boolean
Link copied to clipboard
Link copied to clipboard
fun hasTextPage(index: Int): Boolean
Link copied to clipboard
fun mapDeviceCoordinateToPage(pageIndex: Int, startX: Int, startY: Int, sizeX: Int, sizeY: Int, rotate: Int, deviceX: Int, deviceY: Int): PointF

Convert the screen coordinates of a point to page coordinates.

Link copied to clipboard
fun mapPageCoordinateToDevice(pageIndex: Int, startX: Int, startY: Int, sizeX: Int, sizeY: Int, rotate: Int, coords: RectF): RectF
Link copied to clipboard
fun measureCharacterBox(pageIndex: Int, index: Int): RectF?

Get bounding box of a particular character.

Link copied to clipboard

Create new document from file

fun newDocument(bytes: ByteArray, password: String?): Long

Create new document from file with password

Link copied to clipboard
fun newPageSearch(pageIndex: Int, query: String, matchCase: Boolean, matchWholeWord: Boolean): TextSearchContext

A handle class for the search context. stopSearch must be called to release this handle.

Link copied to clipboard
fun openPage(pageIndex: Int): Long

Open page and store native pointer

fun openPage(fromIndex: Int, toIndex: Int): LongArray

Open range of pages and store native pointers

Link copied to clipboard
fun prepareTextInfo(pageIndex: Int): Long

Prepare information about all characters in a page. Application must call FPDFText_ClosePage to release the text page information.

fun prepareTextInfo(fromIndex: Int, toIndex: Int): LongArray

Prepare information about all characters in a range of pages. Application must call FPDFText_ClosePage to release the text page information.

Link copied to clipboard
fun releaseTextInfo(pageIndex: Int)
fun releaseTextInfo(fromIndex: Int, toIndex: Int)

Release all resources allocated for a text page information structure.

Link copied to clipboard
fun renderPage(surface: Surface, pageIndex: Int, startX: Int, startY: Int, drawSizeX: Int, drawSizeY: Int, renderAnnot: Boolean = false)

Render page fragment on Surface.

Page must be opened before rendering.

Link copied to clipboard
fun renderPageBitmap(bitmap: Bitmap, pageIndex: Int, startX: Int, startY: Int, drawSizeX: Int, drawSizeY: Int, renderAnnot: Boolean = false)

Render page fragment on Bitmap. This method allows to render annotations.

Page must be opened before rendering.

Link copied to clipboard
Link copied to clipboard
fun setLogWriter(logWriter: LogWriter)

Sets the LogWriter instance to be used for logging.