Please check following code snippet which we have used for testing the scenario. We did not create blank PDF and add page to it - instead, we have performed find and replace operation on the PDF which you have shared after setting the font substitution.
public void FindAndReplace() throws java.lang.Exception
{
//Set the folder with the only one font LiberationSans-Regular.ttf
Document.setLocalFontPaths(new ArrayList());
Document.addLocalFontPath("/usr/share/fonts/");
//configure font substitution
CustomSubst1 subst1 = new CustomSubst1();
FontRepository.getSubstitutions().add(subst1);
// Open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(dataDir + "templateVOE.pdf");
// Create TextAbsorber object to find all instances of the input search phrase
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber("#borrowerName");
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (com.aspose.pdf.TextFragment textFragment : (Iterable<com.aspose.pdf.TextFragment>) textFragmentCollection)
{
// Update text and other properties
textFragment.setText("Batman");
}
// Save the updated PDF file
pdfDocument.save(dataDir + "Updated_Text_new.pdf");
}
private static class CustomSubst1 extends com.aspose.pdf.text.CustomFontSubstitutionBase
{
public boolean trySubstitute(com.aspose.pdf.text.CustomFontSubstitutionBase.OriginalFontSpecification originalFontSpecification, /*out*/ com.aspose.pdf.Font[] substitutionFont)
{
try
{
substitutionFont[0] = FontRepository.findFont("DejaVuSans");;
} catch (java.lang.NullPointerException e)
{
return false;
}
return true;
}
}
Please also check the font directory in attached image, which path was used in the above code snippet. Font_Directory.png (39.7 KB) In case we have missed some routine while testing the scenario, you may please highlight it, so that we can again test the scenario and try to replicate the error in our environment.