Text Replacement and Table Formatting in Aspose PDF for Java

Hello Team,

I recently started working with Aspose PDF for Java to perform Text Replacement that should automatically re-arrange page contents. However, I am encountering issues when dealing with tables:

  1. When I use ReplaceAdjustment as WholeWordsHyphenation, the table lines disappear, as shown in the attached image.
textFragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);

with-whole-words-hyphenation.PNG (51.7 KB)

  1. When I do not use ReplaceAdjustment or if I use ReplaceAdjustment as None, the lines are present, but the contents are not rearranged properly. Specifically, the columns in the table do not adjust correctly if I replace a large text.
textFragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.None);

without-replace-adjustment.PNG (58.3 KB)

The original PDF file looks like this.

original-pdf-file.png (50.0 KB)

Here is the relevant code snippet:

Document document = new Document(Files.newInputStream(Paths.get(SOURCE_PDF_FILE)));
PageCollection pages = document.getPages();

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Lorem Ipsum is");
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
textFragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);

document.getPages().accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

for (TextFragment textFragment : textFragmentCollection) {
    textFragment.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.");
}

document.save(Files.newOutputStream(Paths.get(DEST_PDF_FILE)));

Is this a known limitation with tables, or is there a way to achieve the desired behaviour where the table lines are also adjusted, and the content is rearranged correctly?

I am using Aspose PDF with Java 8.

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>24.6</version>
</dependency>

Thank you for your assistance. Regards.

@HansanaA

The re-arranging of the content inside PDF after text replacement can be challenging as elements are stored differently in PDF structure. Nevertheless, we can perform investigation to check and find possible solution to deal with such scenarios. Can you please share a sample PDF for our reference?

Hi @asad.ali

Thank you for the response. I appreciate it. Sure please find the attached PDF file.

lorem-ipsum.pdf (65.7 KB)

@HansanaA

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44093

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @asad.ali,

Thank you for the response. I will check with my team to get the Paid Support Service.

@HansanaA

Sure, if this issue is urgent and a showstopper, you can please check our paid support option. In the meanwhile, we will keep you posted with the rectification status of the ticket via this forum thread.

@asad.ali Sure. Thank you.