Free Support Forum - aspose.com

How to create TableCell object and TableRow object

Hi ,


is their any way in which we can set properties of TableRow and TableCell

like border , backgroundcolor , font in fucntion and return its object


and add it to builder or document .

Please help me for this with example and code…

Regards ,

Jeevan Joshi.

Hi Jeevan,


Thanks for your inquiry. Yes, you can achieve your requirements by using Aspose.Words. Please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsnet/Applying+Borders+and+Shading
http://www.aspose.com/docs/display/wordsnet/Applying+Formatting+on+the+Table+Level
http://www.aspose.com/docs/display/wordsnet/Applying+Formatting+on+the+Row+Level
http://www.aspose.com/docs/display/wordsnet/Applying+Formatting+on+the+Cell+Level

Please check the following code snippet for your kind reference.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:#2B91AF;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> doc = new Document();<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();

builder.InsertCell();

// Set the borders for the entire table.

table.SetBorders(LineStyle.Single, 2.0, Color.Black);

// Set the cell shading for this cell.

builder.CellFormat.Shading.BackgroundPatternColor = Color.Red;

builder.Writeln("Cell #1");

builder.InsertCell();

// Specify a different cell shading for the second cell.

builder.CellFormat.Shading.BackgroundPatternColor = Color.Green;

builder.Writeln("Cell #2");

// End this row.

builder.EndRow();

// Clear the cell formatting from previous operations.

builder.CellFormat.ClearFormatting();

// Create the second row.

builder.InsertCell();

// Create larger borders for the first cell of this row. This will be different

// compared to the borders set for the table.

builder.CellFormat.Borders.Left.LineWidth = 4.0;

builder.CellFormat.Borders.Right.LineWidth = 4.0;

builder.CellFormat.Borders.Top.LineWidth = 4.0;

builder.CellFormat.Borders.Bottom.LineWidth = 4.0;

builder.Writeln("Cell #3");

builder.InsertCell();

// Clear the cell formatting from the previous cell.

builder.CellFormat.ClearFormatting();

builder.Writeln("Cell #4");

doc.Save(MyDir + "Table.SetBordersAndShading Out.doc");


Regarding fonts, please note that, all text of the document is stored in runs of text. You can simply iterate through the Run nodes and get the font objects (Run.Font ) as follows:

foreach (Run r in doc.GetChildNodes(NodeType.Run, true))
{
Aspose.Words.Font f = r.Font;
}


You can get also font object by using ParagraphFormat.Style.Font as shown below:

//Get the Font Name of current Paragraph.

paragraph.ParagraphFormat.Style.Font.Name;

//Get the Font Size of current paragraph.

paragraph.ParagraphFormat.Style.Font.Size;



You can set font properties to DocumentBuilder as shown below:

DocumentBuilder.ParagraphFormat.Style.Font (Name, size, etc)

DocumentBuilder.Font (Name, size, etc)


Hope this helps you. Please let us know if you have any more queries.



Hi Tahir ,

Thanks for your reply .

See this e,g. code for documentbuilder object it works fine as I need

Dim doc As New Document(“D:\doc1.doc”)
Dim builder1 As New DocumentBuilder(doc)
builder1 = GetQue(doc)
doc.save(“D:\abc.doc”, SaveFormat.Doc)

Public Function GetQue(ByVal doc As Document) As DocumentBuilder
Dim builder As New DocumentBuilder(doc) '(doc)
builder.MoveToDocumentEnd()
builder.InsertBreak(BreakType.SectionBreakNewPage)
builder.PageSetup.PaperSize = PaperSize.A4
builder.PageSetup.RightMargin = 10.0
builder.Writeln(“ABC”)
return builder
end function
in the above code as I need builder to generate diff type of text so I kept in diff function made changes and returned builder object.

In same way I want to create Function for TableCell and TableRow

e.g.

Public Function GetCell(ByVal docMain As Document, ByVal strQuestionText As String) As Aspose.Words.Tables.Cell
Dim builder As New DocumentBuilder(docMain)
Dim cellQ As Aspose.Words.Tables.Cell

builder.Font.Name = “Arial”
builder.Font.Size = 9
builder.Font.Bold = True
builder.Font.Color = System.Drawing.ColorTranslator.FromHtml("#333333")
return cellQ
end function

My question is after getting tablecell object form Function 'GetCell ’ how to insert/add this object to table ? same case for tablerow.


Regards ,
Jeevan Joshi.

Hi Jeevan,


Thanks for your inquiry. You can easily add a Row object into Table and Cell object into Row by using CompositeNode.AppendChild method. The method adds the specified node to the end of the list of child nodes. Please check the following code snippet for your kind reference.

<span style=“font-size:
10.0pt;font-family:“Courier New”;mso-no-proof:yes”> Dim doc
As New
Document()<o:p></o:p>

' We start by creating the table object. Note how we must pass the document object

' to the constructor of each node. This is because every node we create must belong

' to some document.

Dim table As New Table(doc)

' Add the table to the document.

doc.FirstSection.Body.AppendChild(table)

' Here we could call EnsureMinimum to create the rows and cells for us. This method is used

' to ensure that the specified node is valid, in this case a valid table should have at least one

' row and one cell, therefore this method creates them for us.

' Instead we will handle creating the row and table ourselves. This would be the best way to do this

' if we were creating a table inside an algorthim for example.

Dim row As New Row(doc)

row.RowFormat.AllowBreakAcrossPages = True

table.AppendChild(row)

' We can now apply any auto fit settings.

table.AutoFit(AutoFitBehavior.FixedColumnWidths)

' Create a cell and add it to the row

Dim cell As New Cell(doc)

cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue

cell.CellFormat.Width = 80

' Add a paragraph to the cell as well as a new run with some text.

cell.AppendChild(New Paragraph(doc))

cell.FirstParagraph.AppendChild(New Run(doc, "Row 1, Cell 1 Text"))

' Add the cell to the row.

row.AppendChild(cell)

' We would then repeat the process for the other cells and rows in the table.

' We can also speed things up by cloning existing cells and rows.

row.AppendChild(cell.Clone(False))

row.LastCell.AppendChild(New Paragraph(doc))

row.LastCell.FirstParagraph.AppendChild(New Run(doc, "Row 1, Cell 2 Text"))

doc.Save(MyDir & "Table.InsertTableUsingNodes Out.doc")



Hi Tahir ,


Thanks for your reply.