Free Support Forum - aspose.com

No warnings logged for incorrect font type

@Akash007,

Your concern makes sense. Please spare us little time to evaluate your issue thoroughly to figure it out soon.

Once we have an update on it, we will let you know.

@Amjad_Sahi Thanks

@Akash007,

Please set the warning after creating Workbook object:

Workbook workbook = new Workbook();
            workbook.getSettings().setWarningCallback( new WarningCallback1())

See the complete example for your reference:
e.g.
Sample code:

Workbook workbook = new Workbook();
        workbook.getSettings().setWarningCallback( new WarningCallback1());
        Worksheet worksheet = workbook.getWorksheets().get(0);
        Cell a = worksheet.getCells().get(CellsHelper.cellIndexToName(0, 0));
        a.setValue("RPEAsposecells");
        Style style = a.getStyle();
        style.getFont().setName("IBM Plex Sans SemiBold, Arial Black");
        a.setStyle(style);
        workbook.save("f:\\files\\out1.xlsx");

..........

class WarningCallback1 implements IWarningCallback {

    @Override
    public void warning(WarningInfo info) {
        if(info.getWarningType() == WarningType.FONT_SUBSTITUTION)
        {
            System.out.println(info.getDescription() + " In short, font is substituted ");


        }

        if(info.getWarningType() == WarningType.INVALID_FONT_NAME)
        {
            System.out.println(info.getDescription() + " In short, (invalid font)this font is not found");


        }
    }

}

Hope, this helps a bit.

@Amjad_Sahi Thanks for the timely response.

The CellsException doesn’t occur with the code snippet you’ve shared which is good. However, when i open the generated excel file, it throws an error about document recovery. I checked the internal xml files and found that the long font name is still there. So, basically the issue causing the warning hasn’t been resolved yet. Is this how it is supposed to work or the library should truncate the font or substitute the font with some default font just like it happens while saving word doc as PDF using PDFSaveOptions?

@Akash007,

Yes, this is expected behavior. If you want to get exception or customize the behavior (so the output Excel file should not prompt any error messages in MS Excel), you may try like following:
e.g.
Sample code:

 Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.getWorksheets().get(0);
        Cell a = worksheet.getCells().get(CellsHelper.cellIndexToName(0, 0));
        a.setValue("RPEAsposecells");
        Style style = a.getStyle();
        try
        {
        style.getFont().setName("IBM Plex Sans SemiBold, Arial Black");
        a.setStyle(style);
        }
        catch (CellsException ee)
        {
         if (ee.getMessage().equals("The max length of the font name is 31"))
         {

        OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.XLSX);
        workbook.save("f:\\files\\out1.xlsx", saveOptions);

         }
        }

Hope, this helps a bit.

@Akash007,

Moreover, you can set CorrectedOject in the class WarningInfo to truncate font name or set a default font name for your needs. See the sample code that will suit your needs well.
e.g
Sample code:

    Workbook workbook = new Workbook();
    workbook.getSettings().setWarningCallback(new WarningCallback());
    Worksheet worksheet = workbook.getWorksheets().get(0);
    Cell a = worksheet.getCells().get(CellsHelper.cellIndexToName(0, 0));
    a.setValue("RPEAsposecells");
    Style style = a.getStyle();
    style.getFont().setName("IBM Plex Sans SemiBold, Arial Black");
    a.setStyle(style);

    OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.XLSX);

    workbook.save("output.xlsx", saveOptions);
.........

public class WarningCallback implements IWarningCallback {

    @Override
    public void warning(WarningInfo info) {
    if(info.getWarningType() == WarningType.INVALID_FONT_NAME)
    {
    //use ErrorObject, CorrectObject
    //truncate font name
    info.setCorrectedObject(info.getErrorObject().toString().substring(0, 31));

    //or set a default font name. e.g. Arial
    //info.setCorrectedObject("Arial");
    }
    else if(info.getWarningType() == WarningType.FONT_SUBSTITUTION)
    {
    System.out.println(info.getDescription());
    }
    }

    }

Hope, this helps a bit.

@Amjad_Sahi Yes this helps. I do have one more query. I gave a random font name(e.g- ‘xs8304F’ , basically font which doesn’t exist) with length less than 31 in the excel generation code. The execution happened without any warning. This is different from aspose.words where a document being saved as PDF(using PDFSaveOptions) registers a warning of font not being found and it sets a default font on its own. Why is there this difference? Also, I see the constant WarningType.FONT_SUBSTITUTION exists in the aspose.cells library. How is this constant used then?

@Akash007,

Please note, both MS Word and MS Excel have different file formats and architectures, so you cannot evaluate and compare APIs of Aspose.Words with Aspose.Cells, neither you can parse objects of one API to other. Please note, when you specify an invalid font name (which is less than 31 chars in length), INVALID_FONT_NAME warning won’t be initialized as MS Excel can accept invalid font names in it. Please note, WarningType.FONT_SUBSTITUTION would be initialized (you will get the warning) when you are rendering to PDF file format. See the sample code on how WarningType.FONT_SUBSTITUTION would be initialized/used (Aspose.Cells would automatically use default (existing) font for substitution) for your reference:
e.g.
Sample code:

Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.getWorksheets().get(0);
        Cell a = worksheet.getCells().get(CellsHelper.cellIndexToName(0, 0));
        a.setValue("RPEAsposecells");
        Style style = a.getStyle();
        style.getFont().setName("DIBM Plexssfs");
        a.setStyle(style);
        
             PdfSaveOptions saveOptions = new PdfSaveOptions(SaveFormat.PDF);
             saveOptions.setWarningCallback(new WarningCallback1());
             workbook.save("f:\\files\\out1.pdf", saveOptions);

..........
class WarningCallback1 implements IWarningCallback {

    @Override
    public void warning(WarningInfo info) {
        if(info.getWarningType() == WarningType.INVALID_FONT_NAME)
        {
            System.out.println(info.getDescription() + " In short, (invalid font)this font is not found");
            //use ErrorObject, CorrectObject
            //truncate font name
            //info.setCorrectedObject(info.getErrorObject().toString().substring(0, 31));

            //or set a default font name. e.g. Arial
            info.setCorrectedObject("Arial");
        }
//This segment would be processed.
        else if(info.getWarningType() == WarningType.FONT_SUBSTITUTION)
        {
            System.out.println(info.getDescription());
        }
    }
}