Get all Textbox in Main Document

I have a document that contains several TextBoxes (Shape). I want to find all of theme in the main document (ignore Header, Footer and etc). I try do this by bellow code, but it return all shapes in Header, Footer and document!

var shapes = wordDocument.GetChildNodes(Aspose.Words.NodeType.Shape, true);
foreach (Aspose.Words.Drawing.Shape textBox in shapes)
{
   if (textBox.ShapeType != Aspose.Words.Drawing.ShapeType.TextBox)
	continue;

 ...
}

How I can do it?

@freydoonk,

Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.

Document wordDocument = new Document(MyDir + "in.docx");

foreach (Section section in wordDocument.Sections)
{
    var shapes = section.GetChildNodes(Aspose.Words.NodeType.Shape, true);
    foreach (Aspose.Words.Drawing.Shape textBox in shapes)
    {
        if (textBox.ShapeType != Aspose.Words.Drawing.ShapeType.TextBox)
            continue;
    }
}

Test.zip (22.0 KB)

I used your suggested code but it didn’t work. I attached a word document file that contains 4 TextBoxs, I want to change background color of TextBoxes which contains “Section 1” and “Section 2”, but when bellow code was run, backgouround color of all of them changed!

var wordDocument = new Document(filePath);

foreach (Section section in wordDocument.Sections)
{
  var sectionShapes = section.GetChildNodes(Aspose.Words.NodeType.Shape, true);
  
  foreach (Aspose.Words.Drawing.Shape textBox in sectionShapes)
  {
    if (textBox.ShapeType != Aspose.Words.Drawing.ShapeType.TextBox)
      continue;

    textBox.FillColor = Color.LightGray;
  }
}

@freydoonk,

Please use Section.Body.GetChildNodes method as shown below to get the desired output. Hope this helps you.

Document wordDocument = new Document(MyDir + "test.docx");

foreach (Section section in wordDocument.Sections)
{
    var shapes = section.Body.GetChildNodes(Aspose.Words.NodeType.Shape, true);
    foreach (Aspose.Words.Drawing.Shape textBox in shapes)
    {
        if (textBox.ShapeType != Aspose.Words.Drawing.ShapeType.TextBox)
            continue;

        textBox.FillColor = Color.LightGray;
    }
}

wordDocument.Save(MyDir + "out.docx");

Thank you for your prompt reply. Your suggested code worked fine. :slight_smile: