Last active
March 10, 2021 11:14
-
-
Save aspose-com-gists/d77cf182b00b4674ec702a7e50e0302a to your computer and use it in GitHub Desktop.
Find and Replace text in PDF file using C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Find and Replace text in PDF file using C++ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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