Page Text Extraction
ComPDF Flutter 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.
final CPDFPage page = document.pageAtIndex(0);
final String text = await page.getAllText();
print('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.
final CPDFPage page = document.pageAtIndex(0);
const CPDFRectF rect = CPDFRectF(
left: 40,
top: 760,
right: 560,
bottom: 680,
);
final String text = await page.getTextInRect(rect);
print('Text in rectangle: $text');The rect parameter uses the CPDFRectF structure. The values are page coordinates:
const CPDFRectF rect = CPDFRectF(
left: 40,
top: 760,
right: 560,
bottom: 680,
);Get Text Lines From a Page
Use getTextLines() to retrieve all text lines from the current page.
final CPDFPage page = document.pageAtIndex(0);
final List<CPDFTextLine> lines = await page.getTextLines();
print('Line count: ${lines.length}');Each text line is represented by CPDFTextLine:
class CPDFTextLine {
final int pageIndex;
final int lineIndex;
final int location;
final int length;
final CPDFRectF rect;
}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. It uses the same CPDFRectF format accepted by CPDFReaderWidgetController.setDisplayPageIndex(rectList: ...) |
Get Text From a Specific Line
Use getTextByLine(line) to get the actual text content for a text line.
final CPDFPage page = document.pageAtIndex(0);
final List<CPDFTextLine> lines = await page.getTextLines();
if (lines.isNotEmpty) {
final String firstLineText = await page.getTextByLine(lines.first);
print('First line: $firstLineText');
}The line should come from getTextLines() on the same page. Passing a line from another page to the current page returns a platform error.
Highlight a Text Line in CPDFReaderWidget
When you use CPDFReaderWidget, you can pass CPDFTextLine.rect to setDisplayPageIndex(rectList:) to highlight the line in the reader view.
final int pageIndex = await controller.getCurrentPageIndex();
final CPDFPage page = controller.document.pageAtIndex(pageIndex);
final List<CPDFTextLine> lines = await page.getTextLines();
if (lines.isNotEmpty) {
final CPDFTextLine firstLine = lines.first;
final String text = await page.getTextByLine(firstLine);
await controller.setDisplayPageIndex(
pageIndex: firstLine.pageIndex,
rectList: [firstLine.rect],
);
print('Highlighted text: $text');
}This is useful when you need to compare extracted text with its position on the page.
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 'package:compdfkit_flutter/document/cpdf_document.dart';
import 'package:compdfkit_flutter/page/cpdf_page.dart';
import 'package:compdfkit_flutter/page/cpdf_text_line.dart';
import 'package:compdfkit_flutter/util/cpdf_rectf.dart';
Future<void> extractPageText(String filePath) async {
final CPDFDocument document = await CPDFDocument.createInstance();
await document.open(filePath);
final CPDFPage page = document.pageAtIndex(0);
final String allText = await page.getAllText();
const CPDFRectF rect = CPDFRectF(
left: 40,
top: 760,
right: 560,
bottom: 680,
);
final String rectText = await page.getTextInRect(rect);
final List<CPDFTextLine> lines = await page.getTextLines();
final String firstLineText =
lines.isNotEmpty ? await page.getTextByLine(lines.first) : '';
print('All text length: ${allText.length}');
print('Rectangle text: $rectText');
print('Line count: ${lines.length}');
print('First line: $firstLineText');
}Notes
- 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.CPDFTextLine.rectcan be passed directly toCPDFReaderWidgetController.setDisplayPageIndex(rectList: ...)to highlight the corresponding line.getTextByLine(line)should use a line returned bygetTextLines()from the same page.- Invalid page indexes return a platform error.
- If you need keyword search, continue using the existing
CPDFTextSearcher.