Free Support Forum - aspose.com

AutoFit - how to check what option is set on a document

Hi,

I can see it is possible to set AutoFit options via AutoFit(AutoFitBehavior behaviour) method on a table object.

Can you tell how I can check what AutoFitBehavior is set on a table?

Thanks,
Lukasz

@acturisaspose,

Can you please provide complete details of your usecase? What feature exactly do you want to see in Aspose.Words API? Is the desired feature available in MS Word i.e. can you meet this requirement by using MS Word? If yes, please share complete steps in MS Word. We will then be in better position to address your concerns accordingly.

Hi,

I would like to know how to know what AutoFitBehavior is set on a particular table.

What I would like to do in code is something like this:

        var docBuilder = new DocumentBuilder(new Document());
        var table = docBuilder.StartTable();
        docBuilder.InsertCell();
        
        table.AutoFit(AutoFitBehavior.AutoFitToContents);
           ...
        if(table.AutoFit == AutoFitBehavior.AutoFitToContents)
           ...
        else
           ...

        docBuilder.EndTable();

In Microsoft Word this is available under Table Tools / Layout / Autofit drop-down.

Thank you,
Lukasz

@acturisaspose,

Thanks for your inquiry. We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-17439. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for any inconvenience.

@acturisaspose,

Regarding WORDSNET-17439, it is to update you that we have completed the analysis of this issue and come to a conclusion that we would not be able to implement the fix to this issue. Your issue (WORDSNET-17439) will be closed with ‘Won’t Fix’ resolution. Please see the following analysis details:

There is no way to find out which of the auto fit methods was applied. In MS Word there is no way to do this too - Autofit drop-down does not show which method was applied previously – you can just choose what to apply right now. Please check this screenshot.

But you may be able to get the desired output by using the existing API. For example, if AllowAutoFit == false and PreferredWidth == PreferredWidth.Auto, it means AutoFitBehavior.FixedColumnWidths was applied. If AllowAutoFit == true and Table.PreferredWidth == PreferredWidth.FromPercent(100), it means AutoFitBehavior.AutoFitToWindow was applied. the following code may work for you:

Document doc = new Document("D:\\Temp\\in.docx");

Table tab = doc.FirstSection.Body.Tables[0];

if (!tab.AllowAutoFit && tab.PreferredWidth.Equals(PreferredWidth.Auto))
{
    Console.WriteLine("AutoFitBehavior.FixedColumnWidths is applied on this table");
}
else if (tab.AllowAutoFit && tab.PreferredWidth.Equals(PreferredWidth.FromPercent(100)))
{
    Console.WriteLine("AutoFitBehavior.AutoFitToWindow is applied on this table");
}
else
{
    Console.WriteLine("AutoFitBehavior.AutoFitToContents is applied on this table");
}

Hope, this helps.

Hi Awais,

I understand. Thank you for the workaround, it seems to be what I needed :slight_smile:

Thank you,
Lukasz