Free Support Forum - aspose.com

Need some help with TextBox Shapes

Hi,

I’m looking for some help in dealing with text inside TextBox Shapes. I basically want to got through all TextBox Shapes in a document and see if the text meets a certain format, delete the text. If I try this:

foreach (Aspose.Words.Node node in paragraph.ChildNodes)
{
if (node.NodeType == NodeType.Shape)
{
Aspose.Words.Drawing.Shape shape = node as Aspose.Words.Drawing.Shape;

if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.TextBox)
{
Aspose.Words.Drawing.TextBox textBox = shape.TextBox;
}
}
}

I don’t know how to read the text associated with the TextBox from the object. Would it be Paragraphs? Runs?

Hi Gary,


Thanks for your inquiry. Please use the Shape.GetText method to get the text of TextBox as shown in following code snippet.

Document doc = new
Document(MyDir + “TextBox.docx”);<o:p></o:p>

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))

{

if (shape.NodeType == NodeType.Shape)

{

if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.TextBox)

{

string text = shape.GetText();

}

}

}

You can also get the child nodes of TextBox like Paragraph, Runs and get the text of Paragraph and Run nodes. Please see the following code snippet for your kind reference.


Document doc = new Document(MyDir + "TextBox.docx");

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))

{

if (shape.NodeType == NodeType.Shape)

{

if (shape.ShapeType == Aspose.Words.Drawing.ShapeType.TextBox)

{

foreach (Paragraph para in shape.GetChildNodes(NodeType.Paragraph, true))

{

string paraText = para.GetText();

}

}

}

}


Thanks Tahir,

I think that makes things a little bit clearer (slight error in the snippet with the shape.NodeType not being needed).

Can you help me with respect to the paragraph and runs? The original code was part of a much larger test code I was working on with getting the Text from inside a document that matches a certain condition. Say I want to remove all the text in a document that is written in red, what’s the best way to approach it? I need it removed from all Sections, Header/Footers, Tables and TextAreas.

I had been looping through the body paragraphs, then the header/footer paragraphs, then the tables cell paragraphs. I’m wonder if I can run the GetChildNodes across the whole document in one sweep for Paragraphs? Then use the runs to determine the font color? Or just look for all runs?

Would “wordDocument.GetChildNodes(NodeType.Run, true)” do all the above (Sections, Header/Footers, Tables and TextAreas)? Then check run.Font.Color and if red use run.Remove()? e.g:

Aspose.Words.RunCollection runCollection = wordDocument.GetChildNodes(NodeType.Run, true) as Aspose.Words.RunCollection;
for (Int32 i = runCollection.Count-1; i >= 0; i–)
{
Aspose.Words.Run run = runCollection[i];
if (run.Font.Color.R == 255 && run.Font.Color.G == 0 && run.Font.Color.B == 0)
run.Remove();
}

Hi Gary,


Thanks for your inquiry. Yes, the Document.GetChildNodes(NodeType.Run, true) returns all Run nodes in the document. You can use Document.GetChildNodes(NodeType.Run, true) to achieve your requirements.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.