TextFragment loses embedded font when text is replaced

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");
}

@jtmille3

You are using a source PDF in the code snippet that you have shared. Please share that file with us as well so that we can proceed with testing the scenario and addressing it accordingly.

Source PDF

@jtmille3

We were able to reproduce the issues that you have mentioned. Therefore, we have logged a ticket as PDFJAVA-40555 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

I’ve updated the test case. It clearly fails right after I set the text on the TextFragment. The font MinionPro-Regular is wiped out immediately and replaced with the font TimesNewRoman.

/**
 * 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);

    final String lorem_ipsum = "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.";

    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);

        // ALREADY FAILS HERE
        Assertions.assertEquals("MinionPro-Regular", textFragment.getTextState().getFont().getFontName());
    }
}

@jtmille3

We have updated the earlier logged ticket according to the provided information. We will let you know once we have additional updates regarding its resolution.

The issues you have found earlier (filed as PDFJAVA-40555) have been fixed in Aspose.PDF for Java 21.8.