public static void MergeWithTextReplace(string templateName, string documentName, MergeData mergeData) { var mergedDocumentName = string.Empty; var templateDirectory = ConfigurationManager.AppSettings["TemplateFolder"] ?? String.Format("{0}\\Temp\\", System.AppDomain.CurrentDomain.BaseDirectory); var templatePath = String.Format("{0}{1}", templateDirectory, templateName); var documentPath = String.Format("{0}{1}", String.Format("{0}\\Temp\\", AppDomain.CurrentDomain.BaseDirectory), documentName); File.Copy(templatePath, documentPath, true); using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentPath, true)) { wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document); // Make document readonly var docSett = wordDocument.MainDocumentPart.DocumentSettingsPart; docSett.RootElement.Append(new DocumentProtection { Edit = DocumentProtectionValues.ReadOnly }); docSett.RootElement.Save(); var body = wordDocument.MainDocumentPart.Document.Body; foreach (var mergeElement in mergeData.MergeElements) { foreach (var run in body.Descendants <Run>()) { foreach (var text in run.Descendants <Text>()) { var search = String.Format("*{0}*", mergeElement.MergeField); if (text.Text.Contains(search)) { text.Text = text.Text.Replace(search, mergeElement.MergeValue); } } } } wordDocument.Close(); } }
public static void Merge(string templateName, string documentName, MergeData mergeData) { var mergedDocumentName = string.Empty; var templateDirectory = ConfigurationManager.AppSettings["TemplateFolder"] ?? String.Format("{0}\\Templates\\", System.AppDomain.CurrentDomain.BaseDirectory); var templatePath = String.Format("{0}{1}", templateDirectory, templateName); var documentPath = String.Format("{0}{1}", String.Format("{0}\\Temp\\", System.AppDomain.CurrentDomain.BaseDirectory), documentName); File.Copy(templatePath, documentPath, true); using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentPath, true)) { wordDocument.ChangeDocumentType(WordprocessingDocumentType.Document); var mainDocPart = wordDocument.MainDocumentPart.Document; // Make document readonly var docSett = wordDocument.MainDocumentPart.DocumentSettingsPart; docSett.RootElement.Append(new DocumentProtection { Edit = DocumentProtectionValues.ReadOnly }); docSett.RootElement.Save(); foreach (var mergeElement in mergeData.MergeElements.Where(x => x.GetType() == typeof(TextMergeElement))) { var mergeField = GetMergeField(mainDocPart.Descendants <SimpleField>(), mergeElement.MergeField); if (mergeField == null) { continue; } foreach (var textField in mergeField.Descendants <Text>()) { textField.Text = mergeElement.MergeValue; } } foreach (var mergeElement in mergeData.MergeElements.Where(x => x.GetType() == typeof(ImageMergeElement))) { var bookMarkStart = mainDocPart.Descendants <BookmarkStart>().SingleOrDefault(b => b.Name == mergeElement.MergeField); if (bookMarkStart == null) { continue; } string imageRId = "r" + Guid.NewGuid().ToString().Replace("-", String.Empty); MainDocumentPart mainPart = wordDocument.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Bmp, imageRId); using (FileStream stream = new FileStream(mergeElement.MergeValue, FileMode.Open)) { imagePart.FeedData(stream); } bookMarkStart.Parent.AppendChild <Drawing>(CreateDrawing(imageRId)); } wordDocument.Close(); } }