In Aspose.Words, we normally use the Document class to create a document and the DocumentBuilder class to modify it.
The following code example shows how to open an existing document and add text into it:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting before adding text.
Aspose.Words.Font font = builder.Font;
font.Size = 16;
font.Bold = true;
font.Color = Color.Blue;
font.Name = "Arial";
font.Underline = Underline.Dash;
builder.Write("Insert text");
doc.Save("ModifiedDocument.docx");
You can also do the same using the Open XML SDK. At the same time, note that it looks somewhat more complicated and more cumbersome.
Following are the namespace we need to use:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
Below is the code explaining this functionality by using OpenAndAddTextToWordDocument as a function:
public void OpenAndAddTextToWordDocumentFeature()
{
// Open a `WordprocessingDocument` for editing using the filepath.
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(MyDir + "Document.docx", true))
{
// Assign a reference to the existing document body.
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
// Add new text.
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Append text in body - Open and add text to word document"));
}
}