Building tables dynamically


#1

I am currently evaluating Aspose Word for replacing my existing routines for building Word docs (using Word and COM automation). My application is an education tool that automates the creation of student guides. Currently I use the DOM to create new tables like this:

myDoc.Tables.Add(myDoc.Paragraphs(i).Range, 1, 3)

In Aspose Word, I found no equivalent for this. It seems like I need to use the DocBuilder to start a new table or something like that. Is that correct? For me the scenario with creating a standard Word doc with merge fields upfront is a no-go, as I have no idea upfront how large the documents are going to be.

Please advice...


#2

Yes, the DocumentBuilder class is used to dynamically build document content including tables. Here is an example of how to build a simple table:


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

builder.CellFormat.Width = 100f;

builder.InsertCell();

builder.Writeln(“Row 1, Cell 1”);

builder.InsertCell();

builder.Writeln(“Row 1, Cell 2”);

builder.EndRow();

builder.InsertCell();

builder.Writeln(“Row 2, Cell 1”);

builder.InsertCell();

builder.Writeln(“Row 2, Cell 2”);

builder.EndRow();

builder.EndTable();

doc.Save(“new.doc”);



#3

Thanks Dmitry. Could you also provide an example of how to specify border styles for each of the cells? I was struggling with the .cellformat but the intend of this seems to be to return just any of the borders for modification in another way. I couldn’t find out how.

Thanks,


Mick


#4

Sure Mick. Let’s update the example so each of the cells now has its own border style (I’ve highlighted the changes):


Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

builder.CellFormat.Width = 100f;

builder.InsertCell();

builder.CellFormat.Borders[BorderType.Top].LineStyle = LineStyle.Double;

builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Double;

builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Double;

builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Double;

builder.Writeln(“Row 1, Cell 1”);

builder.InsertCell();

builder.CellFormat.Borders.ClearFormatting();

builder.CellFormat.Borders[BorderType.Top].LineStyle = LineStyle.Dot;

builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Dot;

builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Dot;

builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Dot;

builder.Writeln(“Row 1, Cell 2”);

builder.EndRow();

builder.InsertCell();

builder.CellFormat.Borders.ClearFormatting();

builder.CellFormat.Borders[BorderType.Top].LineStyle = LineStyle.Emboss3D;

builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Emboss3D;

builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Emboss3D;

builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Emboss3D;

builder.Writeln(“Row 2, Cell 1”);

builder.InsertCell();

builder.CellFormat.Borders.ClearFormatting();

builder.CellFormat.Borders[BorderType.Top].LineStyle = LineStyle.Thick;

builder.CellFormat.Borders[BorderType.Bottom].LineStyle = LineStyle.Thick;

builder.CellFormat.Borders[BorderType.Left].LineStyle = LineStyle.Thick;

builder.CellFormat.Borders[BorderType.Right].LineStyle = LineStyle.Thick;

builder.Writeln(“Row 2, Cell 2”);

builder.EndRow();

builder.EndTable();

doc.Save(“new.doc”);


In this case, there’s no actually need to specify all four sides of the border for each cell because some of the sides are adjacent.