How to Apply Different AutoFit Settings to a Table using Java

Hi,

with Aspose.Words 10.x and Aspose.Words 11 using table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW) results in the table being resized to fit the page widths. But the result is very different from what you would get in Microsoft Word as it seems that the resize doesnt take into account the preferred widths of the column but only their content.

Using table.setPreferredWidth(PreferredWidth.fromPercent(100)) however yields results closer to Word’s functionality.

My questions are:
1. is is safe to use table.setPreferredWidth(PreferredWidth.fromPercent(100)) as the mean to achieve autofit to window?
2. what is the difference between using the two methofs?

Thanks and regards,
Dragos

Hi Dragos,

Thanks for your query. I have not found any problem with AutoFitBehavior.AUTO_FIT_TO_WINDOW. It would be great, If you share your document for investigation purposes.

Here is what you can do to reproduce it:
1. create a table with 1 row and 3 cells
2. in each cell set the content to be a single word, for example Aspose
3. set the first cell width to 50pts, the second to 100pts and the third’s to 150pts
4. set the table’s autofit to window

Result: a table occupying the whole page width and with 3 identically sized columns.

To see the expected results do steps 1-4 in Word or steps 1-3 in Aspose and just step 4 in Word.

Regards,
Dragos

Hi Dragos,

Thanks for sharing the further information. Yes, the three cells will become identical width after calling autoFit method. You can set the width of each cell after using autoFit method. Please see the code below and please read following documentation link for your kind reference.

table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);

cell1.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(50));

cell2.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(100));

cell3.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(150));

Hi Dragos,


Thanks for your inquiry

If you check the details under AutoFit to Window heading (under the image) in this article here: http://docs.aspose.com/display/wordsjava/How+to++Apply+Different+AutoFit+Settings+to+a+Table

You will find that the cause for the difference is because Aspose.Words applies this setting a little differently when compared to MS Word. This is all explained in the article along with the internal steps run when you apply this setting.

This should help answer your question. Yes it is perfectly safe to use both methods. The difference is just because of the way Aspose.Words performs the AutoFit to Window setting. Setting the preferred width of the table directly avoids clearing the preferred with of the cells so the output appears closer to how MS Word performs the operation.

In most cases this should produce the desired output, however I’m not fully sure what will happen if the preferred widths do not sum close to the page width. If you have any trouble in this case, please feel free to ask for help.

Thanks,

Thanks to both of you, very useful answers.

@Tahir - setting the autofit behavior before setting the preferred widths works on Word ( with a couple of minor glitches) but on PDF the columns have identical sizes

@Adam - thanks for the heads up about the sum of columns vs the page width. And again, just like using the autofit method, the results on PDF are slightly different than the Word ones. Still they seem better than what you get using autofit.

Regards,
Dragos

Hi Dragos,

Thanks for sharing the information. It would be great, If you share your documents and code for investigation purposes.