Grec Caracteres are displayed as boxes when using aspose word java

Hello,
We use in our project the aspose word to generate pdf . we have a bug today that one of our client use in template grec caracters which are displayed as boxes when we generate the document . In our code , basically we use FontSettings.getDefaultInstance().setFontsSources(updatedFontSources) , and those updatedFontSources are setting from folder fonts in our project which contains a list of ttf files . to resolve the issue with grec caracters shoud i add the right ttf policie used for grec caracters ? I need your help please ! Thanks

@MDAKNOU

To resolve the issue of Greek characters being displayed as boxes when generating a PDF using Aspose.Words for Java, you need to ensure that the correct TrueType font (TTF) files that support Greek characters are included in your font sources.

Here are the steps you can follow:

  1. Add the Correct Fonts: Make sure that the TTF files for the fonts that support Greek characters are present in the folder you are using as your font source. Common fonts that support Greek characters include Arial, Times New Roman, and others.

  2. Set Font Sources: You can set the font sources in your code using the FontSettings class. Here’s an example of how to do this:

    import com.aspose.words.*;
    
    public class FontSettingsExample {
        public static void main(String[] args) throws Exception {
            // Set the folder containing your TTF files
            String fontFolder = "C:\\MyFonts\\"; // Update this path to your font folder
    
            // Set the font sources
            FontSettings.getDefaultInstance().setFontsSources(new FontSourceBase[] {
                new SystemFontSource(), 
                new FolderFontSource(fontFolder, true)
            });
    
            // Load your document
            Document doc = new Document("path_to_your_document.docx");
            // Save the document as PDF
            doc.save("output.pdf");
        }
    }
    
  3. Check for Font Substitution: If the specified font is not found, Aspose.Words may substitute it with a default font that does not support Greek characters. You can implement a warning callback to check for any font substitution warnings during the document processing.

  4. Test with Different Fonts: If the issue persists, try using different fonts that are known to support Greek characters. You can also specify a default font for rendering in case the desired font is not available.

By ensuring that the correct fonts are included and properly configured, you should be able to display Greek characters correctly in your generated PDF documents.

If you continue to experience issues, consider checking the Aspose documentation or community forums for additional support.

Sources:
[1]: Working with Fonts in Aspose.Words for Java

Hello,

I checked in the list of ttf we have the Arial.ttf which as you explained support greek caracters ! But we got greek caracters as boxes in result!

I can share you here the code using the FontSettings class:

public TemplateServiceImpl(AppConfiguration appConfiguration, TemplateManagerClient templateManagerClient) {
    this.appConfiguration = appConfiguration;
    this.templateManagerClient = templateManagerClient;
    var initialFontSources = getInitialFontSources();
    var updatedFontSources = addCustomFontSources(initialFontSources);
    FontSettings.getDefaultInstance().setFontsSources(updatedFontSources);
}

private FontSourceBase[] addCustomFontSources(ArrayList<FontSourceBase> initialFontSources) {
    initialFontSources.add(new FolderFontSource(appConfiguration.getFontsFolder(), false));
    return initialFontSources.toArray(new FontSourceBase[0]);
}

private ArrayList<FontSourceBase> getInitialFontSources() {
    return new ArrayList<>(Arrays.asList(FontSettings.getDefaultInstance().getFontsSources()));
}

It’s quite urgent if you can have a look at it ! please thanks!

@MDAKNOU Could you please attach your problematic input and output documents here for testing? We will check the issue and provide you more information.

Yes these are the input doc word and the output pdf :

@MDAKNOU Thank you for additional information. You have attached only template documents. If I understand correctly, your application fills the document with data and then save it as PDF. Could you please save the output as PDF and DOCX documents and attach both of them them for our reference?

Most likely the problem occurs because the fonts used in the document are not available in the environment where document is rendered to PDF. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the layout differences due to differences in fonts metrics. You can implement IWarningCallback to get a notification when font substitution is performed.
The following articles can be useful for you:
https://docs.aspose.com/words/java/specify-truetype-fonts-location/
https://docs.aspose.com/words/java/install-truetype-fonts-on-linux/

Hello @alexey.noskov

Thanks for your support , the issue is fixed now!

1 Like