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

3

5

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?

Dermatoglyphics answered 25/3, 2015 at 3:33 Comment(0)
D
4

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.

Dermatoglyphics answered 10/4, 2015 at 21:8 Comment(0)
S
7

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.

Swec answered 28/9, 2017 at 6:32 Comment(1)
Thanks Miguel. Although that was more than 2 years ago, I remember trying this approach, especially with Type = TableLayoutValues.Fixed. It still did not help. There must have been something else in the fray. At any rate, my applied solution, though a little hacky, has been working fine. No need to touch it anymore.Dermatoglyphics
D
4

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.

Dermatoglyphics answered 10/4, 2015 at 21:8 Comment(0)
M
0

I used this code to adjust particular column's width, while all others column are automatically adjusted based on their content.

Table tbl = new Table();

TableProperties tableProp = new TableProperties();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };

// Make the table width 100% of the page width.
TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };

tableProp.Append(tableStyle, tableWidth);

tbl.AppendChild(tableProp);

//Add n columns to table
TableGrid tg = new TableGrid(new GridColumn(), new GridColumn());

tbl.AppendChild(tg);

TableRow tr1 = new TableRow();

//I Manually adjust width of the first column
TableCell tc1 = new TableCell(GenerateTableCellPropsWithWidth("270"), new Paragraph(new Run(new Text("№"))));

//All other column are adjusted based on their content
TableCell tc2 = new TableCell(GenerateTableCellPropsWithWidth(), new Paragraph(new Run(new Text("Title"))));

tr1.Append(tc1, tc2);
tbl.AppendChild(tr1);

//This method is only used for headers, while regular rows cells contain no TableCellProperties
private TableCellProperties GenerateTableCellPropsWithWidth(string width = null)
{
    TableCellProperties tcp = new TableCellProperties();
    tcp.AppendChild(width.IsNullOrEmpty()
        ? new TableCellWidth {Type = TableWidthUnitValues.Auto}
        : new TableCellWidth {Type = TableWidthUnitValues.Pct, Width = width});
    return tcp;
}
Misgovern answered 27/10, 2021 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.