No way to resize a table?


#1

Hi there,

Was testing out the capabilities of Aspose.Word and found that I had to use DocumentBuilder in order to write documents that contain nested tables.

Solved that portion but now my problem is that I have no way (at least not that I can find in the documented API) to set table properties.

Is there a way for me to resize the table so that it would fit into the parent cell margins?

Thanks and Regards.


#2

Just to add on, there seems to be no workaround possible. Specifying the perferred column width seems to be ignored as the perferred table width still stays the same.
Currently the table created by DocumentBuilder is bigger than the cell in which it is residing in, hence much contents are not viewable. A manual resize can be done by going into table propeties and setting table perferred width to 90%, however, my document contains too much of these nested tables to change each and everyone manually.

Hope that I have overlooked something!

Thanks and Regards.


#3
Ok, I figured out what is happening. When you call DocumentBuilder.MoveToMergeField it moves the cursor into the table that you have in the template. At that time DocumentBuilder.RowFormat and CellFormat become "attached" to the current position in the document. that is to the cell and row where the cursor is. So RowFormat and CellFormat take on formatting that is at that location. That location happens to have 9.75" preferred width for the table set. This is set for the big table in your document.
Then you start a new table, row and a cell and what happens is that formatting that is in RowFormat and CellFormat is copied into the new cell that you create. This gives you 9.75" preferred width for the nested table. This formatting is specified at the row level in the doc file, but the public api (RowFormat) does not expose this property yet. However you can do RowFormat.ClearFormatting() and it will solve the problem.

The code for mail merge as follows

private void HandleMergeField(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == "ScanResult")
{
string allscanresult = e.FieldValue.ToString();
allscanresult = allscanresult.Trim();
if (allscanresult!="")
{
string[] scanresultrows = allscanresult.Split(new char[] {'\n'});
builder.MoveToMergeField(e.FieldName);
// At this stage RowFormat is "attached" to the current table formatting and it contains an attribute that specifies 9.75" preferred width.
// There is no direct way to control this property in Aspose.Words yet.

builder.StartTable();
// At this stage a new table is created and RowFormat and CellFormat are "detached" from the outer table,
// but the point is they retain copies of the values. So new table has 9.75" preferred width at this stage.
// This gets rid of the existing row formatting.
builder.RowFormat.ClearFormatting();

foreach (string scanresultrow in scanresultrows)
{
// I'm not sure you really need this.
// builder.RowFormat.AllowAutoFit = false;
builder.Font.ClearFormatting();

string scanresultrowtrim = scanresultrow.TrimEnd(';');
string[] scanresultcells = scanresultrowtrim.Split(new char[] { ';' });
foreach (string scanresultcell in scanresultcells)
{
builder.InsertCell();
builder.CellFormat.Width = 100;
builder.Write(scanresultcell);
// Don't need to set this two times.
// builder.CellFormat.Width = 100;
}
builder.EndRow();
// You probably don't need this either.
// builder.RowFormat.AllowAutoFit = false;
}
builder.EndTable();
}
}
}
}

#4

Could you post/share the technique you developed to simulate nested tables by using DocumentWriter?

Thanks, Narayan


#5

Not sure that I have understood you correctly. If you mean how to make nested tables using DocumentBuilder then here is a sample code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// start table

builder.StartTable();

builder.InsertCell();

builder.Write("Text");

builder.InsertCell();

builder.Write("Text");

// start nested table

builder.StartTable();

builder.InsertCell();

builder.Write("Text");

builder.InsertCell();

builder.Write("Text");

builder.EndRow();

builder.EndTable();

// end nested table

builder.InsertCell();

builder.Write("Text");

builder.EndRow();

builder.EndTable();

// end table

string filename = Application.StartupPath + @"\TestNestedTables Out.doc";

// Save resulting document.

doc.Save(filename);

// Open saved document in MS Word.

System.Diagnostics.Process.Start(filename);

Best regards,