Free Support Forum - aspose.com

How to align the text and following content while replacing text using

Hi,

We are using aspose.pdf for java with Coldfusion. I am trying to replace text in a PDF document using textFragmentAbsorber.getTextFragments().get_Item(eachItem).setText method of the textfragmentabsorber object.

The text seems to get replaced correctly, however the replaced content does not wrap and extends beyond the boundary of the PDF document and thus cannot be seen. Also the following content on the PDF does not get rearranged or get pushed beyond the PDF boundary. Can you pls suggest how this can be fixed?

Below is the code where filepath, newfilepath, compinsertvalue_new_filled are variables contained appropriate paths and text to be replaced. The below code is supposed to replace the text “[CompInsert]” with the content in the variable “compinsertvalue_new_filled”

					<CFOBJECT
						action = "create"
						type="java"
						class="com.aspose.pdf.TextFragmentAbsorber"
						name="textFragmentAbsorber">

                                                    <cfset docPDF.init(filepath)>
						<cfset pageCount = docPDF.getPages().size()>

                                                    <cfset textFragmentAbsorber.init("[CompInsert]")>
							
							<cfloop from="1" to="#pageCount#" index="eachPage">
							
								<cfset docPDF.getPages().get_Item(eachPage).Accept(textFragmentAbsorber)>
								<cfset textFragmentCollection = textFragmentAbsorber.getTextFragments()>
								<cfset textFragmentCount = textFragmentAbsorber.getTextFragments().size()>
								
								
								<cfloop from="1" to="#textFragmentCount#" index="eachItem">
									<cfset textFragmentAbsorber.getTextFragments().get_Item(eachItem).setText(javacast("string",compinsertvalue_new_filled))>
								</cfloop>
								
							</cfloop>
                                                            <cfset docPDF.save(newfilepath)>
						
						<cfset docPDF.close()>

Kindly suggest what should I do to make the content that is placed in the PDF wrap and rearrange the following content within the margins of the PDF.

Regards,

@Batrinux,

Kindly send us your source PDF document and let us know the version of Aspose.PDF for Java API. We will investigate your scenario in our environment and share our findings with you.

Telnet Offer 1 Week Vacation2.pdf (95.6 KB)
Attached is the PDF which we are using. We replace things within square brackets ([]) with data from the database.

Also, if we have like 8 files, most of which are either 1 or 2 pages, it sometimes takes 10 minutes to fill the documents and save them. Are there any known issues with aspose regarding the times it takes to fill the documents.

Regards,

@Batrinux,

Kindly send us your output PDF along with target and replaced values, and also highlight the problematic area with the help of a snapshot. Please also create the same test runs for other scenarios. Your response is awaited.

Addtl Comp Perf Bonus_Filled_[2018-05-17]_[01-29].pdf (61.1 KB)
Pls see attached. The text highlighted in orange is the one that was replaced and on that line the text “[CompInsert]” used to exist. The whole text is coming on a single line and extending beyond the boundaries of the PDF doc. It should have ideally wrapped itself and pushed the following content further down after creating appropriate space for itself.

Can you also let us know about any known performance issues?

Regards

@Batrinux,

You are using an old version 17.12 of Aspose.PDF for Java API, please download and try the latest version 18.4 of Aspose.PDF for Java API. The content of the input and output PDF documents does not match, please send us the actual input PDF and the complete code. Your response is awaited.

Addtl Comp Perf Bonus.pdf (59.2 KB)
Pls find the input PDF attached herewith.

Regards,

@Batrinux,

We have tested your scenario with the latest version 18.4 of Aspose.PDF for Java API, and managed to replicate the problem of text being extended beyond the boundaries of the PDF document. It has been logged under the ticket ID PDFJAVA-37750 in our issue tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates. This is the output PDF: Output18.4.pdf (55.9 KB)
Java

String dataDir = "C:\\Pdf\\test841\\";
int textFragmentCount;
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("[CompInsert]");
Document docPDF = new Document(dataDir + "Addtl Comp Perf Bonus.pdf");
for (Page page : (Iterable<Page>) docPDF.getPages()) 
{
	page.accept(textFragmentAbsorber);
	TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
	textFragmentCount = textFragmentAbsorber.getTextFragments().size();
	for (TextFragment fragment : (Iterable<TextFragment>) textFragmentCollection){
		fragment.setText("Test - Performance Bonus: $5k/year (Based on the following note). Eligible for the performance bonus till end of 2015 and");
	}
}
docPDF.save(dataDir + "Output18.4.pdf");

Hi Imran.rafique,

Can you also let me know if there are any known issues with time taken to invoke classes to create objects and calling its methods.

Sometimes it takes us around 10 minutes to fill a bunch of PDF docs. Can you kindly comment on that as well.

Regards

@Batrinux,

Well, it depends on the complexity of the document and specifications of the environment. Please add code check to calculate the processing time of each PDF document, and filter out the most time consuming documents. After this, please send us your problematic PDF documents and code. We will investigate your PDF documents in our environment, and share our findings with you.

@Batrinux

Thank you for being patient.

We have investigated the ticket PDFJAVA-37750 and would like to request you to use below line of code in your environment.

textFragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);

However, after text replacement the line under the link remains in old place. For which another ticket PDFJAVA-37826. has been created in our issue management system. We will let you know as soon as this ticket will be resolved.