In my html i have several tables, i want to set the header to be repeating for all of them except the first one.
I have tried the following code, but it only repeated the main table first row, which was the title of the page, as in the attached file. How can i apply this code to all tables except the first one?
Private Function RepeatHeaderRow(ByVal doc As Document) As Document
Try
Dim nodes() As Node = doc.GetChildNodes(NodeType.Table, True).ToArray()
For Each table As Table In nodes
If table.Rows.Count > 3 Then
table.FirstRow.RowFormat.HeadingFormat = True
End If
Next
Catch ex As Exception
End Try
Return doc
End Function
Hi Amir,
Thanks for your inquiry. From memory there is no way to make a table header repeat on each page if it is nested inside another table. This is the same in MS Word which also does not allow this. In your case you might want to rework your document so that it avoids nested tables. There is code that can do this automatically if needed.
Thanks,
Can you please give me this code to try
I have tried the below but it didn’t work, all the tables were gone when i used it
'If table is nested move it into the main body.
Dim parent As Node = table.GetAncestor(NodeType.Table)
If Not parent Is Nothing Then
parent.ParentNode.InsertAfter(table, parent)
parent.Remove()
End If
Hi Amir,
I believe this is happening because you have tables nested within tables within tables. Try using this code below instead:
Private Shared Sub UnnestTables(ByVal doc As Document)
'Converting to a static array ensures that a table is not processed twice
Dim nodes() As Node = doc.GetChildNodes(NodeType.Table, True).ToArray()
'When we insert the tables into the body after the parent table, they will be inserted directly after the parent
'and infront of the other nodes. This means the tables will end up inserted in the wrong way. Reverse the order
'in which we process them in to avoid this.
Dim list As New ArrayList(nodes)
list.Reverse()
For Each table As Table In list
'Gets the depth of the nested table, 0 = Not nested in a table
' 1 = Nested inside one table
' 2 = Nested inside two tables etc
'We only want to move the immediate nested tables
If GetDepthOfNodeType(table, NodeType.Table) = 1 Then
Dim parent As Table = CType(table.GetAncestor(NodeType.Table), Table)
parent.ParentNode.InsertAfter(table, parent)
End If
Next table
End Sub
Private Shared Function GetDepthOfNodeType(ByVal node As Node, ByVal type As NodeType) As Integer
Dim depth As Integer = 0
Dim parent As Node = node.GetAncestor(type)
Do While parent IsNot Nothing
depth += 1
parent = parent.GetAncestor(type)
Loop
Return depth
End Function
Thanks,
Thanks for the code
It didn’t work for me as it un-nested all and every table in my page
I have changed my page by changing top level table to DIV and then it worked
Thanks
Hi Amir,
The code worked as expected on my side using your original template document. Please note you have alot of nested tables, but only the ones nested directly inside the risk matrix report are moved.
It’s great your document is working as you wanted now. If you have any further queries please feel free to ask.
Thanks,