Chinese characters become blocks where converting excel to image

Hello, I’m using aspose-cells 22.5 to create excel file, and then converting the excel file to png image file. Howerver, the chineses character appear as block in the png image file, and this only happens in the linux server with centos6. Everything works fine in my own Mac computer.
image.png (44.1 KB)

// code create excel file
// fontfolder is the font where I put the customFont.ttf
FontConfigs.setFontFolder(fontFolder, false);
Workbook workbook = new Workbook();
Style defaultStyle = workbook.getDefaultStyle();
defaultStyle.getFont().setName("customFont");
workbook.setDefaultStyle(defaultStyle);
Worksheet worksheet = workbook.getWorksheets().get(0);
Cells cells = worksheet.getCells();
for (int row = 0; row < 10; row++) {
    for (int column = 0; column < 5; column++) {
        Cell cell = cells.get(row, column);
        cell.putValue("中国xxxxx");
        Style style = cell.getStyle();
        style.getFont().setName("customFont");
        cell.setStyle(style);
    }
}
workbook.save("test.xls", SaveFormat.EXCEL_97_TO_2003);
// code converting excel file to image
// fontfolder is the font where I put the customFont.ttf
FontConfigs.setFontFolder(fontFolder, false);
IndividualFontConfigs fontConfigs = new IndividualFontConfigs();
fontConfigs.setFontFolder(fontFolder, false);
LoadOptions opts = new LoadOptions(LoadFormat.EXCEL_97_TO_2003);
DefaultStyleSettings defaultStyleSettings = opts.getDefaultStyleSettings();
defaultStyleSettings.setFontName("customFont");
opts.setFontConfigs(fontConfigs);
Workbook book = new Workbook("text.xls", opts);
Worksheet sheet = book.getWorksheets().get(0);

ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setCheckWorkbookDefaultFont(true);
imgOptions.setHorizontalResolution(500);
imgOptions.setVerticalResolution(500);
imgOptions.setImageType(ImageType.PNG);
imgOptions.setAllColumnsInOnePagePerSheet(true);
imgOptions.setDefaultFont("customFont");

 SheetRender sr = new SheetRender(sheet, imgOptions);
 sr.toImage(0, "test.png");

@dingjiahao,

The issue might be due to missing fonts on linux on your end. Could you please try to substitute your custom font with some known font (which should also be installed) if it works for your needs (see the document for your reference). If you still find the issue, kindly share your customfont.ttf file. Also, save to PDF file format (at the end of your code) and provide the output PDF file. Moreover, zip and provide the output Excel file for reference. We will check it further.

I also find it’s due to missing fonts on linux, But I’m trying to run the program on the machine that I don’t have the privilege to install fonts. I understand that aspose-cells can configure the fonts that are not installed in the linux by setting the font folder. Did my codes set all font configurations correctly to using custom and my own font first?

SALE_INFO-3823f666-8b20-4e7f-a317-7239d2c85d54.xls.zip (2.6 KB)

this is the ouput of the excel file. I zipped ttf file, but the file size is restricted by the forum. I will try to output the pdf file and give you feedback.
Strangely, I click the cell of the excel file, it shows the font is “customFont”. But if I double click the cell, the font changes to “宋体” for the chinese characters.

@dingjiahao,

Please provide your output PDF file for reference. It looks like you have renamed some font to “customFont”. Did you change the name of some Chinese font and save it as?

Please zip your font file(s) and upload to some file sharing service. Share the download link seamlessly (without any id or password).

zip file.

@dingjiahao,

Thanks for the font file.

It looks like you have renamed the font and originally its title is “HYYongZiZhiRiSheng J”. So, you may try to substitute it in code accordingly, see the sample line of code for your reference.

FontConfigs.setFontSubstitutes("customFont", new String[] { "HYYongZiZhiRiSheng J" });

Alternatively, please don’t set default font for your Excel file as “customFont”. Only install the font “HYYongZiZhiRiSheng J” properly and your application should have no proper rights (read/write) to get the fonts folder you specify in the code.

Now render to image and PDF again and provide both files for reference.

I have dealt with my issue. The problem is that I put the ttf file in the resource folder of the java spring boot application and the application is a jar package on the linux server. It seems like aspose-cell cannot read the path inside the jar package. I upload the ttf file onto an oss, and read the file from the internet. And using setFontSource to load this fonts. Then it works.

@dingjiahao,

It seems you have sorted out your issue now. Feel free to write us back if you have further comments or queries.