PDF text replace not wrapping to a new line

Hello,


I’m using the TextFragmentObsorber to find a text field in my PDF document, and then using the TextFragment to replace the next. The new text is much longer than the old text, and it is not causing any overlap which is great. However, because the text is much longer other text now flows off of the page instead of pushing down to the next line as I would typically expect. How can I ensure my subsequent text does not go off of the screen but is instead dropped down to the next line?

Thanks

Hi Patrick,


Thanks for using our API’s.

In order to accomplish your requirements, please follow the instructions specified over Text Replacement should automatically re-arrange Page Contents.

In case you still encounter same issue, please share the input document, so that we can test the scenario in our environment. We are sorry for this inconvenience.

I followed the link but that does not solve the issue. I have attached two PDF files that demonstrate the problem. The first is the one I use when searching for the placeholder text, and the other is the result of the text being replaced and saved to a new PDF file.


Please help me resolve this issue!

Hi Patrick,


Thanks for using our API’s.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNET-41324 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Glad you could reproduce it. I know it’s difficult to say, but could you please to try to provide me with an estimate as to when this will be actively worked on? Trying to gauge whether or not I can hold off until it’s fixed or if I need to purchase from another vendor.


Thanks for your help!!!

Hi Patrick,


Thanks for your patience.

As we recently have noticed earlier reported issue, so its pending for review and is not yet resolved. However the product team will surely consider investigating/fixing it as per development schedule and as soon as we have some definite updates regarding its resolution, we will let you know. Please be patient and spare us little time. We are sorry for this delay and inconvenience.

Hello, I’m getting pressure from stakeholders regarding this issue. We have to finalize our product choice before the end of September. Is there any updates or timing info you can give to me that I may be able to relay back to my team? I really don’t want to have to go with another vendor, because we have very satisfied with Aspose other than this one issue.


Thanks

Hi Patrick,


Thanks for your patience.

The earlier reported issue is still pending for review and is not yet resolved as the team has been busy fixing other previously reported issues. However your concerns have been recorded with product team and they will surely consider fixing them as per their schedule. As soon as we have some definite updates, we will let you know.

We are sorry for this delay and inconvenience.

Hi Nayyer Shahbaz,


I am also experiencing the same issue. Since it has been around 2 months I am just wondering if by any chance you know if this issue has been fixed in the latest releases?

Thanks for your help,

Kind Regards,
William

Hi William,


Thanks for your inquiry. I am afraid the reported issue is still not resolved, as product team is busy in resolving other issues in the queue reported earlier.

However, please note mostly issues vary from file to file. We will appreciate it if you please share your sample PDF document along with sample code here, we will look into it and will provide you information accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal Ahmad,


I have copied the snippet of text which I had the issue with into a new PDF document because I can’t send you the original PDF document for privacy reasons.

This new PDF document does have the same issue so I can send these over instead.

The code is within a function on it’s own but basically is nearly the same as the code you provided on your documentation website:

Public Function ReplaceText(ByVal findString As String, ByVal replaceString As String, Optional ByVal pageNo As Integer = -1) As Boolean
Try
'Open document
If Not IsDocumentOpen() Then Return False

'Create TextAbsorber object to find all instances of the input search phrase
Dim FindText As New TextFragmentAbsorber(findString.Trim)

'Pages to search
If pageNo = -1 Then
_pdfDocument.Pages.Accept(FindText)
Else
_pdfDocument.Pages(pageNo).Accept(FindText)
End If

'Get a collection of text fragments to replace
Dim textFragmentCollection As TextFragmentCollection = FindText.TextFragments

'Loop through the fragments
For Each textFragment As TextFragment In textFragmentCollection
textFragment.Text = replaceString 'Update text and other properties
Next textFragment

Return True
Catch ex As Exception
Return False
End Try
End Function


The passed in variable (findString) is «_Email» and the replaceString is al*********r@udgroup.co.uk (see the pdf document attached to see the actual email address).

In the edited PDF we expected that the email address will be placed onto the next line but it has been pushed off the edge of the PDF sheet. We also expect it to respect any spaces it has in front of it.

If you need any more information please feel free to contact me further.

Kind Regards,
William

Hi William,

Thanks for sharing your sample code and document. Please use WholeWordsHyphenation TextReplacementOptions, it tries to distribute words in paragraph after replacement. It will help you to accomplish your requirement.

Dim textFragmentAbsorber As New TextFragmentAbsorber(“«_Email»”)

textFragmentAbsorber.TextReplaceOptions.ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation

Best Regards,

Hi Tilal Ahmad,


That is brilliant, it now goes onto the new line but what I have noticed is it also chops the word ‘Customer’.
Is there a reason for this?

(See attached)

Kind Regards,
William

Hi William,


Thanks for your feedback. You are using a quite old version 9.6.0, please download and try latest version of Aspose.Pdf for .NET, it will resolve the issue.

Best Regards,

I am also facing same kind of problem.
i am trying to absorb text “first” in pdf file.
sample1.pdf:-contains
My first pdf page My first pdf page My first pdf page My first pdf page

I am using aspose-pdf-12.0.0.jar
trying to replace with “abcdefg1,abcdefg2,abcdefg3,abcdefg4” with the following code.

		TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("first");			
		pdfDocument.getPages().accept(textFragmentAbsorber);			
		for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentAbsorber.getTextFragments()) {	
			textFragment.setText("abcdefg1,abcdefg2,abcdefg3,abcdefg4");
	}
		pdfDocument.save(dataDir+"TestFragment.pdf");	

But the result ends in same line with incomplete sentences.

@shashi0094

Thanks for contacting support.

We could not find any attachment with your post. However, 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 so that we can 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); 

Thank you very much for the reply.I tried with the above code. It works fine but partially works fine.
I have tried with my own replacing text. PFA. Kindly advice.testreplace.zip (221.2 KB)

Can you please provide more examples on TextReplaceOptions.

@shashi0094

Thanks for getting back to us.

We have tested the scenario in our environment with Aspose.PDF for Java 18.11 and were able to notice that text was getting overlapped while replacing it with long string. Hence, we have generated an issue as PDFJAVA-38238 in our issue tracking system for the sake of investigation. We will further investigate the reasons behind this issue and keep you posted with the status of rectification. Please spare us little time.

We are sorry for the inconvenience.