Wiki
Clone wikiAspose for OpenXML / Open and add text to Word Document
OpenXML SDK:
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.
static void Main(string[] args) { string strDoc = "Sample.doc"; string strTxt = "Append text in body - OpenAndAddTextToWordDocument"; OpenAndAddTextToWordDocument(strDoc, strTxt); } private static void OpenAndAddTextToWordDocument(string filepath, string txt) { // Open a WordprocessingDocument for editing using the filepath. WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, 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(txt)); // Close the handle explicitly. wordprocessingDocument.Close(); } }
Aspose.Words
using Aspose.Words Document doc = new Document("Sample Aspose.doc"); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("Insert text"); doc.Save("Add text Out.doc");
Download
Updated