-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public final class PdfiumCore implements Closeable
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:
Create a new
PdfiumCoreinstance.Open a PDF document using
newDocument().Perform desired operations, such as rendering pages or extracting text.
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classPdfiumCore.Companion
-
Field Summary
Fields Modifier and Type Field Description private final IntegerpageCountprivate final IntegercurrentDpi
-
Constructor Summary
Constructors Constructor Description PdfiumCore()
-
Method Summary
Modifier and Type Method Description final IntegergetPageCount()final IntegergetCurrentDpi()final UnitnewDocument(ParcelFileDescriptor fd)Create new document from file final LongnewDocument(ParcelFileDescriptor fd, String password)Create new document from file with password final LongnewDocument(ByteArray bytes, String password)Create new document from file with password final LongopenPage(Integer pageIndex)Open page and store native pointer final LongArrayopenPage(Integer fromIndex, Integer toIndex)Open range of pages and store native pointers final IntegergetPageWidth(Integer index)Get page width in pixels. final IntegergetPageHeight(Integer index)Get page height in pixels. final IntegergetPageWidthPoint(Integer index)Get page width in PostScript points (1/72th of an inch). final IntegergetPageHeightPoint(Integer index)Get page height in PostScript points (1/72th of an inch). final SizegetPageSize(Integer index)Get size of page in pixels. final IntegergetPageRotation(Integer index)Get the rotation of page<br></br> final UnitrenderPage(Surface surface, Integer pageIndex, Integer startX, Integer startY, Integer drawSizeX, Integer drawSizeY, Boolean renderAnnot)Render page fragment on Surface. final UnitrenderPageBitmap(Bitmap bitmap, Integer pageIndex, Integer startX, Integer startY, Integer drawSizeX, Integer drawSizeY, Boolean renderAnnot)Render page fragment on Bitmap. final UnitclosePage(Integer pageIndex)Release native page resources of given page final IntegergetLastError(Long docPtr)Gets the last occurred error code. final StringgetErrorMessage(Integer errorCode)Gets the error message from a specific error code. Unitclose()Closes the PDF document and releases resources. final MetagetDocumentMeta()Get metadata for given document final List<Bookmark>getTableOfContents()Get table of contents (bookmarks) for given document final List<Link>getPageLinks(Integer pageIndex)Get all links from given page final PointFmapDeviceCoordinateToPage(Integer pageIndex, Integer startX, Integer startY, Integer sizeX, Integer sizeY, Integer rotate, Integer deviceX, Integer deviceY)Convert the screen coordinates of a point to page coordinates. final RectFmapPageCoordinateToDevice(Integer pageIndex, Integer startX, Integer startY, Integer sizeX, Integer sizeY, Integer rotate, RectF coords)final LongprepareTextInfo(Integer pageIndex)Prepare information about all characters in a page. final LongArrayprepareTextInfo(Integer fromIndex, Integer toIndex)Prepare information about all characters in a range of pages. final UnitreleaseTextInfo(Integer pageIndex)Release all resources allocated for a text page information structure. final UnitreleaseTextInfo(Integer fromIndex, Integer toIndex)Release all resources allocated for a text page information structure. final IntegercountCharactersOnPage(Integer pageIndex)final StringextractCharacters(Integer pageIndex, Integer startIndex, Integer length)Extract unicode text string from the page. final CharacterextractCharacter(Integer pageIndex, Integer index)Get Unicode of a character in a page. final RectFmeasureCharacterBox(Integer pageIndex, Integer index)Get bounding box of a particular character. final IntegergetCharacterIndex(Integer pageIndex, Double x, Double y, Double xTolerance, Double yTolerance)Get the index of a character at or nearby a certain position on the page final IntegercountTextRect(Integer pageIndex, Integer charIndex, Integer count)Count number of rectangular areas occupied by a segment of texts. final RectFgetTextRect(Integer pageIndex, Integer rectIndex)Get a rectangular area from the result generated by FPDFText_CountRects. final StringextractText(Integer pageIndex, RectF rect)Extract unicode text within a rectangular boundary on the page. final TextSearchContextnewPageSearch(Integer pageIndex, String query, Boolean matchCase, Boolean matchWholeWord)A handle class for the search context. final UnitsetCurrentDpi(Integer d)final BooleanhasPage(Integer index)final BooleanhasTextPage(Integer index)final BooleanhasSearchHandle(Integer index)final UnitsetLogWriter(LogWriter logWriter)Sets the LogWriter instance to be used for logging. -
-
Method Detail
-
getPageCount
final Integer getPageCount()
-
getCurrentDpi
final Integer getCurrentDpi()
-
newDocument
final Unit newDocument(ParcelFileDescriptor fd)
Create new document from file
-
newDocument
@Synchronized() final Long newDocument(ParcelFileDescriptor fd, String password)
Create new document from file with password
-
newDocument
@Synchronized() final Long newDocument(ByteArray bytes, String password)
Create new document from file with password
-
openPage
final LongArray openPage(Integer fromIndex, Integer toIndex)
Open range of pages and store native pointers
-
getPageWidth
final Integer getPageWidth(Integer index)
Get page width in pixels. <br></br> This method requires page to be opened.
-
getPageHeight
final Integer getPageHeight(Integer index)
Get page height in pixels. <br></br> This method requires page to be opened.
-
getPageWidthPoint
final Integer getPageWidthPoint(Integer index)
Get page width in PostScript points (1/72th of an inch).<br></br> This method requires page to be opened.
-
getPageHeightPoint
final Integer getPageHeightPoint(Integer index)
Get page height in PostScript points (1/72th of an inch).<br></br> This method requires page to be opened.
-
getPageSize
final Size getPageSize(Integer index)
Get size of page in pixels.<br></br> This method does not require given page to be opened.
-
getPageRotation
final Integer getPageRotation(Integer index)
Get the rotation of page<br></br>
-
renderPage
final Unit renderPage(Surface surface, Integer pageIndex, Integer startX, Integer startY, Integer drawSizeX, Integer drawSizeY, Boolean renderAnnot)
Render page fragment on Surface.<br></br> Page must be opened before rendering.
-
renderPageBitmap
final Unit renderPageBitmap(Bitmap bitmap, Integer pageIndex, Integer startX, Integer startY, Integer drawSizeX, Integer drawSizeY, Boolean renderAnnot)
Render page fragment on Bitmap. This method allows to render annotations.<br></br> Page must be opened before rendering.
For more info see PdfiumCore.renderPageBitmap
-
getLastError
@Synchronized() final Integer getLastError(Long docPtr)
Gets the last occurred error code. Should be called after a native function has failed.
- Parameters:
docPtr- The document pointer.
-
getErrorMessage
@Synchronized() final String getErrorMessage(Integer errorCode)
Gets the error message from a specific error code.
- Parameters:
errorCode- The error code.
-
close
@Synchronized() Unit close()
Closes the PDF document and releases resources.
-
getDocumentMeta
final Meta getDocumentMeta()
Get metadata for given document
-
getTableOfContents
final List<Bookmark> getTableOfContents()
Get table of contents (bookmarks) for given document
-
getPageLinks
final List<Link> getPageLinks(Integer pageIndex)
Get all links from given page
-
mapDeviceCoordinateToPage
final PointF mapDeviceCoordinateToPage(Integer pageIndex, Integer startX, Integer startY, Integer sizeX, Integer sizeY, Integer rotate, Integer deviceX, Integer deviceY)
Convert the screen coordinates of a point to page coordinates.
The page coordinate system has its origin at the left-bottom corner of the page, with the X-axis on the bottom going to the right, and the Y-axis on the left side going up.
NOTE: this coordinate system can be altered when you zoom, scroll, or rotate a page, however, a point on the page should always have the same coordinate values in the page coordinate system.
The device coordinate system is device dependent. For screen device, its origin is at the left-top corner of the window. However this origin can be altered by the Windows coordinate transformation utilities.
You must make sure the start_x, start_y, size_x, size_y and rotate parameters have exactly same values as you used in the FPDF_RenderPage() function call.
- Parameters:
pageIndex- index of pagestartX- Left pixel position of the display area in device coordinates.startY- Top pixel position of the display area in device coordinates.sizeX- Horizontal size (in pixels) for displaying the page.sizeY- Vertical size (in pixels) for displaying the page.rotate- Page orientation: 0 (normal) 1 (rotated 90 degrees clockwise) 2 (rotated 180 degrees) 3 (rotated 90 degrees counter-clockwise)deviceX- X value in device coordinates to be converted.deviceY- Y value in device coordinates to be converted.
-
mapPageCoordinateToDevice
final RectF mapPageCoordinateToDevice(Integer pageIndex, Integer startX, Integer startY, Integer sizeX, Integer sizeY, Integer rotate, RectF coords)
-
prepareTextInfo
final Long prepareTextInfo(Integer pageIndex)
Prepare information about all characters in a page. Application must call FPDFText_ClosePage to release the text page information.
- Parameters:
pageIndex- index of page.
-
prepareTextInfo
final LongArray prepareTextInfo(Integer fromIndex, Integer toIndex)
Prepare information about all characters in a range of pages. Application must call FPDFText_ClosePage to release the text page information.
- Parameters:
fromIndex- start index of page.toIndex- end index of page.
-
releaseTextInfo
final Unit releaseTextInfo(Integer pageIndex)
Release all resources allocated for a text page information structure.
- Parameters:
pageIndex- index of page.
-
releaseTextInfo
final Unit releaseTextInfo(Integer fromIndex, Integer toIndex)
Release all resources allocated for a text page information structure.
- Parameters:
fromIndex- start index of page.toIndex- end index of page.
-
countCharactersOnPage
final Integer countCharactersOnPage(Integer pageIndex)
-
extractCharacters
final String extractCharacters(Integer pageIndex, Integer startIndex, Integer length)
Extract unicode text string from the page.
- Parameters:
pageIndex- index of page.startIndex- Index for the start characters.length- Number of characters to be extracted.
-
extractCharacter
final Character extractCharacter(Integer pageIndex, Integer index)
Get Unicode of a character in a page.
- Parameters:
pageIndex- index of page.index- Zero-based index of the character.
-
measureCharacterBox
final RectF measureCharacterBox(Integer pageIndex, Integer index)
Get bounding box of a particular character.
- Parameters:
pageIndex- index of page.index- Zero-based index of the character.
-
getCharacterIndex
final Integer getCharacterIndex(Integer pageIndex, Double x, Double y, Double xTolerance, Double yTolerance)
Get the index of a character at or nearby a certain position on the page
- Parameters:
pageIndex- index of page.x- X position in PDF "user space".y- Y position in PDF "user space".xTolerance- An x-axis tolerance value for character hit detection, in point unit.yTolerance- A y-axis tolerance value for character hit detection, in point unit.
-
countTextRect
final Integer countTextRect(Integer pageIndex, Integer charIndex, Integer count)
Count number of rectangular areas occupied by a segment of texts.
This function, along with FPDFText_GetRect can be used by applications to detect the position on the page for a text segment, so proper areas can be highlighted or something. FPDFTEXT will automatically merge small character boxes into bigger one if those characters are on the same line and use same font settings.
- Parameters:
pageIndex- index of page.charIndex- Index for the start characters.count- Number of characters.
-
getTextRect
final RectF getTextRect(Integer pageIndex, Integer rectIndex)
Get a rectangular area from the result generated by FPDFText_CountRects.
- Parameters:
pageIndex- index of page.rectIndex- Zero-based index for the rectangle.
-
extractText
final String extractText(Integer pageIndex, RectF rect)
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.
- Parameters:
pageIndex- index of page.rect- the text rectangle to extract.
-
newPageSearch
final TextSearchContext newPageSearch(Integer pageIndex, String query, Boolean matchCase, Boolean matchWholeWord)
A handle class for the search context. stopSearch must be called to release this handle.
- Parameters:
pageIndex- index of page.query- A unicode match pattern.matchCase- match casematchWholeWord- match the whole word
-
setCurrentDpi
final Unit setCurrentDpi(Integer d)
-
hasTextPage
final Boolean hasTextPage(Integer index)
-
hasSearchHandle
final Boolean hasSearchHandle(Integer index)
-
setLogWriter
final Unit setLogWriter(LogWriter logWriter)
Sets the LogWriter instance to be used for logging.
This function allows you to configure the logger with a custom implementation of the
LogWriterinterface.- Parameters:
logWriter- The LogWriter instance to use for logging.
-
-
-
-