SheetRender toImage Changing Font from Calibri to TimesNewRoman

I have a test excel spreadsheet that if I run through the code via SheetRender.toImage() the font changes from Calibri to TimesNewRoman. I believe this is something to do with default formatting since the cells around my Calibri cell are set to TimesNewRoman

final PageSetup setup = sheet.getPageSetup();
setup.setPrintArea(“B2”);
// Set all margins as 0
setup.setLeftMargin(0);
setup.setRightMargin(0);
setup.setTopMargin(0);
setup.setBottomMargin(0);
final SheetRender sr = new SheetRender(sheet, options);|
sr.toImage(0, tmpTableFilepath);|

I can upload the spreadsheet since it seems to be specific to this spreadsheet. Am I able to upload it?

@camilla11

Cause

The issue you’re experiencing with the font changing from Calibri to Times New Roman when using SheetRender.toImage() is likely due to default formatting settings in the Excel file. If the surrounding cells are formatted with Times New Roman, it may affect the rendering of the cell in question.

Solution

To ensure that the font remains as Calibri when rendering the image, you can explicitly set the font for the specific cell or range before calling the toImage() method. This will help maintain the desired font style during the rendering process.

Code

Here’s a sample code snippet to set the font for a specific cell:

// Load the workbook
Workbook workbook = new Workbook("path/to/your/excel/file.xlsx");
Worksheet sheet = workbook.getWorksheets().get(0);

// Set the font for the specific cell
Style style = sheet.getCells().get("B2").getStyle();
style.setFontName("Calibri");
sheet.getCells().get("B2").setStyle(style);

// Set up the page setup and margins
PageSetup setup = sheet.getPageSetup();
setup.setPrintArea("B2");
setup.setLeftMargin(0);
setup.setRightMargin(0);
setup.setTopMargin(0);
setup.setBottomMargin(0);

// Render the sheet to an image
SheetRender sr = new SheetRender(sheet, options);
sr.toImage(0, "output/path/to/image.png");

Next Steps

  1. Update your code to include the font setting for the specific cell you want to render.
  2. Test the rendering again to confirm that the font remains as Calibri.
  3. If the issue persists, check for any additional formatting settings in the Excel file that might be affecting the output.

Please implement these changes and let me know if you need further assistance!

@camilla11
Would you like to provide sample file and runnable test code? Please compress the sample file and code into zip format and upload them here. We will check it soon.

@John.He - Here is a sample project with the sample xlsx file in the resources folder. You can see the temporary png file created uses TimesNewRoman font while the xlsx file uses Calibri
ForAspose.zip (64.1 KB)

@camilla11
image1.png (6.3 KB)

Please check attached image: B2 ,Calibri, E2 timesNewRoman, C2: out image.
I could not find your issue that font was changed.

And could you confirm that Calibri font was correctly installed on your server.

Hi @simon.zhao , are you on a Windows machine? I just asked my colleague on a Windows machine to try and he also cannot reproduce it.

However I am using a Mac and am able to consistently reproduce it and have figured out the setting that seems to cause it.
On my Mac Excel I go to Format → Style
image.jpg (236.6 KB)
and then Click the Bottom Left “Format” button
image.jpg (197.9 KB)

and I select Windings and run it through the sample project and the result image is in Windings
image.jpg (74.4 KB)

However if I look at other formatting options in Excel, I see Calibri
image.jpg (144.2 KB)

If I right click my cell and click “Format Cells” I also see Calibri
image.jpg (119.0 KB)

Very strange stuff!

@camilla11,

Thanks for providing further details and screenshots.

Previously, we tested your issue on a Windows machine. However, we will now try to thoroughly evaluate it in macOS environment. By the way, you did not confirm if the Calibri font is installed on your macOS? You may open the Font Book application on your Mac to see a list of installed fonts, including Calibri or others.

Oh interesting. I did not have Calibri in my Font Book but interestingly if I I set Calibri as my Format → Style → Format for my Excel Sheet, the photo was correctly in Calibri. And if I created a new Excel sheet with Calibri, the image was also Calibri

Now that I’ve installed the Calibri font, it always correctly shows as Calibri

@camilla11,

It seems your issue was resolved after installing the required font on your macOS, correct? Please don’t hesitate to contact us anytime if you have any further questions or feedback.

@amjad.sahi Yes, but I am curious why if I don’t install Calibri, it still works as Calibri in the image as long as I don’t set that specific setting I described above.

@camilla11,

We are not entirely certain at the moment, but we will investigate why the Calibri font still appears to work (even if it is not installed) in the image on your Mac environment, provided that specific setting is not applied. We will follow up with you once we have more information.

@amjad.sahi Thank you. Also I am able to reproduce the original problem with the following file (and the same java program above) even though I have the Calibri font installed in my font book
calibriTest.xlsx.zip (18.7 KB)

If I change the font and resave the file, I can no longer reproduce. Its something to do with that specific state of the file.

@camilla11,

Thanks for the template Excel file.

I tested your scenario using the newly provided file. The Calibri font is installed on my machine. When I converted your template XLSX file to PDF using Aspose.Cells for Java, I noticed that the output PDF used Times New Roman instead of Calibri. Upon further evaluation of your template XLSX file, I observed that the text “Expense Recoveries” in cell B2 is not formatted in the “Calibri” font with the specified size but appears to use a different font. When I selected cell B2 and checked the Font combo box, “Calibri” was not highlighted as the selected font, which it should be if the font is installed. For your reference, I have included a screenshot to illustrate this…
sc_shot.png (54.8 KB)

It seems that the font of the text in cell B2 might not be “Calibri”. It could be a theme font or another type, but we are not entirely certain at this point. We will review it further and get back to you soon.

@camilla11
The font setting is very strange :
The font is "Calibri ",not “Calibri”.

@camilla11 ,

In a word, there is whitespace at the end in the font name "Calibri " of your source file.

Is it my version of Excel but I cannot see the extra space in the font setting
image.jpg (130.2 KB)

@camilla11
Please check snapshot in Windows Excel:
image.png (22.8 KB)
There is a space ends with Calibri.

It seems Mac Excel auto trims the name of font, please resave it, then open in windows Excel to check whether font name is “Calibri”.

Thank you @simon.zhao - I asked my coworker with a Windows machine to open the spreadsheet and confirm that there is a space at the end of "Calibri "

@camilla11
You are welcome! Please feel free to contact us if you have any questions.