Free Support Forum - aspose.com

Format rows in Word tables

Hi,

In assemble document I have word tables(not aspose) from templates.

For each table I need to update row height (excluding first row-header).

Is it possible to do in the Aspose.word?

Thank you,

pa

Hi Godson,


Thanks for your inquiry. Sure, you can use RowFormat.Height property to specify height of table row. Please see the following link for more information:
http://www.aspose.com/docs/display/wordsnet/Specifying+Row+Heights

Also, please use the following code snippet to be able to achieve what you’re looking for:

Document doc = new
Document(@“C:\Temp\input.docx”);

// Get tables from the document

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);

foreach (Table tab in tables)

{

// loop theough all rows and specify height of each row

foreach (Row row in tab.Rows)

{

// Skip the first row

if (!row.IsFirstRow)

{

row.RowFormat.Height = 36;

row.RowFormat.HeightRule = HeightRule.Exactly;

}

}

}

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


I hope, this helps.

Best regards,

Hi Awais,

Thanks for your reply.
This code works for whole document.
Is it possible to only update table (or tables) between two bookmarks?

Best Regards,

pa

Hi Godson,


Thanks for your inquiry. Sure, you can extract content (Paragrphs, Tables etc) from inside Bookmark nodes and set row height for specific Table nodes. Please read the following article for more details:
http://www.aspose.com/docs/display/wordsnet/Extract+Content+from+a+Bookmark

Best regards,

Hi Awais,

Thank you for your reply.
I do not need to extract and create another document.
I need to update table format between two bookmarks in the existing document.

Do you have a sample how to do this?

Thank you,

pa

Hi Godson,


Thanks for your inquiry. Please try running the following code snippet:

Document doc = new
Document(@“C:\Temp\in.docx”);

Bookmark bookmark = doc.Range.Bookmarks["bm"];

BookmarkStart bookmarkStart = bookmark.BookmarkStart;

BookmarkEnd bookmarkEnd = bookmark.BookmarkEnd;

ArrayList extractedNodesInclusive = ExtractContent(bookmarkStart, bookmarkEnd, true);

foreach (Node node in extractedNodesInclusive)

{

if (node.NodeType.Equals(NodeType.Table))

{

Table tab = node as Table;

// loop theough all rows and specify height of each row

foreach (Row row in tab.Rows)

{

// Skip the first row

if (!row.IsFirstRow)

{

row.RowFormat.Height = 36;

row.RowFormat.HeightRule = HeightRule.Exactly;

}

}

}

}

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



I hope, this helps.

PS: You can also access individual tables by index.

Best regards,

Thank you Awais,

I did it like this:

Bookmark b1 = docMain.Range.Bookmarks[bmName1];

Bookmark b2 = docMain.Range.Bookmarks[bmName2];

Paragraph paraOne = b1.BookmarkStart.ParentNode.NextSibling as Paragraph;

for (Node node = paraOne; node != b2.BookmarkStart.ParentNode; node = node.NextSibling)

{

if (node.NodeType == NodeType.Table)

{

Aspose.Words.Tables.Table firstTable = node as Aspose.Words.Tables.Table;

if (firstTable != null)

{

foreach (Aspose.Words.Tables.Row row in firstTable.Rows)

{

// Skip the first row

if (!row.IsFirstRow)

{

//row.RowFormat.BottomPadding = 2;

//row.RowFormat.TopPadding = 2;

row.RowFormat.Height = iheight; //18;// 36;

row.RowFormat.HeightRule = HeightRule.Exactly;

}

}

}

}

}

And it is working also.

pa

Hi,


It’s great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,

Hi Awais,

Thank you for your help.

I am trying to solve another issue with this task.

Please see two attached files: in.docx and out.docx

To get out.docx I did update rows in the table:

foreach (Aspose.Words.Tables.Row row in firstTable.Rows)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

{

// Skip the first row

if (!row.IsFirstRow)

{

row.RowFormat.Height = 19;

row.RowFormat.HeightRule = HeightRule.Exactly;

}

}

Now I have issue with vertical alignment. I did try to change padding but is not working properly also.

Is it possible after updating row height also to do vertical alignment of the content for each row?

Hi Godson,


Thanks for your inquiry. In this case, you need to set the vertical cell alignment of every Cell of Row correctly. Please see the following code:

foreach (Aspose.Words.Tables.Row
row in firstTable.Rows)

{

// Skip the first row

if (!row.IsFirstRow)

{

row.RowFormat.Height = 19;

row.RowFormat.HeightRule = HeightRule.Exactly;

foreach (Cell cell in row.Cells)

cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

}

}


I hope, this helps.

Best regards,