Retrieving the text from textbox shapes

Hi there

I have a .docx file that contains a textbox, which in turn, contains some text. I’m wondering what the best method would be to retrieve this text. I’ve tried a few different approaches but so far nothing seems to be working. If you could direct me at some code samples or similar I would be very grateful, thanks!

Eric

Hi Eric,

Thanks for your inquiry. Please see the following link to learn how to extract text:
https://docs.aspose.com/words/net/how-to-extract-selected-content-between-nodes-in-a-document/
Please let me know in case of any issues. I will be glad to help you.
Best regards,

Hi Andrey

Thanks for your reply. I don’t think I explained myself very well I have attached the document I mentioned before, and if you open it you will see a textbox in the top right hand corner. We are successfully retrieving the text and formatting for all the other content in the file, but I can’t seem to retrieve the content inside the textbox. I’m wondering what property in your DOM exposes any text contained inside a text box.
If you could let me know or have any code samples for retrieving content inside text boxes I would be very grateful, thanks!

Eric

Hi Eric,

Thank you for additional information. In Aspose.Words object model TextBox is represented as Shape. You can use the following code to retrieve text from a TextBox:

Document doc = new Document("Customer+Advisory.docx");
// Get all shapes in the document.
NodeCollection shapesColl = doc.GetChildNodes(NodeType.Shape, true, false);
// Loop through all shapes.
foreach(Shape shape in shapesColl)
{
    Console.WriteLine(shape.ToTxt());
}

Best regards,

Hi Andrey

Thanks for your reply! That works very well ,thanks
What is the easiest way to change the text inside a textbox then?

Thanks

Eric

Hi Eric,

Thanks for your request. In this case, please try using the following code:

Document doc = new Document("Customer+Advisory.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get all shapes in the document.
NodeCollection shapesColl = doc.GetChildNodes(NodeType.Shape, true, false);
// Loop through all shapes.
foreach(Shape shape in shapesColl)
{
    if (shape.ShapeType == ShapeType.TextBox)
    {
        // Remove old text
        shape.RemoveAllChildren();
        // Add new paragraph
        shape.AppendChild(new Paragraph(doc));
        builder.MoveTo(shape.FirstParagraph);
        // Insert Text
        builder.Write("This is text inside textbox");
    }
}
doc.Save("out.docx");

Hope this helps.
Best regards,