Duplicating Word document using OpenXml and C#

前端 未结 6 1834
野趣味
野趣味 2020-12-06 06:27

I am using Word and OpenXml to provide mail merge functionality in a C# ASP.NET web application:

1) A document is uploaded with a number of pre-defined strings for s

6条回答
  •  感动是毒
    2020-12-06 06:52

    As an addenda to the above; what's perhaps more useful is finding content controls that have been tagged (using the word GUI). I recently wrote some software that populated document templates that contained content controls with tags attached. To find them is just an extension of the above LINQ query:

    var mainDocument = doc.MainDocumentPart.Document;
    var taggedContentControls = from sdt in mainDocument.Descendants()
                                let sdtPr = sdt.GetFirstChild()
                                let tag = (sdtPr == null ? null : sdtPr.GetFirstChild())
                                where (tag != null)
                                select new
                                {
                                    SdtElem = sdt,
                                    TagName = tag.GetAttribute("val", W).Value
                                };   
    

    I got this code from elsewhere but cannot remember where at the moment; full credit goes to them.

    The query just creates an IEnumerable of an anonymous type that contains the content control and its associated tag as properties. Handy!

提交回复
热议问题