Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find and Replace text in PDF file using C++
Find and Replace text in PDF file using C++
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");
// Accept the absorber for all the pages
pdfDocument->get_Pages()->Accept(textFragmentAbsorber);
// Get the extracted text fragments
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();
// Loop through the fragments
for (auto textFragment : textFragmentCollection)
{
// Update text and other properties
textFragment->set_Text(u"UPDATED TEXT");
textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
textFragment->get_TextState()->set_FontSize(22);
}
// Save the resulting PDF document.
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");
// search text within page bounds
textFragmentAbsorber->get_TextSearchOptions()->set_LimitToPageBounds(true);
// specify the page region for TextSearchOptions
textFragmentAbsorber->get_TextSearchOptions()->set_Rectangle(MakeObject<Rectangle>(100, 100, 800, 700));
// Accept the absorber for first page of document
pdfDocument->get_Pages()->idx_get(1)->Accept(textFragmentAbsorber);
// Get the extracted text fragments
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();
// Loop through the fragments
for (auto textFragment : textFragmentCollection)
{
// Update text and other properties
textFragment->set_Text(u"UPDATED TEXT");
textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
textFragment->get_TextState()->set_FontSize(22);
}
// Save resulting PDF document.
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Document");
// Accept the absorber for second page of document
pdfDocument->get_Pages()->idx_get(2)->Accept(textFragmentAbsorber);
// Get the extracted text fragments
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();
// Loop through the fragments
for (auto textFragment : textFragmentCollection)
{
// Update text and other properties
textFragment->set_Text(u"UPDATED TEXT");
textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
textFragment->get_TextState()->set_FontSize(22);
}
// Save resulting PDF document.
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument2.pdf");
// Load PDF file
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 2.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"\\d{4} - \\d{4}"); // Like 1999-2000
// Set text search option to enable regular expression usage
auto textSearchOptions = MakeObject<TextSearchOptions>(true);
textFragmentAbsorber->set_TextSearchOptions(textSearchOptions);
// Accept the absorber for all pages of the document
pdfDocument->get_Pages()->Accept(textFragmentAbsorber);
// Get the extracted text fragments
auto textFragmentCollection = textFragmentAbsorber->get_TextFragments();
// Loop through the fragments
for (auto textFragment : textFragmentCollection)
{
// Update text and other properties
textFragment->set_Text(u"UPDATED TEXT");
textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman"));
textFragment->get_TextState()->set_FontSize(22);
}
// Save resulting PDF document.
pdfDocument->Save(u"OutputDirectory\\UpdatedDocument.pdf");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment