Aspose PDF Java - How to replace Specific text in first two paragraphs after Greeting text found

I am unable to replace specific text only in first two paragraphs after Greetings text found. PLease share sample sinnpet if possible in java.

Sample PDF Formate:

Address
zxxxxxx
xxxxxx

                Heading 1
                Heading 2

Greetings mathew,

Transport necessary for continued care of the member. The Facility
provides care at the direction PAYEMNT of the attending physician

PAYEMNT of this claim. Should remain denied, please
provide a detailed explanation PAYEMNT test to have zero devlivery.

// paragraph 3

// paragraph 4

Note: In above example I want to replace PAYEMNT text with TRANSACTION text in 1 & 2 paragraphs after Greetings text related line found

@Surendra.Kottapalli

Can you please share your sample PDF along with sample code snippet that you are using? We will test the scenario in our environment and address it accordingly.

Asad.ali. Thank you for your quick response.

Below source code I written to Update “Payment” word to “Transaction” word in first two Paragraphs after “Greetings” word found in PDF. In this code I am trying to Replace Payment Word in first occurrence Paragraph. This code Identifying Paragraph and extracting first Paragraph but not able to write back PDF.
Can you please share java code snippet which will Identify first two Paragraphs and replace Word after specific word found(Like Greetings Word).

// Code Snippet I am trying

public static void main(String[] args) {

Document pdfDocument = null;
pdfDocument = new Document(SRC);
Test1(“Payment”, “Transaction”)
}

public static void Test1(String originalText,String longerText) {
ParagraphAbsorber paragraphAbsorber = new ParagraphAbsorber();
paragraphAbsorber.visit(pdfDocument.getPages().get_Item(1));

	String textInParagraph = "";
	boolean foundIt = false;

	MarkupParagraph originalParagraph = null;

	for (PageMarkup markup : paragraphAbsorber.getPageMarkups()) {
	    if (foundIt) break;
	    for (MarkupSection section : markup.getSections()) {
	        if (foundIt) break;
	        for (MarkupParagraph paragraph : section.getParagraphs()) {
	            if (foundIt) break;
	            for (TextFragment fragment : paragraph.getFragments()) {
	                if (fragment.getText().equals(originalText) || originalText.equals(fragment.getText())) {
	                    textInParagraph = paragraph.getText();
	                    originalParagraph = paragraph;
	                    foundIt = true;
	                    break;
	                }
	            }
	        }
	    }
	}

	String searchableContent = textInParagraph.replace(" ", "\\s+");
	TextFragmentAbsorber absorber = new TextFragmentAbsorber(searchableContent);
absorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
	pdfDocument.getPages().accept(absorber);
	if (textInParagraph.length() > 0) {
	    String newText = textInParagraph.replaceAll(originalText, longerText);
		TextFragmentCollection textFragmentCollection = absorber.getTextFragments();

	    for (TextFragment fragment  : (Iterable<TextFragment>) textFragmentCollection) {
	    	fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
	    	fragment.setText(newText);
	    }
	}

}
TestPDF.pdf (106.1 KB)

@Surendra.Kottapalli

We have logged an investigation ticket as PDFJAVA-42545 in our issue tracking system to further analyze this case. We will look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.