Free Support Forum - aspose.com

Special characters on in Text Replace

Hello, I am currently evaluating Aspose.PDF for Java. I need to replace placeholders in an existing PDF file. If I replace the placeholders with normal characters (eg. “Peter” or “Hello”) it looks good. As soon as I try to replace it with a special character (eg. dots, braces etc) it sets a default font family and doesn’t transfer the one used in the PDF-document. This is a problem as soon as I want to replace E-Mail addresses for example.

I am using this line to replace the text:
pdfContentEditor.replaceText(stringToReplace[0], stringToReplace[1]);

Am I missing something?

@timhonermann

Thanks for contacting support.

We have tested the scenario in our environment using one of our sample PDFs and Aspose.PDF for Java 18.9. We were unable to find any issue at our side. The text was replaced fine with another text as well as special characters. Would you please use following code snippet to replace the text inside your PDF and in case you still face any issue, please share your sample PDF document. We will test the scenario again in our environment and address it accordingly.

try (InputStream in = new FileInputStream(dataDir + "c.pdf")) {
Document document = new Document(in);
TextFragmentAbsorber absorber = new TextFragmentAbsorber("San Mateo");
TextSearchOptions searchOption = new TextSearchOptions(false);
absorber.setTextSearchOptions(searchOption);
Page firstPage = document.getPages().get_Item(1);
firstPage.accept(absorber);
absorber.getTextFragments().get_Item(1).setText("J!@n d@e");
document.save(dataDir + "c_out.pdf");
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
1 Like

@asad.ali

Thanks for the quick answer. I did the replacement as you suggested, with Aspose.PDF for Java 18.9. I still get the same result.
Replaced PDF (97.5 KB)

e.g if I replace it with “pxBankName” instead of “px.BankName” the font stays the same and doesn’t change.

You can test it just by replacing anything inside this document with dots or @ (these are the two characters I tested it with)

@timhonermann

Thanks for writing back.

It seems you have shared an output PDF document. Would you please share respective source PDF file so that we can test the scenario in our environment and address it accordingly. Also, we have noticed that you are using a trial version of the API without license. We request you to please consider applying 30-days temporary license and test the scenario again with valid license.