Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table AutoFit is always set to ColumnWidth Value even thou Table is Autofitted properly #246

Open
PrzemyslawKlys opened this issue Jul 22, 2018 · 5 comments

Comments

@PrzemyslawKlys
Copy link
Contributor

Following code:

// Create a document.
      using( DocX document = DocX.Create( TableSample.TableSampleOutputDirectory + @"MergeCells.docx" ) )
      {
        // Add a title.
        document.InsertParagraph( "Merge and delete cells" ).FontSize( 15d ).SpacingAfter( 50d ).Alignment = Alignment.center;

        // Add A table .               
        var t = document.AddTable( 3, 2 );
        t.Design = TableDesign.TableGrid;

        var t1 = document.InsertTable( t );

        // Add 4 columns in the table.
        t1.InsertColumn( t1.ColumnCount, true );
        t1.InsertColumn( t1.ColumnCount, true );
        t1.InsertColumn( t1.ColumnCount, true );
        t1.InsertColumn( t1.ColumnCount, true );

        // Merged Cells 1 to 4 in first row of the table.
        t1.Rows[ 0 ].MergeCells( 1, 4 );

        // Merged the last 2 Cells in the second row of the table.
        var columnCount = t1.Rows[ 1 ].ColumnCount;
        t1.Rows[ 1 ].MergeCells( columnCount - 2, columnCount - 1 );

        // Add text in each cell of the table.
        foreach( var r in t1.Rows )
        {
          for( int i = 0; i < r.Cells.Count; ++i )
          {
            var c = r.Cells[ i ];
            c.Paragraphs[ 0 ].InsertText( "Column " + i );
            c.Paragraphs[ 0 ].Alignment = Alignment.center;
          }
        }
        t1.AutoFit = AutoFit.Contents;

        // Delete the second cell from the third row and shift the cells on its right by 1 to the left.
        t1.DeleteAndShiftCellsLeft( 2, 1 );

        document.Save();
        Console.WriteLine( "\tCreated: MergeCells.docx\n" );
      }

Even thou you set

t1.AutoFit = AutoFit.Contents;

Reading

t1.Tables[0].Autofit // always returns ColumnWidth 

Table formatting works properly thou.

Issue is reported in PSWriteWord - EvotecIT/PSWriteWord#8

@PrzemyslawKlys
Copy link
Contributor Author

Seems to be related to this:

image

If you remove this line document.Tables[0].Autofit changes to Contents. Most likely requires proper update instead of hardcoding.

@XceedBoucherS
Copy link
Collaborator

Hi,

This is already fixed in v1.4 and up.
Commercial version (which is at v1.5) can be tested for free here : https://xceed.com/xceed-words-for-net/

@PrzemyslawKlys
Copy link
Contributor Author

Ah, ok. Thanks.

@XenonP
Copy link

XenonP commented Sep 22, 2023

Setting table AutoFit property to AutoFit.Contents throws exception that index is out of range. Is this only included in payed version?
image
upd: this is not related to payed version, there is some kind of issue when applying AutoFit.Contents to the table of fixed size with appended row which contains merged cells. I've moved this assignment before calling merge on inserted row collumns and it worked just fine:

var t = document.AddTable(data.Patients.Select(p => p.ProvidedServices.Count).Sum() + 1, TableLayout.Headers.Count);//N*7
//Some data is added here...
//Appending final row
var r = t.InsertRow();
var totalPrice = data.Patients.SelectMany(p => p.ProvidedServices).Sum(s => s.Price).ToString();
r.Cells[5].Paragraphs[0].Append("some data:", new Formatting()
{
    Bold = true,
    FontFamily = new Font("Times New Roman"),
    Size = 12
});
r.Cells[6].Paragraphs[0].Append(totalPrice).Alignment = Alignment.center;
r.MergeCells(0, 5);//If this row is commented code works just fine.
t.AutoFit = AutoFit.Contents;//Crashes here with shown above exception.

@evancekafando
Copy link

Hi

This is a known issue for which a fix has been implemented. It will be available in version 2.6.

Evance
The Xceed Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants