Replace Text in PDF with TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation results in error

Hi,
We are tying to replace the text in PDFA-2b file. Th PDFA file consists of simple table with all keywords that we are interested to replace. When we use “TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation” we get error as below.
Specified argument was out of the range of valid values.
Parameter name: Parameter name: index

Also the TextStamp applied goes behind the table.

Please check the .log files for error messages

Attached the input pdf, output pdf, code & keywords for which we get error.
Aspose.zip (229.9 KB)

Version of Aspose PDF API for JAVA is 17.9

@sarvanan.murthi.ext,

We have tried to execute your code and facing an error as: Error:: Missing required options: i, o, k, v, p. Please list down all steps to replicate the said error. We will investigate and share our findings with you.

-i “Input PDF” -o “Output PDF” -k “KeyWordsCustomStamping.txt” -v “StampResult_CustomStamping.txt” -p true

You can execute as above… the text files mentioned are also attached… “-p true” is to add TextStamp to the output PDF

@sarvanan.murthi.ext,

We are working over your scenario and will get back to you soon.

Any findings?

@sarvanan.murthi.ext,

We are sorry for the delay. We have simplified your scenario by removing additional code and could not replicate the said error in our environment with Aspose.Pdf for Java 17.10. These are the code and output PDF files: Test_Stamp_by_keyword_Output.pdf (43.6 KB) and CustomStamping.zip (4.4 KB). If you can find the issue, then please highlight with the help of a snapshot.

Hi,
Thanks for the reply.

The problem is when we use “TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation”.

Can you please try with below replace function?

/*****************************************************************************************/
public static void replace(String strKey,String strValue) throws IOException
{
INFO("Replacing key “+strKey+” with value "+strValue);

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(strKey);
	
TextReplaceOptions textReplaceOptions = textFragmentAbsorber.getTextReplaceOptions(); 

if(textReplaceOptions != null)
{

	textReplaceOptions.setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);

}

textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);


pdfInputDocument.getPages().accept(textFragmentAbsorber);

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
if(textFragmentCollection.size()>0)
{
	int i=1;
	for (TextFragment textFragment : textFragmentCollection) 
	{
		if(textFragment != null)
		{
		INFO("\tReplacing Occurence "+i+" in Page "+textFragment.getPage().getNumber());				
		try {
			textFragment.setText(strValue);
		} catch (Exception e) {
			INFO("\t"+"Error:: "+e.getLocalizedMessage());
		}
		++i;
		}
	}
}
else{
	INFO("\tNo Occurences found");
}

}
/*****************************************************************************************/

@sarvanan.murthi.ext,

We managed to replicate the said error in our environment. It has been logged under the ticket ID PDFJAVA-37289 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Please elaborate a bit about the problem behavior with the help of a snapshot.

Hi,
If we apply TextStamp to a PDF having table, then the Stamp we apply is not visible. It goes behind the table.
Below is the code we are using to apply TextStamp,
/***************************************************************************/
public static void stampPreiew() throws UnsupportedEncodingException
{
String path = CustomStamping.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String strJarPath = URLDecoder.decode(path, “UTF-8”);

	String strStampFile=new File(strJarPath).getParent()+"/stamp.info";
	INFO("STAMP Information File::"+strStampFile);

	Map<String, String> mStampInformation=new HashMap<String, String>();

	File file = new File(strStampFile);
	if (!file.exists())
	{
		INFO("INFO:: "+ "Stamp Information File ["+strStampFile+"] not found. Using Default Settings");
	}else{

		INFO("Reading Stamp Information File");
		INFO("******************************");
		ArrayList<String> mapping = readFileContents(strStampFile,"::");
		for (String string : mapping)
		{
			String[] splittedStrings = string.split("=", -1);

			if(splittedStrings.length==2)
			{
				mStampInformation.put(splittedStrings[0], splittedStrings[1]);
				INFO("\tSTAMPINFO:: "+splittedStrings[0]+" "+splittedStrings[1]);
			}
		}
		INFO("*************************************");
		INFO("End of Reading Stamp Information File");
		INFO("");
	}

	TextStamp textStamp=null;

	if(mStampInformation.containsKey("stamp1.text")){
		textStamp=new TextStamp(mStampInformation.get("stamp1.text"));
	}else 
	{
		INFO("\tStamp Text information not found!! Using Preview as default");
		textStamp=new TextStamp("Preview");
	}

	if(mStampInformation.containsKey("stamp1.fontsize"))
	{
		try {
			textStamp.getTextState().setFontSize(Float.parseFloat(mStampInformation.get("stamp1.fontsize")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Font Size not valid!! Using 100f as default");
			textStamp.getTextState().setFontSize(100f);
		}
	}
	else
	{
		INFO("\tStamp Font Size information not found!! Using 100f as default");
		textStamp.getTextState().setFontSize(100f);
	}

	if(mStampInformation.containsKey("stamp1.horizontalalign"))
	{
		try {
			textStamp.setHorizontalAlignment(Integer.parseInt(mStampInformation.get("stamp1.horizontalalign")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Horizontal Alignment information not valid!! Using Center as default");
			textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
		}
	}
	else
	{
		INFO("\tStamp Horizontal Alignment information not found!! Using Center as default");
		textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
	}

	if(mStampInformation.containsKey("stamp1.verticalalign"))
	{
		try {
			textStamp.setVerticalAlignment(Integer.parseInt(mStampInformation.get("stamp1.verticalalign")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Vertical Alignment information not valid!! Using Center as default");
			textStamp.setVerticalAlignment(VerticalAlignment.Center);
		}
	}
	else
	{
		INFO("\tStamp Vertical Alignment information not found!! Using Center as default");
		textStamp.setVerticalAlignment(VerticalAlignment.Center);
	}

	if(mStampInformation.containsKey("stamp1.textalign"))
	{
		try {
			textStamp.setTextAlignment(Integer.parseInt(mStampInformation.get("stamp1.textalign")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Text Alignment information not valid!! Using Center as default");
			textStamp.setTextAlignment(HorizontalAlignment.Center);
		}
	}
	else
	{
		INFO("\tStamp Text Alignment information not found!! Using Center as default");
		textStamp.setTextAlignment(HorizontalAlignment.Center);
	}



	if(mStampInformation.containsKey("stamp1.rotateangle"))
	{
		try {
			textStamp.setRotateAngle(Double.parseDouble(mStampInformation.get("stamp1.rotateangle")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Rotate information not valid!! Using 0 as default");
			textStamp.setRotateAngle(0);
		}
	}
	else
	{
		INFO("\tStamp Rotate information not found!! Using 0 as default");
		textStamp.setRotateAngle(0);
	}

	if(mStampInformation.containsKey("stamp1.fontstyle"))
	{
		try {
			textStamp.getTextState().setFontStyle(Integer.parseInt(mStampInformation.get("stamp1.fontstyle")));
		} catch (NumberFormatException e) {
			INFO("\tStamp Font Style information not valid!! Using Italic as default");
			textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
		}
	}
	else
	{
		INFO("\tStamp Font Style information not found!! Using Italic as default");
		textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
	}


	if(mStampInformation.containsKey("stamp1.font"))
	{
		try {
			textStamp.getTextState().setFont(FontRepository.findFont(mStampInformation.get("stamp1.font"), true));
		} catch (Exception e) {
			INFO("\tStamp Font not installed!! Using Arial as default");
			textStamp.getTextState().setFont(FontRepository.findFont("Arial", true));
		}
	}else{
		INFO("\tStamp Font information not found!! Using Arial as default");
		textStamp.getTextState().setFont(FontRepository.findFont("Arial", true));
	}


	textStamp.getTextState().setForegroundColor(Color.getLightGray());
	textStamp.setJustify(true);
	textStamp.setBackground(true);

	PageCollection pages = pdfInputDocument.getPages();
	for (Page page : pages)
	{
		page.addStamp(textStamp);
	}
}

/***************************************************************************/

@sarvanan.murthi.ext,

We managed to replicate the problem of invisible text stamp in our environment. It has been logged under the ticket ID PDFJAVA-37292 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Hello Aspose Team,

is there any update on PDFJAVA-37289 and PDFJAVA-37292.?

@sarvanan.murthi.ext,

The linked ticket IDs PDFJAVA-37289 and PDFJAVA-37292 are not resolved yet. We will investigate as per the development schedules and let you know once a significant progress has been made in this regard.

Hello Aspose Team,

is there any update on PDFJAVA-37289 and PDFJAVA-37292.?

@sarvanan.murthi.ext

Thanks for your inquiry.

I am afraid that your reported issues have not been yet resolved. These issues are dependent upon internal components of the API and it will take some time, in order to resolve them. Since, there are other pending issues in the queue as well, your issues will be resolved over first come first serve basis. As soon as we have some definite updates regarding resolution progress of your issues, we will let you know. Please spare us little time.

We are sorry for the inconvenience.

Hello Aspose Team,

is there any update on PDFJAVA-37289 and PDFJAVA-37292.?

@sarvanan.murthi.ext

I am afraid that earlier logged issues have not been yet resolved, due to other pending issues in the queue. However, as soon as we have some definite updates regarding their resolution, we will surely inform you. We greatly appreciate your patience and comprehension in this regard. Please spare us little time.

We are sorry for the inconvenience.

Hello Aspose Team,

is there any update on PDFJAVA-37289 and PDFJAVA-37292.?

@sarvanan.murthi.ext

Thanks for your inquiry.

We would like to share with you that above issue has been resolved and its fix will be included in upcoming version Aspose.PDF for Java 18.9 which will be available for download in the end of this month. As soon as fix is available, we will surely notify you.

I am afraid that issue is pending for a resolution due to other pending issues in the queue. As soon as we make some significant progress towards its rectification, we will surely let you know. Please be patient and spare us little time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-37289) have been fixed in this update.