I am creating a Word document with OpenXml, 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.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using System;
namespace WordDocExample
{
class Program
{
static void Main(string[] args)
{
Test();
}
private static void Test()
{
string str = "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 (WordprocessingDocument doc = WordprocessingDocument.Create(@"C:UsersemanuDesktopGFAS TMPReport.docx", WordprocessingDocumentType.Document))
{
doc.AddMainDocumentPart();
doc.MainDocumentPart.Document = new Document();
doc.MainDocumentPart.Document.Body = new Body();
Body bd = 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();
}
}
private static void AddATable(Body bd)
{
Table table = new Table();
TableProperties tableProperties = new TableProperties(
new TableBorders(
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 },
new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.Single), Size = 12 }
)
);
table.AppendChild(tableProperties);
TableGrid tableGrid = new TableGrid();
tableGrid.Append(new GridColumn { Width = "2400" });
tableGrid.Append(new GridColumn { Width = "2400" });
table.AppendChild(tableGrid);
for (int i = 1; i <= 10; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= 2; j++)
{
TableCell tc = new TableCell();
tc.Append(new Paragraph(new Run(new Text($"Cell {i}-{j}"))));
tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr.Append(tc);
}
table.Append(tr);
}
bd.Append(table);
}
private static void CreateMulticolumnSection(Body bd, int cols)
{
// Create a section properties for n columns
SectionProperties sectionProperties = new SectionProperties();
Columns columns = new Columns
{
EqualWidth = true,
ColumnCount = cols
};
sectionProperties.Append(columns);
bd.Append(sectionProperties);
}
}
}
The result I get:
The result I want:
PS: I searched if there was another thread on StackOverflow 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.