Package 

Class PdfiumCore

  • 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 PdfiumCore instance.

    • 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.

    • Constructor Detail

      • PdfiumCore

        PdfiumCore()
    • Method Detail

      • 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.

      • 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.
      • 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 page
        startX - 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.
      • 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.
      • 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 case
        matchWholeWord - match the whole word
      • 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 LogWriter interface.

        Parameters:
        logWriter - The LogWriter instance to use for logging.