Objective-C Requires Chilkat v11.7.0+
Objective-C
Extract PDF Text to a StringBuilder and Save to a File
See more PDF Text Extraction Examples
Demonstrates ExtractTextSb, which writes the extracted text to a StringBuilder instead of returning it as a JSON string. Use it for large documents. The JSON output still receives the quality report.Chilkat Objective-C Downloads
#import <CkoPdf.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example is available at long_report.pdf
CkoPdf *pdf = [[CkoPdf alloc] init];
success = [pdf LoadFile: @"qa_data/pdf/long_report.pdf"];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d",@"Number of pages: ",[pdf.NumPages intValue]);
// ExtractTextSb takes the same operation and options as ExtractText, but the text
// goes to a StringBuilder. This avoids building a very large JSON string for
// documents with many pages.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
CkoStringBuilder *sbText = [[CkoStringBuilder alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [pdf ExtractTextSb: @"text" jsonIn: json sbOut: sbText jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",pdf.LastErrorText);
return;
}
NSLog(@"%@%d%@",@"Extracted ",[sbText.Length intValue],@" characters.");
// Save the text to a file.
success = [sbText WriteFile: @"qa_output/long_report.txt" charset: @"utf-8" emitBom: NO];
if (success == NO) {
NSLog(@"%@",sbText.LastErrorText);
return;
}
// The JSON output does not contain the text, but it still has the quality report.
NSLog(@"%@%@",@"Quality: ",[jsonOut StringOf: @"quality.rating"]);
NSLog(@"%@%@%@",@"Pages with no text (need OCR): [",[jsonOut StringOf: @"quality.pagesNeedingOcr"],@"]");
// Sample output:
// Number of pages: 3
// Extracted 4522 characters.
// Quality: good
// Pages with no text (need OCR): []