Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to fix the width of a column using OpenXml Library in C#?

Tags:

c#

openxml-sdk

I have a Word table with 2 columns. I am trying to get Word not to squeeze column1 when column2 has long text that wraps.

here is the intended layout:

 column1   Column2                
 -------   -------                
 1.        Long text that wraps   
           a couple of times.     
           Another paragraph of   
           long text that wraps.  

 2.        More long text that    
           wraps again.           
           Also another paragraph 
           of long wrapping text. 

Notice column2 has 2 paragraphs in each cell. Not sure if this matters, but that's what has to be in there.

When the number of table rows reaches double digits and after I programmatically send the completed document to the browser on the client site (they see it as a download and open it in Word) this is what happens:

 column1   Column2                
 -------   -------                
 .         .
 .         .

 9.        Long text that wraps   
           a couple of times.     
           Another paragraph of   
           long text that wraps.  

 1         More long text that    
 0.         wraps again.           
           Also another paragraph 
           of long wrapping text. 

 1         More long text that    
 1.        wraps again.           
           Also another paragraph 
           of long wrapping text. 

Notice how "10." and "11." in column1 get wrapped (because Word decided to squeeze the width of that column,...I think)

I tried increasing the GridColumn width (even to a large value) to no avail. Word always seems to re-size the column widths to what it sees as the best fit...I think. I read on other forums (not Microsoft websites) that all width settings are considered "preferred" only. Not sure if this is true, but it seems true enough in this case!!

I have tried setting

new TableCellProperties()
{
  Width = "4000",
  Type = TableWidthUnitValues.Pct
}

also

{
  Width = "3170",
  Type = TableWidthUnitValues.Dxa
}

I have also tried table layout set to fixed:

Type = TableLayoutValues.Fixed

Also set the table cell margins and the TableCellSpacing to 0 in the table properties; but nothing helped.

Is there any OpenXml API that tells Word not to mess with a column's width?

like image 656
shakyjake Avatar asked Mar 25 '15 03:03

shakyjake


2 Answers

I had the same problem but I was able to do it using the TableLayout property.

// First, we create the table, its properties and we append it.
Table table = new Table();
TableProperties props = new TableProperties();
table.AppendChild<TableProperties>(props);

// Now we create a new layout and make it "fixed".
TableLayout tl = new TableLayout(){ Type = TableLayoutValues.Fixed };
props.TableLayout = tl;

// Then we just create a new row and a few cells and we give them a width
var tr = new TableRow();
var tc1 = new TableCell();
var tc2 = new TableCell();
tc1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2000" }));
tc2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2000" }));
table.Append(tr);

Now, when we add text to the table, this is no longer rescaled.

like image 116
Miguel Avatar answered Nov 15 '22 04:11

Miguel


For those interested, I finally resolved this.

The solution was to abandon the table object altogether. Instead I relied on Paragraphs only.

There were several discussions on the internet that clearly mentioned that column-widths in a table object in Word cannot be accurately controlled through OpenXml. I do not know for sure if this is true, but as I mentioned in my question above, nothing I tried helped. Not even fixing the width in Word itself before extracting the code from it with OpenXml SDK tools. No one offered a concrete alternative either.

But, using Paragraphs with Indentation and Justification objects and embedding a Tab character, separated the contents of the paragraph into 2 "columns," where the right column accurately wraps indented. Giving me the exact desired result as in the table in the question above. No squeezing of column1 either.

like image 40
shakyjake Avatar answered Nov 15 '22 02:11

shakyjake