Textboxes in Microsoft Word are versatile tools that enhance document layout and design. They allow users to position text independently of the main text flow, making it easier to create visually appealing documents. In some cases, you may need to extract text from textboxes for repurposing, or update the text inside a textbox to ensure clarity and relevance.
In this article, you will learn how to extract or update text in a textbox in a Word document using C# with Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Extract Text from Textbox in a Word Document
Using Spire.Doc for .NET, you can access a specific text box in a document with the Document.TextBoxes[index] property. Iterate through the text box's child objects to check if each one is a paragraph or a table. For paragraphs, retrieve the text using the Paragraph.Text property. For tables, loop through the cells to extract text from each cell.
The steps to extract text from a textbox in a Word document are as follows:
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific textbox using Document.TextBoxes[index] property
- Iterate through the child objects of the textbox.
- Determine if a child object if a paragraph. If yes, get the text from the paragraph using Paragraph.Text property.
- Determine if a child object if a table. If yes, get the text from the table using ExtractTextFromTable() method.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace ExtractTextFromTextbox { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); // Get a specific textbox TextBox textBox = document.TextBoxes[0]; // Create a StreamWriter to write extracted text to a txt file using (StreamWriter sw = File.CreateText("result.txt")) { // Iterate though child objects of the textbox foreach (DocumentObject objt in textBox.ChildObjects) { // Determine if the child object is a paragraph if (objt.DocumentObjectType == DocumentObjectType.Paragraph) { // Write paragraph text to the txt file sw.Write((objt as Paragraph).Text); } // Determine if the child object is a table if (objt.DocumentObjectType == DocumentObjectType.Table) { // Extract text from table to the txt file ExtractTextFromTable(objt as Table, sw); } } } } // Extract text from a table static void ExtractTextFromTable(Table table, StreamWriter sw) { for (int i = 0; i < table.Rows.Count; i++) { TableRow row = table.Rows[i]; for (int j = 0; j < row.Cells.Count; j++) { TableCell cell = row.Cells[j]; foreach (Paragraph paragraph in cell.Paragraphs) { sw.Write(paragraph.Text); } } } } } }
Update a Textbox in a Word Document
To update a text box, first clear its existing content using the TextBox.ChildObjects.Clear() method. Then, add a new paragraph and set its text.
The steps to update a textbox in a Word document are as follows:
- Create a Document object.
- Load a Word file using Document.LoadFromFile() method.
- Get a specific textbox using Document.TextBoxes[index] property
- Remove existing content of the textbox using TextBox.ChildObjects.Clear() method.
- Add a paragraph to the textbox using TextBox.Body.AddParagraph() method.
- Add text to the paragraph using Paragraph.AppendText() method.
- Save the document to a different Word file.
- C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace UpdateTextbox { class Program { static void Main(string[] args) { // Create a Document object Document document = new Document(); // Load a Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx"); // Get a specific textbox TextBox textBox = document.TextBoxes[0]; // Remove child objects of the textbox textBox.ChildObjects.Clear(); // Add a new paragraph to the textbox Paragraph paragraph = textBox.Body.AddParagraph(); // Set line spacing paragraph.Format.LineSpacing = 15f; // Add text to the paragraph TextRange textRange = paragraph.AppendText("The text in this textbox has been updated."); // Set font size textRange.CharacterFormat.FontSize = 15f; // Save the document to a different Word file document.SaveToFile("UpdateTextbox.docx", FileFormat.Docx2019); // Dispose resources document.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.