Free Support Forum - aspose.com

Text Box

I have an application that needs to write text to user created Word documents. The users will add a Text Box into the document indicating where they want the text to be written. I have attached a sample document that contains a background image and a text box, which should be the only two elements the documents contain. Is there a way for me to loop through the objects on the document, find the textbox, and write text to it? I have tried using the GetChildNodes method to loop through the shapes on the document. I can see that there are two; however, I'm not sure how to distinguish between the image and text box, nor how to write to it.

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

Document doc = new Document(@"Test001\Test.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(@"Test001\out.docx");

Hope this helps.

Best regards,

That's perfect. Thanks.