Free Support Forum - aspose.com

How to Set Different cell borders inside single table

Hi,


Trying to set up different table cell borders per single table:


[Test]<o:p></o:p>

public void FakeTableInsideTable()

{

var doc = new Document();

var builder = new DocumentBuilder(doc);

builder.Font.Size = 14;

// Master Lines

var table = builder.StartTable();

var cell1 = builder.InsertCell();

builder.Write("Table1.row1.col1");

var cell2 = builder.InsertCell();

builder.Write("Table1.row1.col2");

var cell3 = builder.InsertCell();

builder.Write("Table1.row1.col3");

builder.EndRow();

cell1 = builder.InsertCell();

builder.Write("Table1.row2.col1");

cell2 = builder.InsertCell();

builder.Write("Table1.row2.col2");

cell3 = builder.InsertCell();

builder.Write("Table1.row2.col3");

builder.EndRow();

// sublines

cell1 = builder.InsertCell();

builder.Font.Size = 8;

cell1.CellFormat.Borders.Bottom.ClearFormatting();

cell1.CellFormat.Borders.Right.ClearFormatting();

builder.Write("Table2.row1.col1");

cell2 = builder.InsertCell();

cell2.CellFormat.Borders.Bottom.ClearFormatting();

cell2.CellFormat.Borders.Right.ClearFormatting();

builder.Write("Table2.row1.col2");

cell3 = builder.InsertCell();

cell3.CellFormat.Borders.Bottom.ClearFormatting();

builder.Write("Table2.row1.col3");

builder.EndRow();

cell1 = builder.InsertCell();

cell1.CellFormat.Borders.Right.ClearFormatting();

builder.Write("Table2.row2.col1");

cell2 = builder.InsertCell();

cell2.CellFormat.Borders.Right.ClearFormatting();

builder.Write("Table2.row2.col2");

cell3 = builder.InsertCell();

builder.Write("Table2.row2.col3");

builder.EndRow();

// Master Lines

builder.Font.Size = 14;

cell1 = builder.InsertCell();

builder.Write("Table1.row3.col1");

cell2 = builder.InsertCell();

builder.Write("Table1.row3.col2");

cell3 = builder.InsertCell();

builder.Write("Table1.row3.col3");

builder.EndRow();

builder.EndTable();


doc.Save(@"C:\\outFakeTableInsideTable.docx");

}

What I am getting and what trying to achieve is in img attachment:



ps. what a nice spaghetti coding here!

Hi Philo,


Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Hope this helps you.

The BorderCollection.ClearFormatting method removes all borders of an object and Border.ClearFormatting method resets border properties to default values. Please read following documentation link for your kind reference.
http://www.aspose.com/docs/display/wordsnet/Applying+Borders+and+Shading

var doc = new Document();<o:p></o:p>

var builder = new DocumentBuilder(doc);

builder.Font.Size = 14;

// Master Lines

var table = builder.StartTable();

var cell1 = builder.InsertCell();

builder.Write("Table1.row1.col1");

var cell2 = builder.InsertCell();

builder.Write("Table1.row1.col2");

var cell3 = builder.InsertCell();

builder.Write("Table1.row1.col3");

builder.EndRow();

cell1 = builder.InsertCell();

builder.Write("Table1.row2.col1");

cell2 = builder.InsertCell();

builder.Write("Table1.row2.col2");

cell3 = builder.InsertCell();

builder.Write("Table1.row2.col3");

builder.EndRow();

builder.CellFormat.ClearFormatting();

// sublines

var cell = builder.InsertCell();

builder.Font.Size = 8;

builder.Write("Table2.row1.col1");

//Removes all borders of an object.

cell.CellFormat.Borders.ClearFormatting();

//Set line width of left border

cell.CellFormat.Borders.Left.LineWidth = 1.0;

cell = builder.InsertCell();

builder.Write("Table2.row1.col2");

cell.CellFormat.Borders.ClearFormatting();

cell = builder.InsertCell();

builder.Write("Table2.row1.col3");

cell.CellFormat.Borders.ClearFormatting();

cell.CellFormat.Borders.Right.LineWidth = 1.0;

builder.EndRow();

cell = builder.InsertCell();

builder.Write("Table2.row2.col1");

cell.CellFormat.Borders.ClearFormatting();

cell.CellFormat.Borders.Left.LineWidth = 1.0;

cell = builder.InsertCell();

builder.Write("Table2.row2.col2");

cell.CellFormat.Borders.ClearFormatting();

cell = builder.InsertCell();

builder.Write("Table2.row2.col3");

cell.CellFormat.Borders.ClearFormatting();

cell.CellFormat.Borders.Right.LineWidth = 1.0;

builder.EndRow();

builder.CellFormat.ClearFormatting();

// Master Lines

builder.Font.Size = 14;

cell1 = builder.InsertCell();

builder.Write("Table1.row3.col1");

cell2 = builder.InsertCell();

builder.Write("Table1.row3.col2");

cell3 = builder.InsertCell();

builder.Write("Table1.row3.col3");

builder.EndRow();

builder.EndTable();

doc.Save(MyDir + "Out.docx");