Search PDF via C++

Advanced PDF document search. Use Aspose.PDF for C++ to modify PDF documents programmatically

How to Search PDF File Using C++

In order to search PDF file, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy to use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Search PDF File via C++

You need Aspose.PDF for C++ to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create TextFragmentAbsorber Object with text to find as parameter.
  3. Get all extracted text fragments collection.
  4. Loop through each fragment to get all of its information.

Search PDF Files - C++

// Load PDF document
Document pdfDocument = new Document("source.pdf");

// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("\d{4}-\d{4}"); // like 1999-2000

// Set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);

// Accept the absorber for first page of document

// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

// Loop through the fragments
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
	System.out.println("Text :- " + textFragment.getText());
	System.out.println("Position :- " + textFragment.getPosition());
	System.out.println("XIndent :- " + textFragment.getPosition().getXIndent());
	System.out.println("YIndent :- " + textFragment.getPosition().getYIndent());
	System.out.println("Font - Name :- " + textFragment.getTextState().getFont().getFontName());
	System.out.println("Font - IsAccessible :- " + textFragment.getTextState().getFont().isAccessible());
	System.out.println("Font - IsEmbedded - " + textFragment.getTextState().getFont().isEmbedded());
	System.out.println("Font - IsSubset :- " + textFragment.getTextState().getFont().isSubset());
	System.out.println("Font Size :- " + textFragment.getTextState().getFontSize());
	System.out.println("Foreground Color :- " + textFragment.getTextState().getForegroundColor());