0

I can create two tables easy enough but I am having trouble getting them to appear side by side like this:

Tables Side-by-Side

I am unsure how to achieve this using the Open XML SDK. I'm guessing it will either be a TableProperty or a trick with paragraphs but using the Productivity Tool I couldn't work it out. Code snippet:

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

body.Append(leftTable);

/// Do something to right table properties here?

body.Append(rightTable);

I am open to different methods, although ideally the idea would be transferable to three tables side by side too.

Adam Knights
  • 2,141
  • 1
  • 25
  • 48

1 Answers1

0

In the end I realised there are two main ways to achieve this is Word - either click a table and drag it's top left crosshair to where you want it or split the page into two columns and place a column break between the tables.

Floating Tables Method

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

/// Add table position properties and place table in top left
TableProperties tblProps = leftTable.Descendants<TableProperties>().First();
TablePositionProperties tblPos = new TablePositionProperties() { VerticalAnchor = VerticalAnchorValues.Text, TablePositionY = 1 };
TableOverlap overlap = new TableOverlap() { Val = TableOverlapValues.Overlap };
tblProps.Append(tblPos, overlap);

body.Append(leftTable);

/// Add position property to right table, set 8700 and 2400 to where you want the table
TableProperties tblProps2 = rightTable.Descendants<TableProperties>().First();
TablePositionProperties tblPos2 = new TablePositionProperties() { HorizontalAnchor = HorizontalAnchorValues.Page, VerticalAnchor = VerticalAnchorValues.Page, TablePositionX = 8700, TablePositionY = 2400 };
TableOverlap overlap2 = new TableOverlap() { Val = TableOverlapValues.Overlap };

tblProps2.Append(tblPos2, overlap2);

body.Append(rightTable);

Columns Method

Before the tables place this code to keep the prior content in one column:

/// Make sure everything else stays at one column
Paragraph oneColPara = body.AppendChild(new Paragraph());

/// Adjust current doc properties to keep things like landscape
SectionProperties sectionOneProps = null;
if (body.Descendants<SectionProperties>().Count() > 0)
  sectionOneProps = (SectionProperties)body.Descendants<SectionProperties>().First().CloneNode(true);
else
  sectionOneProps = new SectionProperties();

sectionOneProps.RemoveAllChildren<Columns>();
sectionOneProps.RemoveAllChildren<DocGrid>();

sectionOneProps.Append(new Columns(){ Space = "708" }, new DocGrid(){ LinePitch = 360 }, new SectionType { Val = SectionMarkValues.Continuous } );
oneColPara.Append(new ParagraphProperties(sectionOneProps));

Then do this in the tables part:

int LeftWidth = 2000;
int RightWidth = 2000;
int NumberOfCols = 2;
Table leftTable = StartTable(NumberOfCols, LeftWidth); // Create a basic table
Table rightTable = StartTable(NumberOfCols, RightWidth);

/// Need this blank para to line tables up
body.Append(new Paragraph());

body.Append(leftTable);

/// Place the tables side by side
body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Column })));

body.Append(rightTable);

/// Make section have 2 columns
Paragraph twoColPara = body.AppendChild(new Paragraph());

/// Adjust current doc properties to keep things like landscape
SectionProperties sectionTwoProps = null;
if (body.Descendants<SectionProperties>().Count() > 0)
  sectionTwoProps = (SectionProperties)body.Descendants<SectionProperties>().First().CloneNode(true);
else
  sectionTwoProps = new SectionProperties();

sectionTwoProps.RemoveAllChildren<Columns>();
sectionTwoProps.RemoveAllChildren<DocGrid>();

sectionTwoProps.Append(new Columns() { Space = "284", ColumnCount = 2 }, new DocGrid() { LinePitch = 360 }, new SectionType { Val = SectionMarkValues.Continuous });
twoColPara.Append(new ParagraphProperties(sectionTwoProps));
Adam Knights
  • 2,141
  • 1
  • 25
  • 48