We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Position or Move Cursor to just after Heading Paragraph & Insert a Table Shape Field etc in Word Document using C# .NET

Hi,
How can I specify where the table I would like to create be placed in a word document?
When I use
builder.StartTable();
builder.InsertCell();
It just starts creating the table at the top of the page; however, I’d like to place the table under a heading in the middle of the page.

@pgroup,

You can build logic on following code to get the desired output:

Document doc = new Document("C:\\Temp\\input.docx");

// Locate the Node or Paragraph you want to insert Table after
Paragraph targetPara = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
    {
        targetPara = para;
        break;
    }
}

// Move cursor to target Node or Paragraph
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(targetPara);

// create Table
builder.StartTable();
builder.InsertCell();
CellFormat cellFormat = builder.CellFormat;
cellFormat.Width = 250;
cellFormat.LeftPadding = 30;
cellFormat.RightPadding = 30;
cellFormat.TopPadding = 30;
cellFormat.BottomPadding = 30;
builder.Writeln("I'm a wonderful formatted cell.");
builder.EndRow();
builder.EndTable();

doc.Save("C:\\Temp\\20.8.docx"); 

In case the problem still remains, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior (if any)
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same by using Aspose.Words for .NET.