This project has moved and is read-only. For the latest updates, please go here.

Table: Cell.Width property has no effect

Oct 15, 2014 at 9:56 AM
Edited Oct 15, 2014 at 10:15 AM
First of all thank you for your work on the library.

Unfortunately i run into a problem regarding the table creation. My intend is to create an invisible table for layout purposes. Therefore, i need to set the width of the cells.

I searched the forum but only found a few hints regarding the use of AutoFit which will override Cell.Width settings. But it just doesn't seem to work.
using Novacode;

namespace ReportCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            DocX document = DocX.Create("output.docx");

            Table layout_table = document.AddTable(2, 2);
            layout_table.Design = TableDesign.TableGrid;

            float availableSpace = document.PageWidth - document.MarginLeft - document.MarginRight;

            layout_table.Rows[0].Cells[0].Width = 0.5 * availableSpace;
            layout_table.Rows[1].Cells[0].Width = 0.5 * availableSpace;            
            layout_table.Rows[0].Cells[1].Width = 0.3 * availableSpace;            
            layout_table.Rows[1].Cells[1].Width = 0.3 * availableSpace;

            document.InsertTable(layout_table);
            document.Save();
        }
    }
}
Any Ideas? Thank you in advance!

edit:
I've been using DocX 1.0.0.13 and 1.0.0.14 together with a .net 4.5 solution and Word 2010.
Oct 15, 2014 at 4:30 PM
I think i found a solution to the problem by myself. The problem is that some some values at the decimal places. Therefore i round them to the nearest integral value.

The following code does work :)
using Novacode;
using System;

namespace ReportCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            DocX document = DocX.Create("output.docx");

            Table layout_table = document.AddTable(2, 2);
            layout_table.Design = TableDesign.TableGrid;

            float availableSpace = document.PageWidth - document.MarginLeft - document.MarginRight;

            layout_table.Rows[0].Cells[0].Width = Math.Round(0.5 * availableSpace);
            layout_table.Rows[1].Cells[0].Width = Math.Round(0.5 * availableSpace);
            layout_table.Rows[0].Cells[1].Width = Math.Round(0.3 * availableSpace);
            layout_table.Rows[1].Cells[1].Width = Math.Round(0.3 * availableSpace);

            document.InsertTable(layout_table);
            document.Save();
        }
    }
}
I'm just not entirely sure why.
Marked as answer by vbreuer on 10/15/2014 at 8:32 AM