Text extraction
Foxit PDF SDK for iOS provides text extraction, selection, search, and retrieval via Core SDK. Page text is organized in FSTextPage, one per page.
FSTextPage is also used for:
- Text search — construct
FSTextSearchfromFSTextPage. - Hyperlinks — construct
FSPageTextLinksfromFSTextPage. - Selection highlights — compute text regions from selection start/end.
Example: extract all page text
objc
#import <FoxitRDK/FSPDFObjC.h>
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];
FSPDFPage *page = [doc getPage:0];
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:YES];
FSTextPage *textPage = [[FSTextPage alloc] initWithPage:page flags:FSTextPageParseTextNormal];
NSString *fullText = [textPage getChars:0 count:-1];
NSLog(@"页面文本: %@", fullText);Example: text rects from selection
Compute text regions from selection endpoints for highlight or range markup:
objc
- (NSArray<NSValue *> *)getTextRectsBySelectionOnPage:(FSPDFPage *)page
startPos:(FSPointF *)startPos
endPos:(FSPointF *)endPos {
FSTextPage *textPage = [[FSTextPage alloc] initWithPage:page
flags:FSTextPageParseTextNormal];
if (!textPage) return nil;
int startIndex = [textPage getIndexAtPos:[startPos getX] y:[startPos getY] tolerance:5];
int endIndex = [textPage getIndexAtPos:[endPos getX] y:[endPos getY] tolerance:5];
if (startIndex > endIndex) {
int temp = startIndex;
startIndex = endIndex;
endIndex = temp;
}
int count = [textPage getTextRectCount:startIndex count:endIndex - startIndex];
if (count <= 0) return nil;
NSMutableArray *rects = [NSMutableArray array];
for (int i = 0; i < count; i++) {
FSRectF *rect = [textPage getTextRect:i];
if (rect) {
// PDF coordinates; convert to device coordinates before display
[rects addObject:[NSValue valueWithCGRect:CGRectMake(
[rect getLeft], [rect getTop],
[rect getRight] - [rect getLeft],
[rect getTop] - [rect getBottom])]];
}
}
return rects;
}API reference
See the API reference for FSTextPage.