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!
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!
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());
}
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");