I have a Word document with placeholders for a number of values, and so far have figured out how to replace one with a table.
My issue now is that I don't know how to set this table to have set margins from the left and right borders of the page, without making a new section.
Here's my code so far:
FileStream fileStreamPath = new FileStream(@"template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);
foreach (var placeholderTuple in placeholders)
{
document.Replace(given: placeholderTuple.placeholder,
replace: placeholderTuple.newvalue,
caseSensitive: true,
wholeWord: false);
}
WTable table = new(document);
table.ResetCells(1, 2);
// Add paragraph and text to the table Cell.
table.Rows[0].Cells[0].AddParagraph().AppendText("First Cell");
table.Rows[0].Cells[1].AddParagraph().AppendText("Last Cell");
// Initializes text body part
TextBodyPart textBodyPart = new TextBodyPart(document);
textBodyPart.BodyItems.Add(table);
document.Replace("@TableStart@", textBodyPart, true, false);
Hi Thomas,
From the given details, we have found that you are looking to set margins from
the left and right borders of the page specifically for a table. In the
Microsoft Word application, it is not possible to set margins directly from the
left and right borders of the page to a table. The option to set margins
applies to sections of the document only.
Similarly, the DocIO library does not provide an API specifically for setting
margins from the left and right borders of the page to a table.
But in the Microsoft Word application, you can align the table horizontally.
Here's how you can achieve this:
1. Right-click on the table and select "Table Properties" from the
options that appear.
|
|
2. In the Table Properties dialog box, navigate to the "Table" tab. Under the "Alignment" section, choose "Center" alignment. Click the "OK" button to apply the changes.
|
|
Same you can
align the table horizontally using our DocIO Library. To achieve this, we
recommend you to use the HorizontalAlignment
API to set the center alignment for the table. Please refer to the highlighted
code snippet below:
|
//Opens an existing Word document. using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) { WTable table = new WTable(document); //Specifies the horizontal alignment of the table table.TableFormat.HorizontalAlignment = RowAlignment.Center; table.ResetCells(1, 2); // Add paragraph and text to the table Cell. WTableCell cell = table.Rows[0].Cells[0]; cell.Width = 100; cell.AddParagraph().AppendText("First Cell"); cell = table.Rows[0].Cells[1]; cell.Width = 100; cell.AddParagraph().AppendText("Last Cell"); // Initializes text body part TextBodyPart textBodyPart = new TextBodyPart(document); textBodyPart.BodyItems.Add(table); document.Replace("@TableStart@", textBodyPart, true, false); //Creates file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Saves the Word document to file stream. document.Save(outputFileStream, FormatType.Docx); } } |
Also in the Microsoft Word application, you have the option to set the horizontal position of a table by following these steps:
1. Right-click on the table and select "Table Properties" from the options that appear.
|
|
2. In the Table Properties dialog box, go to the "Table" tab. Under the "Text wrapping" section, choose the "Around" option.
|
|
3. Next, navigate to the "Positioning" option under the "Table" tab. Specify the desired horizontal position and relative position values to position the table horizontally. Click the "OK" button to apply the changes.
|
|
Same you can set the horizontal position and relative position of a table using
our DocIO Library. To achieve this, we recommend following these steps:
1. Enable text wrapping around the table using the WrapTextAround
API.
2. Set the table's horizontal relative position using the HorizRelationTo
API.
3. Set the horizontal position of the table using the HorizPosition
API.
Please refer
to the highlighted code snippet below:
|
//Opens an existing Word document. using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx)) { WTable table = new WTable(document); ////Set table's WrapTextAround as true. table.TableFormat.WrapTextAround = true; ////Set horizantal relation of the table. table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Margin; ////Set horizantal position of the table. table.TableFormat.Positioning.HorizPosition = 100; table.ResetCells(1, 2); // Add paragraph and text to the table Cell. WTableCell cell = table.Rows[0].Cells[0]; cell.Width = 100; cell.AddParagraph().AppendText("First Cell"); cell = table.Rows[0].Cells[1]; cell.Width = 100; cell.AddParagraph().AppendText("Last Cell"); // Initializes text body part TextBodyPart textBodyPart = new TextBodyPart(document); textBodyPart.BodyItems.Add(table); document.Replace("@TableStart@", textBodyPart, true, false); //Creates file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Saves the Word document to file stream. document.Save(outputFileStream, FormatType.Docx); } } |
Regards,
Anto Nihil S