TextFragments do not seem to retain embedded fonts if the text is replaced. For example, absorbed text has the font “MinionPro-Regular”, but after the text substitution, the font is now “Time New Roman”. In addition, if the TextFragment text is replaced with something really long then line wrapping is not automatically applied. These could be 2 very different problems, but I have one simple test case to highlight both problems.
I’ve tried with both aspose.pdf-20.6.jar and aspose.pdf-21.5.jar
/**
* Use case for aspose for replacement not using embedded fonts
* @throws Exception
*/
@Test
public void testEmbeddedFont() throws Exception {
final TextFragmentAbsorber textFragmentAbsorberReplacement = new TextFragmentAbsorber("{{replace_1}}");
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream("artifacts/template6_1_page_replacement.pdf");
final Document document = new Document(inputStream);
document.getPages().accept(textFragmentAbsorberReplacement);
for (final TextFragment textFragment : textFragmentAbsorberReplacement.getTextFragments()) {
final String token = textFragment.getText().trim();
Assertions.assertEquals("{{replace_1}}", token);
Assertions.assertEquals("MinionPro-Regular", textFragment.getTextState().getFont().getFontName());
// PROBLEM: Observe that the font changes from MinionPro-Regular to Times New Roman and that line wrapping is not honored
textFragment.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt " +
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
"laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " +
"voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat " +
"cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
}
document.save("output.pdf");
}