Search code examples
c#ms-wordopenxml-sdk

DocumentFormat.OpenXml Version 2.8 not working when using MergeField


I am trying to replace mergeField with Text from my database. I have done this before with version 2.5 and it worked perfectly (.Net framework 4). I tried doing the same with .Net Core by using it with .Net standard but its giving issues. I tried debugging and found out its not finding mergeFields inserted into the document. So i copied, Cleared the document and pasted it and then it worked. after then i tried to edit it and changes did not reflect. i tried copy and paste again and it was the same merge field that is fetched. its wierd. here is a code snippet. please help.

 using (WordprocessingDocument _document = WordprocessingDocument.Open(filePath, true))
        {

            _document.ChangeDocumentType(WordprocessingDocumentType.Document);

            _document.GetMergeFields("ref_no").ReplaceWithText(refNo);
            _document.MainDocumentPart.Document.Save();

            mergeFields = _document.MainDocumentPart.RootElement.Descendants<FieldCode>().ToList();

            for (int i = 0; i < request.Data.Count; i++)
            {
                if (request.Data[i].ControlTypeId == (int)FormControlTypeEnum.DATE)
                {
                    _document.GetMergeFields("v" + request.Data[i].Index.ToString().Trim()).ReplaceWithText(DateTime.Parse(request.Data[i].Data).ToShortDateString());
                    _document.MainDocumentPart.Document.Save();
                }
                else if (request.Data[i].ControlTypeId == (int)FormControlTypeEnum.MONEY)
                {

                    _document.GetMergeFields( request.Data[i].Index.ToString().Trim()).ReplaceWithText(string.Format(formatter, "{0:c}", double.Parse(request.Data[i].Data)));
                    _document.MainDocumentPart.Document.Save();

                }
                else

                {
                    _document.GetMergeFields(request.Data[i].Index.ToString().Trim()).ReplaceWithText(request.Data[i].Data);
                    _document.MainDocumentPart.Document.Save();
                }



            }
            ////document.GetMergeFields(newDoc.jsonFields[i].controlName).ReplaceWithText(newDoc.jsonFields[i].data);
            ////document.MainDocumentPart.Document.Save();


        }







 public static IEnumerable<FieldCode> GetMergeFields(this WordprocessingDocument doc, string mergeFieldName = null)
    {
        if (doc == null)
            return null;

        List<FieldCode> mergeFields = doc.MainDocumentPart.RootElement.Descendants<FieldCode>().ToList();
        foreach (var header in doc.MainDocumentPart.HeaderParts)
        {
            mergeFields.AddRange(header.RootElement.Descendants<FieldCode>());
        }
        foreach (var footer in doc.MainDocumentPart.FooterParts)
        {
            mergeFields.AddRange(footer.RootElement.Descendants<FieldCode>());
        }

        if (!string.IsNullOrWhiteSpace(mergeFieldName) && mergeFields != null && mergeFields.Count() > 0)
            return mergeFields.WhereNameIs(mergeFieldName);

        return mergeFields;
    }

Solution

  • I had to downgrade t a lower version of the library to get things to work