We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace text with big sentence

When i am trying to replace a text with big sentence in aspose pdf java using TextFragment or PdfContentEditor, the line never breaks. I am not able to see next line in saved pdf. Kindly help.

@shashi0094

Thanks for contacting support.

Would you please use following code snippet with latest version of the API and in case you still face any issue, please share your sample PDF document with us along with the text which you want to replace. We will test the scenario in our environment and address it accordingly.

Document pdfDocument = new Document(myDir+"Lorem_lpsum.pdf");
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("book");
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

TextReplaceOptions textReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation, 
TextReplaceOptions.Scope.REPLACE_FIRST);
textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);
pdfDocument.getPages().accept(textFragmentAbsorber);

for (TextFragment textFragment : textFragmentCollection) {
textFragment.setText("big big big replacement text");
}

FileOutputStream pdfOutpustStream = new FileOutputStream(myDir+"modified.pdf");
pdfDocument.save(pdfOutpustStream); 

PFA. Here i have used PdfContentEditor.sample code.zip (213.8 KB)

Kindly advice how can i proceed with the same.

@shashi0094

Thanks for your inquiry.

Please note that we have shared you DOM Based code snippet which is recommended as com.aspose.pdf.facades approach will be going to obsolete sooner or later. New com.aspose.pdf (Document Object Model) approach is recommended and is more feature rich. We have used earlier shared code snippet with your document and managed to produce fine results.

c_out.pdf (120.1 KB)

In case you face any issue while using suggested approach, please feel free to let us know.