Aspose pdf for Java fails to word-wrap replaced text

Hi,

I am using Aspose.PDF for JAVA to replace some keywords in a pdf.
I’m setting the textOption ReplaceAdjustment.WholeWordsHyphenation with scope REPLACE_ALL on my textAbsorber, collecting the textFragments and then setting their text contents to my chosen string.
For text that is less than two lines long, Aspose correctly wraps it to fit the size of the page. Once the text becomes longer than two lines, Aspose instead fails to split the line following the first one.

Attached you can find an example:
When substituting the string $$MultipleSelection11909600$$ with the string test1, test2, test3, test4, test1, test2, test3, test4, test1, test2, test3, test4, test1, test2, test3, test4, aspose correctly splits the substituted line once but fails to split the line multiple times.
Are you aware of any reason it might be doing so?
I’m currently working with Aspose.pdf version 23.3 on Java 1.8

Thank you for your time,
tcovioli

Screenshot 2023-03-28_input.png (9.7 KB)

Screenshot 2023-03-28_output.png (10.8 KB)

@tcovioli

Would you please share your sample PDF along with the complete code snippet to test the scenario? We will check it in our environment and address it accordingly.

Hi @asad.ali, thank you for your reply.
In attachment I’m sending you an input file and an output file.

The code snipped I use to do the replacement is the following:

public static void test(String inputPath, String outputPath) {
    	String replacingString = "test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15";
    	String replacedString = "$$wordtosubstitute$$";
    	File initialFile = new File(inputPath);
        
    	try {
        	InputStream inputStream = new FileInputStream(initialFile);
        	Document pdfDocument = new Document(inputStream);

        	TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(replacedString);
			TextReplaceOptions textReplaceOptions = new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation, 
					TextReplaceOptions.Scope.REPLACE_ALL);
			textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);
        	
			pdfDocument.getPages().accept(textFragmentAbsorber);
			
			TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
			
			for (TextFragment textFragment : textFragmentCollection) {
				textFragment.setText(replacingString);
			}
			
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            pdfDocument.save(byteArrayOutputStream);
            byteArrayOutputStream.close();
            pdfDocument.close();
            
            byte[] byteArray = byteArrayOutputStream.toByteArray();
           
            File outputFile = new File(outputPath);
			FileOutputStream outputStream = new FileOutputStream(outputFile);
			outputStream.write(byteArray);
			outputStream.close();
			
			System.out.println("Finished writing to file "+outputPath);
			
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }

As you can see, I’m performing the substitution of the string “$$wordtosubstitute$$” (found on page 2 of inputP.pdf) with a long string, which in the file outputP.pdf is not correctly word-wrapped.

Thank you for your time,
Tancredi

inputP.pdf (59.5 KB)
outputP.pdf (216.3 KB)

@tcovioli

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-42627

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
My colleague has purchased the paid support service and has proceeded to open a new ticket (#176758).

Thank you,
Tancredi

@tcovioli

Your ticket will definitely be escalated to the highest priority and you will receive updates in respective paid support thread.