Free Support Forum - aspose.com

Different getColumnWidth values on different machine (linux- windows)

Hi,

I’ve a problem in reading Cells.getColumnWidth(int column). On my machine (Windows) the getColumnWidth return for example (15.0 or 58.0), instead on the client’s server (linux) the same table returns (15.13 or 58.13). Where could be the problem? How aspose calculate the column width? it is based on screen DPI? there might be an heuristic to avoid this problem?

Thanks
Matteo

Hi,


Thanks for your posting and using Aspose.Cells.

It seems, this is a font issue. First, check what are the fonts present in your workbook using the Workbook.GetFonts() method. Then, get all of those font files and place them in some directory on your Linux machine and set the font directory.

For more help, see these articles.


Hi shakeel,

i had also thought it was a font problem, so i choose a font for the table that i was sure there was (“Arial”), but i run in the same problem. Any idea why?

Hi,


Thanks for your posting and using Aspose.Cells.

Please share your sample excel file so that we could look into this issue at our end. Please also download and try the following latest version: Aspose.Cells for Java (Latest Version) and let us know your feedback.

Hi,

i will try the last version, i’m actually on 17.1.0.
Please notice that i’ve the problems only with some linux server.

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”>public class TestColumnWidth {
public static void main(String[] args) {
try {
Workbook wk = new Workbook(“workbook_path);
Range testRange = wk.getWorksheets().getRangeByName(“TEST”);
assert testRange.getWorksheet().getCells().getColumnWidthInch(3) == 4.28125;
assert testRange.getWorksheet().getCells().getColumnWidth(3) == 58.0;


double d1 = testRange.getWorksheet().getCells().getColumnWidthInch(4);
BigDecimal bd = new BigDecimal(d1);
bd = bd.round(new MathContext(6));
double cellTwoRounded = bd.doubleValue();
assert cellTwoRounded == 1.14583;
assert testRange.getWorksheet().getCells().getColumnWidth(4) == 15.0;

d1 = testRange.getWorksheet().getCells().getColumnWidthInch(5);
bd = new BigDecimal(d1);
bd = bd.round(new MathContext(6));
double cellThreeRounded = bd.doubleValue();
assert cellThreeRounded == 1.14583;
assert testRange.getWorksheet().getCells().getColumnWidth(5) == 15.0;
} catch (Exception e) {
throw new RuntimeException();
}
}
}

Hi Matteo,


Thank you for sharing the samples.

I have checked your presented scenario in Ubuntu 15.10 and I can confirm that the said problem is only related to the missing fonts in your environment. Please note, the default font of your sample spreadsheet is Calibri therefore you need it to properly calculate the column widths. Please check the attached snapshots for the test cases. In one of the test case, I have simply executed your code, as a result I was able to see difference in column widths as compared to the results in Windows. In another result, I have set the fonts location (containing Calibri & Arial TTF files) using the FontConfigs class. This time the column widths match with what we are getting in Windows for the same scenario.

Please try the case again by first placing the Calibri & Arial font files in your problematic Linux server, then explicitly tell the API where you have placed the TTF files of both aforementioned fonts.

Hi babar,


Thanks for your help.
Just for understand better, why you need to include also the Arial TTF file? Wasn’t enough to add only the default (Calibri)? In my environment , do you think the missing font was Calibri or Arial or both? Should i set the font directory only for the missing font? If my workbook contains some cells with some of the fonts already present in the environment, the app can see them or it search them in the font directory i’ve set? The last question is, if i install the missing font in the /usr/share/fonts, i still need to set the font directory?

Thanks

Matteo
Hi Matteo,

m4tte:
Just for understand better, why you need to include also the Arial TTF file? Wasn't enough to add only the default (Calibri)?

You do not need Arial font files for the code snippet shared in this thread. You only need Calibri font files to correctly calculate the column widths unless you are auto-fitting the columns according to the content size. In that case you also require the Arial font files because a few cells in the given cell range have been formatted with Arial font. Moreover, for rendering purposes, it is best to have all the required fonts (Calibri, Tahoma & Arial) otherwise you will notice slight difference in size and shape of the contents due to font substitution mechanism triggered in such cases.

m4tte:
In my environment , do you think the missing font was Calibri or Arial or both? Should i set the font directory only for the missing font?

Please note, most of the Linux operating systems do not come with pre-installed windows fonts unless you have manually configured mscorefonts, which too does not contain the Calibri font. I think you were missing the Calibri font files which are essential for the presented scenario.

m4tte:
Should i set the font directory only for the missing font? If my workbook contains some cells with some of the fonts already present in the environment, the app can see them or it search them in the font directory i've set?

As discussed earlier, Linux environments usually lack the Windows fonts therefore it is best to inspect the Workbook for used fonts (you can use Workbook.getFonts() method to get a list of required fonts) and make all the required fonts available to the API. This is the best scenario for rendering spreadsheets. However, if you are just checking the column widths and row heights, you only require the default font of the spreadsheet which can be inspected while using the Workbook.getDefaultStyle().getFont() method. If the required fonts are present in the environment (in fonts default directory), the API will automatically pick the required fonts, otherwise you have to explicitly tell the API where to look for the fonts.

m4tte:
The last question is, if i install the missing font in the /usr/share/fonts, i still need to set the font directory?

Please note that Aspose.Cells for Java API scans the "/usr/share/fonts" directory and its sub directories for font files (TTF & TTC) as a default behaviour. That means, if you place all the required fonts on aforementioned location, you do not need to explicitly set the font location in your code.