I am creating a Word document with OpenXml in VB.NET, but I am having trouble creating sections with multiple columns.
I would like to be able to say “from this point on, all content will be in x columns,” but I can’t figure out how. The documentation is sparse and I can’t understand how to do it.
I tried to create a “CreateMulticolumnSection” method, but it seems that the number of columns in the last SectionProperties
is applied to the entire document.
Here is what I tried to do.
Private Sub Test()
Dim str As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce venenatis felis at lacus volutpat, ut pretium nibh malesuada. Duis sollicitudin iaculis sodales. Phasellus molestie molestie ante, ac feugiat lectus dapibus at. Quisque non varius nunc. Sed non nibh interdum, interdum nulla sit amet, fringilla magna. Quisque lacinia fermentum libero vel sagittis. Aenean vestibulum sed velit eu vestibulum. Duis egestas, risus efficitur tempus feugiat, lacus ipsum tincidunt nunc, et pretium dui sapien sed orci. Quisque cursus egestas vehicula. Vestibulum luctus ut tellus nec mollis"
' Create a new document.
Using doc As WordprocessingDocument = WordprocessingDocument.Create("C:UsersemanuDesktopGFAS TMPReport.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document)
doc.AddMainDocumentPart()
doc.MainDocumentPart.Document = New Document()
doc.MainDocumentPart.Document.Body = New Body()
Dim bd As Body = doc.MainDocumentPart.Document.Body
'Create a single column section
CreateMulticolumnSection(bd, 1)
bd.Append(New Paragraph(New Run(New Text(str))))
' Create a 2 column section
CreateMulticolumnSection(bd, 2)
' Add a table
AddATable(bd)
'Add a text in the same section
bd.Append(New Paragraph(New Run(New Text("Here is a 2 column section!!!"))))
'Go back to 1 column section
CreateMulticolumnSection(bd, 1)
'Add a text
bd.Append(New Paragraph(New Run(New Text(str))))
' Save the document
doc.MainDocumentPart.Document.Save()
End Using
End Sub
Private Sub AddATable(bd As Body)
Dim table As New Table()
Dim tableProperties As New TableProperties(
New TableBorders(
New TopBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12},
New BottomBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12},
New LeftBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12},
New RightBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12},
New InsideHorizontalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12},
New InsideVerticalBorder() With {.Val = New EnumValue(Of BorderValues)(BorderValues.Single), .Size = 12}
)
)
table.AppendChild(tableProperties)
Dim tableGrid As New TableGrid()
tableGrid.Append(New GridColumn() With {.Width = "2400"})
tableGrid.Append(New GridColumn() With {.Width = "2400"})
table.AppendChild(tableGrid)
For i As Integer = 1 To 10
Dim tr As New TableRow()
For j As Integer = 1 To 2
Dim tc As New TableCell()
tc.Append(New Paragraph(New Run(New Text($"Cell {i}-{j}"))))
tc.Append(New TableCellProperties(New TableCellWidth() With {.Type = TableWidthUnitValues.Dxa, .Width = "2400"}))
tr.Append(tc)
Next
table.Append(tr)
Next
bd.Append(table)
End Sub
Private Sub CreateMulticolumnSection(bd As Body, cols As Integer)
' Create a section properties for two columns
Dim sectionProperties As New SectionProperties()
Dim columns As New Columns()
columns.EqualWidth = True
columns.ColumnCount = cols
sectionProperties.Append(columns)
bd.Append(sectionProperties)
End Sub
This is the result I get:
This is the result I want:
PS: I searched if there was another thread on Stack Overflow that answered the question, but I didn’t find anything that suited my case: there is something useful about individual paragraphs, but I can’t add a table to a paragraph.