Edit Text
The "Edit Text" feature in IronWord allows developers to modify the content of an existing DOCX document. Developers can change the text directly by accessing the Paragraphs
collection and selecting the specific Text
object within a paragraph and replacing it with the ReplaceText
method. This provides flexibility for scenarios where content needs to be dynamically updated, such as updating specific sections of a report, replacing placeholders, or modifying text based on user input or data.
The ability to edit text at a granular level ensures that precise changes can be made without affecting the structure or formatting of the rest of the document. This feature is particularly useful in automated document generation workflows where content needs frequent updates or revisions.
5 Steps to Edit Text in an Existing DOCX
- using IronWord;
- WordDocument doc = new WordDocument("sample.docx");
- doc.Paragraphs[0].ReplaceText("old text.", "This is the edited text.");
- doc.Paragraphs[1].ReplaceText(doc.Paragraphs[1].Texts[1].Text, "Updated content for the second paragraph.");
- doc.SaveAs("document.docx");
Replace Text
This code demonstrates editing specific text within an existing Word document using IronWord. A WordDocument
object is initialized to load "sample.docx
". The code then accesses the Paragraphs
collection from the WordDocument
and modifies the first and second paragraphs of the document. The first modification updates the text of the first Text
object in the first paragraph by calling ReplaceText
. The method ReplaceText
takes in two parameters, the first being the string value to find and the second being the text to replace the old value with. In this example, we provided a static string in the first parameter and updated it with the new string for the first paragraph.
However, there are times when you need a more dynamic variable. To achieve that, we can access the existing text within the second paragraph further. We first access the Paragraphs
collection, which is similar to the first instance. Still, in the old text field, we input further drill down to access the existing text by accessing the Texts
array property and then the Text
property. This method gives you complete control to find and update the existing string, ensuring confidence in your document manipulation process.
Click here to view the How-to Guide, including examples, sample code, and files >