Extract Paragraph from PDF
Contents
[
Hide
]
Extract Text from PDF document in Paragraphs form
We can get text from a PDF document by searching a particular text (using “plain text” or “regular expressions”) from a single page or whole document, or we can get the complete text of a single page, range of pages or complete document. However, in some cases, you require to extract paragraphs from a PDF document or text in the form of Paragraphs. We have implemented functionality for searching sections and paragraphs in the text of PDF document pages. We have introduced ParagraphAbsorber Class (like TextFragmentAbsorber and TextAbsorber), which can be used to extract paragraphs from PDF documents.
Iterating through paragraphs collection and get the text of them
// Open an existing PDF file
$document = new Document($inputFile);
// Instantiate ParagraphAbsorber
$absorber = new ParagraphAbsorber();
$absorber->visit($document);
$responseData = "";
foreach ($absorber->getPageMarkups() as $markup) {
$i = 1;
foreach ($markup->getSections() as $section) {
$j = 1;
foreach ($section->getParagraphs() as $paragraph) {
$paragraphText = "\r\n";
foreach ($paragraph->getLines() as $line) {
foreach ($line as $fragment) {
$paragraphText = $paragraphText . $fragment->getText();
}
$paragraphText = $paragraphText . "\r\n";
}
$paragraphText = $paragraphText . "\r\n";
$responseData = $responseData . "Paragraph " . $j++ . " of section " . $i++ . " on page" . ":" . markup->getNumber();
$responseData = $responseData . $paragraphText;
$j++;
}
$i++;
}
}
// Save the extracted text to the output file.
file_put_contents($outputFile, $responseData);