Skip to content
Guides

Text Reflow

The text content in a PDF is stored in a CPDFPage object associated with a specific page. The CPDFPage class can be used to retrieve text information from a PDF page, such as individual characters, single words, text content within a specified character range, or within a defined boundary.

The steps for text reflow are divided into two parts:

  1. Obtain text boundaries on the page through selection.
  2. Retrieve text from the CPDFTextSelection array and rearrange it to fit the device screen size for displaying the same layout.

Here is an example code for obtaining text boundaries on the page through selection:

java
RectF size = readerView.getPageNoZoomSize(1);
CPDFPage pdfPage = readerView.getPDFDocument().pageAtIndex(1);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, size.width(), size.height());
selectRect = pdfPage.convertRectFromPage(readerView.isCropMode(), size.width(), size.height(), selectRect);
CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);
kotlin
val size = readerView.getPageNoZoomSize(1)
val pdfPage = readerView.pdfDocument.pageAtIndex(1)
val pdfTextPage = pdfPage.textPage
var selectRect: RectF? = RectF(0F, 0F, size.width(), size.height())
selectRect = pdfPage.convertRectFromPage(readerView.isCropMode, size.width(), size.height(), selectRect)
val textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect)

Here is an example code for retrieving text from the CPDFTextSelection array:

java
int len = textSelectionArr.length;
for (int i = 0; i < len; i++) {
    CPDFTextSelection textSelection = textSelectionArr[i];
    if (textSelection == null) {
        continue;
    }
    String text = pdfTextPage.getText(textSelection.getTextRange());
}
kotlin
val textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect)
for (textSelection in textSelectionArr) {
  val text = pdfTextPage.getText(textSelection.textRange)
}