We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting u pTable Cell Widths Before Calling InsertCell() (first row has spanned cell)

I need to generate a table and columns with fixed widths…
I can follow https://docs.aspose.com/words/net/applying-formatting/ easy enough I think but my question is…what if my first row has spanned cells… before using Aspose.Words (when I was creating WordXml directly), you would specify your column collection up front along with their widths.
How would I go about doing this with Aspose.Words so that my first row with multiple spanned cells has the appropriate width. Do I just skip setting the width on the spanned cells and let them pick it up from the rows/cells below with widths set?

Thanks in advance.

Hi Terry,

Thanks for your inquiry. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/applying-formatting/

Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

I guess I just need a sample from you guys using a DocumentBuilder to create a table in a word document where you have two columns, each with a hard coded width of 1.5". But have the first column be a cell that spans both columns.
From what I’ve discovered, it looks like I have to manually set the width of the first row (spanned column) to 3". Let me know if you disagree.
The issue I have is this table is dynamically built from ‘xml instructions’ so I don’t know the width (easily) up front.

Hi Terry,

Thanks for your inquiry.

Please
note that a table in MS Word is a set of independent rows. Each row has
a set of cells independent on cells of other rows. So there is no
logical “column” in a MS Word’s table. “The 1st column” is something
like “a set of the 1st cells of each row in a table”.
For
example, it’s possible to have a table where the 1st row consists of
two cells: 2cm and 1cm and the 2nd row consists of different two cells:
1cm and 2cm of width.
I
suggest you, please set the size of each cell as shown in following code snippet. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
// Use fixed column widths.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
builder.Write("first column");
builder.CellFormat.Width = ConvertUtil.InchToPoint(3);
builder.EndRow();
// Insert a cell
builder.InsertCell();
builder.Write("second column");
builder.CellFormat.Width = ConvertUtil.InchToPoint(1.5);
// Insert a cell
builder.InsertCell();
builder.Write("second column");
builder.CellFormat.Width = ConvertUtil.InchToPoint(1.5);
builder.EndRow();
builder.EndTable();
doc.Save(MyDir + "Out.docx");

If you still face problem, please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Thanks. That’s what I’m doing.