Perl Requires Chilkat v11.7.0+
Chilkat2 Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
# 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
my $pdf = Chilkat2::Pdf->new;
my $success = $pdf->LoadFile('qa_data/pdf/long_report.pdf');
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
print 'Number of pages: ', $pdf->NumPages, "\n";
# 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.
my $json = Chilkat2::JsonObject->new;
my $sbText = Chilkat2::StringBuilder->new;
my $jsonOut = Chilkat2::JsonObject->new;
$success = $pdf->ExtractTextSb('text', $json, $sbText, $jsonOut);
if (!$success) {
print $pdf->LastErrorText, "\n";
exit;
}
print 'Extracted ', $sbText->Length, " characters.\n";
# Save the text to a file.
$success = $sbText->WriteFile('qa_output/long_report.txt', 'utf-8', 0);
if (!$success) {
print $sbText->LastErrorText, "\n";
exit;
}
# The JSON output does not contain the text, but it still has the quality report.
print 'Quality: ', $jsonOut->StringOf('quality.rating'), "\n";
print 'Pages with no text (need OCR): [', $jsonOut->StringOf('quality.pagesNeedingOcr'), "]\n";
# Sample output:
# Number of pages: 3
# Extracted 4522 characters.
# Quality: good
# Pages with no text (need OCR): []