Page Text Extraction
ComPDF React Native SDK provides page text APIs for extracting text content from a PDF page. You can get all text from a page, extract text inside a rectangle, retrieve text lines, and get the text content for a specific line.
Get All Text From a Page
Use getAllText() to extract all text from the current page.
const page = pdfReaderRef.current?._pdfDocument.pageAtIndex(0);
const text = await page?.getAllText();
console.log("Page text:", text);If the page has no text layer, such as a scanned image-only PDF, the result is usually an empty string.
Get Text Inside a Rectangle
Use getTextInRect(rect) to extract text inside a rectangle on the current page.
const page = pdfReaderRef.current?._pdfDocument.pageAtIndex(0);
const text = await page?.getTextInRect({
left: 40,
top: 120,
right: 300,
bottom: 180,
});
console.log("Text in rectangle:", text);The rect parameter uses the CPDFRectF structure. The values are page coordinates:
type CPDFRectF = {
left: number;
top: number;
right: number;
bottom: number;
};Get Text Lines From a Page
Use getTextLines() to retrieve all text lines from the current page.
const page = pdfReaderRef.current?._pdfDocument.pageAtIndex(0);
const lines = await page?.getTextLines();
console.log("Line count:", lines?.length ?? 0);Each text line is represented by CPDFTextLine:
type CPDFTextLine = {
pageIndex: number;
lineIndex: number;
location: number;
length: number;
rect: CPDFRectF;
};Field description:
| Field | Description |
|---|---|
pageIndex | The zero-based page index where the line is located |
lineIndex | The zero-based line index in the page line collection |
location | The start character position of the line in the page text |
length | The character length of the line |
rect | The line bounding rectangle in page coordinates |
Get Text From a Specific Line
Use getTextByLine(line) to get the actual text content for a text line.
const page = pdfReaderRef.current?._pdfDocument.pageAtIndex(0);
const lines = await page?.getTextLines();
if (page && lines && lines.length > 0) {
const firstLineText = await page.getTextByLine(lines[0]);
console.log("First line:", firstLineText);
}The line should come from getTextLines() on the same page. Passing a line from another page to the current page returns an error.
Complete Example
The following example extracts all page text, text inside a rectangle, the page line count, and the text of the first line.
import { useRef } from "react";
import { Button } from "react-native";
import { CPDFReaderView } from "@compdfkit_pdf_sdk/react_native";
export default function PageTextExample() {
const pdfReaderRef = useRef<CPDFReaderView>(null);
const extractPageText = async () => {
const page = pdfReaderRef.current?._pdfDocument.pageAtIndex(0);
if (!page) {
return;
}
const allText = await page.getAllText();
const rectText = await page.getTextInRect({
left: 40,
top: 120,
right: 300,
bottom: 180,
});
const lines = await page.getTextLines();
const firstLineText =
lines.length > 0 ? await page.getTextByLine(lines[0]) : "";
console.log("All text length:", allText.length);
console.log("Rectangle text:", rectText);
console.log("Line count:", lines.length);
console.log("First line:", firstLineText);
};
return (
<>
<CPDFReaderView
ref={pdfReaderRef}
document="file:///path/to/sample.pdf"
style={{ flex: 1 }}
/>
<Button title="Extract Text" onPress={extractPageText} />
</>
);
}Notes
- The page text APIs require a mounted
CPDFReaderView. Call them after the document has loaded. - Scanned PDFs or PDFs without a text layer may return an empty string or an empty array.
getTextInRect(rect)andCPDFTextLine.rectuse page coordinates, not screen coordinates.getTextByLine(line)should use a line returned bygetTextLines()from the same page.- If you need keyword search, continue using the existing
CPDFTextSearcher.