Spire.Doc provides several overloaded Replace methods to replace text in different scenarios. This article is going to show you how to replace a specified text in a template document with another document using Spire.Doc.
The template document:
The document to replace text:
Detail steps:
Step 1: Load a template document.
Document document = new Document("Template.docx");
Step 2: Load another document to replace text.
IDocument replaceDocument = new Document("Document1.docx");
Step 3: Replace specified text with the other document.
document.Replace("Document 1", replaceDocument, false, true);
Step 4: Save the file.
document.SaveToFile("Output.docx", FileFormat.Docx2013);
Output:
Full code:
using Spire.Doc; using Spire.Doc.Interface; namespace Replace_Text_With_Document { class Program { static void Main(string[] args) { //Load a template document Document document = new Document("Template.docx"); //Load another document to replace text IDocument replaceDocument = new Document("Document1.docx"); //Replace specified text with the other document document.Replace("Document 1", replaceDocument, false, true); //Save the file document.SaveToFile("Output.docx", FileFormat.Docx2013); } } }