Error when replacing text in pdf using pdfcontenteditor

We are using Aspose PDF JAVA version 24.12
We are trying to use pdfContentEditor to replace text inside an existing PDF file.

When we use the replaceText-code for a piece of text in a font with state: “Embedded Sub” we get the next error when executing in a Docker environment.
Format of font “Symbol” is not supported for new composite font

We have configured:
replaceTextStrategy.setNoCharacterBehavior(ReplaceTextStrategy.NoCharacterAction.UseStandardFont);

But we still get the exception as listed above.
We can think of a few options:

  1. Fully embed the fonts (not always possible)
  2. Specify a standard font? The option: ReplaceTextStrategy.NoCharacterAction.UseStandardFont suggests that something like that is possible.

Can you tell us how to indicated a “StandardFont” in Java?. What are the standard fonts?

@vancar0

Standard Fonts are usually Microsoft Core Fonts that are already present in Windows environment and in Linux like environment, they can be installed by installing the msttcorefonts package. However, can you please share some more details like sample file and the code snippet with complete error details so that we can log an investigation ticket and share the ID with you.

        try (var pdfContentEditor = new PdfContentEditor())
        {
            // Use fallback to standard font if character information is not present in PDF-file
            ReplaceTextStrategy replaceTextStrategy = new ReplaceTextStrategy();
            replaceTextStrategy.setNoCharacterBehavior(ReplaceTextStrategy.NoCharacterAction.UseStandardFont);
            pdfContentEditor.setReplaceTextStrategy(replaceTextStrategy);

            String tempFilename = tempFile.toString();

            pdfContentEditor.bindPdf(tempFilename);

            // Replace the placeholders in the PDF file
            var substitutionCount = substitutePlaceholders(pdfContentEditor, substitutions);

Stacktrace
2025-01-23 08:52:23 Caused by: com.aspose.pdf.internal.ms.System.l7k: Format of font “Symbol” is not supported for new composite fonts
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7v.lj.lf(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7v.lj.(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l8y.lf.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7v.l1p.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7p.le.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7u.lI.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.internal.l7h.lI.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.TextSegment.setText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.TextFragment.setText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.facades.PdfContentEditor.replaceText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.facades.PdfContentEditor.replaceText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
2025-01-23 08:52:23 at com.aspose.pdf.facades.PdfContentEditor.replaceText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]

@vancar0

Can you please share the sample file with us as well? Also, the code sample seems incomplete.

See below for a code example. See also attached pdf
Tried replaceText with and without TextState-argument (which is created with an existing font name).
All attempts result in: “Format of font “Symbol” is not supported for new composite fonts”
KarolisTestDoc.pdf (84.4 KB)

    private void test()
    {
        try (var pdfContentEditor = new PdfContentEditor())
        {
            // Use fallback to standard font if character information is not present in PDF-file
            ReplaceTextStrategy replaceTextStrategy = new ReplaceTextStrategy();
            replaceTextStrategy.setNoCharacterBehavior(ReplaceTextStrategy.NoCharacterAction.UseStandardFont);
            pdfContentEditor.setReplaceTextStrategy(replaceTextStrategy);

            String tempFilename = "test.pdf";
            pdfContentEditor.bindPdf(tempFilename);

            int substitutionCount = 0;
            while (pdfContentEditor.replaceText("{toReplace}", "Approved by Author "))
            {
                substitutionCount++;
            }

            TextState textState = new TextState("DejaVu Sans");
            while (pdfContentEditor.replaceText("{toReplace}", "Approved by Author ", textState))
            {
                substitutionCount++;
            }

            if (substitutionCount > 0)
            {
                // Save the updated PDF document
                pdfContentEditor.save(tempFilename);
            }
        }

    }

@vancar0

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-44681

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Issue seems related to Linux. Which does not pickup the setting:
ReplaceTextStrategy.NoCharacterAction.UseStandardFont

When I use a PDF with a font not installed on my Windows system, it does an automatic fallback, to a default font.
Also generating a new TextState with correct font the behaviour does not changed when running under Linux.

@vancar0

Thanks for sharing this information. We will investigate from this perspective and let you know as soon as the ticket is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

While doing more attempts I added next code

FontRepository.setReplaceNotFoundFonts(true);

This does not resolve the issue, but it changes the stacktrace. It would be really nice if you are able to define some work-around.
Is there a different function that allows modifying text (without these font issues) in an existing PDF.

Next to that is there documentation on how next classes work together when using PdfContentEditor (they all seem related but there dependencies are unclear to me):

FontRepository, TextDefaults, TextState, ReplaceTextStrategy

Caused by: java.lang.NullPointerException: Cannot invoke "com.aspose.pdf.internal.l7v.l0n.lf()" because "<parameter3>" is null
at com.aspose.pdf.internal.l7v.lj.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l7v.lj.<init>(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l8y.lf.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l7v.l1p.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l7p.le.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l7u.lI.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.internal.l7h.lI.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.TextSegment.setText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.TextFragment.setText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.facades.PdfContentEditor.replaceText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.aspose.pdf.facades.PdfContentEditor.replaceText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
at com.bwise.logic.integrations.documentconverter.PdfTextReplacer.replaceText(PdfTextReplacer.java:311) ~[storage-integrations-5.8-SNAPSHOT.jar:5.8-SNAPSHOT]
at com.bwise.logic.integrations.documentconverter.PdfTextReplacer.substitutePlaceholders(PdfTextReplacer.java:298) ~[storage-integrations-5.8-SNAPSHOT.jar:5.8-SNAPSHOT]

@vancar0

Sure, we will consider this feedback as well and implement necessary logic accordingly. We will inform you once the issue is resolved.

More feedback.
In essence I can get the application to work.
Only requirement seems to be that the font times.ttf is available.
But, only the times.ttf from Microsoft seems to work. If that file is not found or it is not the expected TTF. I get the error: “Format of font “Symbol” is not supported for new composite font”
Putting the symbol.ttf in Linux does not solve the issue, adding times.ttf does solve the problem.

Seems like there is some hard-coded dependency on the times.ttf from Microsoft.
Can you at least improve the error message. Now everyone is search for a problem with their symbol-font…
See also: Aspose PDF.Net in Linux Docker Container. Errors about Symbol font - Free Support Forum - aspose.com

Please do something about this. Thanks

@vancar0

Sure, we have noted and taken this feedback into our account. We will consider it while investigating the ticket and let you know as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.