Created
February 18, 2021 01:56
-
-
Save aspose-com-gists/52502af0613b9151b22c72979727dfa2 to your computer and use it in GitHub Desktop.
Find and Replace Text in PDF Java
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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java | |
// Open document | |
Document pdfDocument = new Document("source.pdf"); | |
// Create TextAbsorber object to find all instances of the input search phrase | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("sample"); | |
// Accept the absorber for first page of document | |
pdfDocument.getPages().get_Item(0).accept(textFragmentAbsorber); | |
// Get the extracted text fragments into collection | |
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments(); | |
// Loop through the fragments | |
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) { | |
// Update text and other properties | |
textFragment.setText("New Pharase"); | |
textFragment.getTextState().setFont(FontRepository.findFont("Verdana")); | |
textFragment.getTextState().setFontSize(22); | |
textFragment.getTextState().setForegroundColor(Color.getBlue()); | |
textFragment.getTextState().setBackgroundColor(Color.getGray()); | |
} | |
// Save the updated PDF file | |
pdfDocument.save("Updated_Text.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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java | |
// Open document | |
Document pdfDocument = new Document("input.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 enable regular expression usage | |
TextSearchOptions textSearchOptions = new TextSearchOptions(true); | |
textFragmentAbsorber.setTextSearchOptions(textSearchOptions); | |
// Accept the absorber for all pages of document | |
pdfDocument.getPages().accept(textFragmentAbsorber); | |
// Get the extracted text fragments into collection | |
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments(); | |
// Loop through the fragments | |
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) { | |
// Update text and other properties | |
textFragment.setText("New Pharase"); | |
textFragment.getTextState().setFont(FontRepository.findFont("Verdana")); | |
textFragment.getTextState().setFontSize(22); | |
textFragment.getTextState().setForegroundColor(Color.getBlue()); | |
textFragment.getTextState().setBackgroundColor(Color.getGray()); | |
} | |
// Save the updated PDF file | |
pdfDocument.save("Updated_Text.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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java | |
// Open document | |
Document pdfDocument = new Document("source.pdf"); | |
// Create TextAbsorber object to find all instances of the input search phrase | |
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("sample"); | |
// Accept the absorber for all pages of document | |
pdfDocument.getPages().accept(textFragmentAbsorber); | |
// Get the extracted text fragments into collection | |
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments(); | |
// Loop through the fragments | |
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) { | |
// Update text and other properties | |
textFragment.setText("New Pharase"); | |
textFragment.getTextState().setFont(FontRepository.findFont("Verdana")); | |
textFragment.getTextState().setFontSize(22); | |
textFragment.getTextState().setForegroundColor(Color.getBlue()); | |
textFragment.getTextState().setBackgroundColor(Color.getGray()); | |
} | |
// Save the updated PDF file | |
pdfDocument.save("Updated_Text.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment