Beispiel #1
0
        private static string GetTag(W.SdtElement marker)
        {
            var tag = marker.SdtProperties.GetFirstChild <W.Tag>();

            if (tag == null)
            {
                return(string.Empty);
            }

            return(tag.Val.Value);
        }
Beispiel #2
0
        public static Marker Create(W.SdtElement element)
        {
            var ret = new Marker();

            ret.Tag     = GetTag(element);
            ret.Element = element;
            //ret.Container = element.Parent;
            ret.Container = GetParagraph(element);

            return(ret);
        }
        /// <summary>
        /// Gets the tag value.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="templateTagPart">The template tag part.</param>
        /// <param name="tagGuidPart">The tag GUID part.</param>
        /// <returns></returns>
        protected string GetTagValue(SdtElement element, out string templateTagPart, out string tagGuidPart)
        {
            templateTagPart = string.Empty;
            tagGuidPart = string.Empty;
            Tag tag = openXmlHelper.GetTag(element);

            string fullTag = (tag == null || (tag.Val.HasValue == false)) ? string.Empty : tag.Val.Value;

            if (!string.IsNullOrEmpty(fullTag))
            {
                string[] tagParts = fullTag.Split(':');

                if (tagParts.Length == 2)
                {
                    templateTagPart = tagParts[0];
                    tagGuidPart = tagParts[1];
                }
                else if (tagParts.Length == 1)
                {
                    templateTagPart = tagParts[0];
                }
            }

            return fullTag;
        }
        /// <summary>
        /// Gets the parent container.
        /// </summary>
        /// <param name="parentContainer">The parent container.</param>
        /// <param name="placeHolder">The place holder.</param>
        /// <returns></returns>
        protected bool GetParentContainer(ref SdtElement parentContainer, string placeHolder)
        {
            bool isRefresh = false;
            MainDocumentPart mainDocumentPart = parentContainer.Ancestors<Document>().First().MainDocumentPart;
            KeyValuePair<string, string> nameToValue = this.customXmlPartHelper.GetNameToValueCollectionFromElementForType(mainDocumentPart, DocumentContainerPlaceHoldersNode, NodeType.Element).Where(f => f.Key.Equals(placeHolder)).FirstOrDefault();

            isRefresh = !string.IsNullOrEmpty(nameToValue.Value);

            if (isRefresh)
            {
                SdtElement parentElementFromCustomXmlPart = new SdtBlock(nameToValue.Value);
                parentContainer.Parent.ReplaceChild(parentElementFromCustomXmlPart, parentContainer);
                parentContainer = parentElementFromCustomXmlPart;
            }
            else
            {
                Dictionary<string, string> nameToValueCollection = new Dictionary<string, string>();
                nameToValueCollection.Add(placeHolder, parentContainer.OuterXml);
                this.customXmlPartHelper.SetElementFromNameToValueCollectionForType(mainDocumentPart, DocumentRootNode, DocumentContainerPlaceHoldersNode, nameToValueCollection, NodeType.Element);
            }

            return isRefresh;
        }
        /// <summary>
        /// Gets the SDT content of content control.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Returns the sdt content of content control</returns>
        public static OpenXmlCompositeElement GetSdtContentOfContentControl(SdtElement element)
        {
            var sdtRunELement = element as SdtRun;
            var sdtBlockElement = element as SdtBlock;
            var sdtCellElement = element as SdtCell;
            var sdtRowElement = element as SdtRow;

            if (sdtRunELement != null)
            {
                return sdtRunELement.SdtContentRun;
            }

            if (sdtBlockElement != null)
            {
                return sdtBlockElement.SdtContentBlock;
            }

            if (sdtCellElement != null)
            {
                return sdtCellElement.SdtContentCell;
            }

            if (sdtRowElement != null)
            {
                return sdtRowElement.SdtContentRow;
            }

            return null;
        }
        /// <summary>
        /// Removes the content control and keep contents.
        /// </summary>
        /// <param name="contentControl">The content control.</param>
        /// <param name="customXmlPartDocElement">The custom XML part doc element.</param>
        /// <returns>Returns the custom xml part document element list</returns>
        private IEnumerable<OpenXmlCompositeElement> RemoveContentControlAndKeepContents(SdtElement contentControl, XmlElement customXmlPartDocElement)
        {
            IList<OpenXmlCompositeElement> elementsList = new List<OpenXmlCompositeElement>();

            this.AssignContentFromCustomXmlPartForDataboundControl(contentControl, customXmlPartDocElement);

            foreach (var elem in GetSdtContentOfContentControl(contentControl).Elements())
            {
                var newElement = contentControl.Parent.InsertBefore(elem.CloneNode(true), contentControl);
                AddToListIfCompositeElement(elementsList, newElement);
            }

            contentControl.Remove();
            return elementsList;
        }
        /// <summary>
        /// Determines whether [is content control multiline] [the specified content control].
        /// </summary>
        /// <param name="contentControl">The content control.</param>
        /// <returns>
        ///   <c>true</c> if [is content control multiline] [the specified content control]; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsContentControlMultiline(SdtElement contentControl)
        {
            var contentText = contentControl.SdtProperties.Elements<SdtContentText>().FirstOrDefault();

            var isMultiline = false;

            if (contentText != null && contentText.MultiLine != null)
            {
                isMultiline = contentText.MultiLine.Value;
            }

            return isMultiline;
        }
 /// <summary>
 /// Sets the data binding.
 /// </summary>
 /// <param name="xPath">The x path.</param>
 /// <param name="element">The element.</param>
 protected void SetDataBinding(string xPath, SdtElement element)
 {
     element.SdtProperties.RemoveAllChildren<DataBinding>();
     DataBinding dataBinding = new DataBinding() { XPath = xPath, StoreItemId = new StringValue(this.customXmlPartHelper.customXmlPartCore.GetStoreItemId(element.Ancestors<Document>().First().MainDocumentPart)) };
     element.SdtProperties.Append(dataBinding);
 }
        public static void SetContentOfContentControl(SdtElement contentControl, string content)
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            content = string.IsNullOrEmpty(content) ? string.Empty : content;
            var isCombobox = contentControl.SdtProperties.Descendants<SdtContentDropDownList>().FirstOrDefault() != null;
            var isImage = contentControl.SdtProperties.Descendants<SdtContentPicture>().FirstOrDefault() != null;
            var prop = contentControl.Elements<SdtProperties>().FirstOrDefault();

            if (isCombobox)
            {
                var openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                var run = CreateRun(openXmlCompositeElement, content);
                SetSdtContentKeepingPermissionElements(openXmlCompositeElement, run);
            }

            if (isImage)
            {
                string embed = null;
                Drawing dr = contentControl.Descendants<Drawing>().FirstOrDefault();
                if (dr != null)
                {
                    DocumentFormat.OpenXml.Drawing.Blip blip = dr.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().FirstOrDefault();
                    if (blip != null)
                    {
                        embed = blip.Embed;
                    }
                }

                if (embed != null)
                {
                    var document = (Document)prop.Ancestors<Body>().FirstOrDefault().Parent;
                    IdPartPair idpp = document.MainDocumentPart.Parts.Where(pa => pa.RelationshipId == embed).FirstOrDefault();
                    if (idpp != null)
                    {
                        ImagePart ip = (ImagePart)idpp.OpenXmlPart;
                        DirectoryInfo di = new DirectoryInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                        using (FileStream fileStream = File.Open(Path.Combine(di.Parent.FullName, content), FileMode.Open))
                        {
                            ip.FeedData(fileStream);
                        }
                    }
                }
            }
            else
            {
                var openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                contentControl.SdtProperties.RemoveAllChildren<ShowingPlaceholder>();
                var runs = new List<Run>();

                if (IsContentControlMultiline(contentControl))
                {
                    var textSplitted = content.Split(Environment.NewLine.ToCharArray()).ToList();
                    var addBreak = false;

                    foreach (var textSplit in textSplitted)
                    {
                        var run = CreateRun(openXmlCompositeElement, textSplit);

                        if (addBreak)
                        {
                            run.AppendChild(new Break());
                        }

                        if (!addBreak)
                        {
                            addBreak = true;
                        }

                        runs.Add(run);
                    }
                }
                else
                {
                    runs.Add(CreateRun(openXmlCompositeElement, content));
                }

                SdtContentCell aopenXmlCompositeElement = openXmlCompositeElement as SdtContentCell;
                if (aopenXmlCompositeElement != null)
                {
                    AddRunsToSdtContentCell(aopenXmlCompositeElement, runs);
                }
                else if (openXmlCompositeElement is SdtContentBlock)
                {
                    var para = CreateParagraph(openXmlCompositeElement, runs);
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, para);
                }
                else
                {
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, runs);
                }
            }
        }
        /// <summary>
        /// Gets the text from content control.
        /// </summary>
        /// <param name="contentControl">The content control.</param>
        /// <returns>Returns the text from the content controls</returns>
        public static string GetTextFromContentControl(SdtElement contentControl)
        {
            string result = null;

            if (contentControl != null)
            {
                if (contentControl is SdtRun)
                {
                    if (IsContentControlMultiline(contentControl))
                    {
                        var runs = contentControl.Descendants<SdtContentRun>().First().Elements()
                           .Where(elem => elem is Run || elem is InsertedRun);

                        var runTexts = new List<string>();

                        foreach (var run in runs)
                        {
                            foreach (var runChild in run.Elements())
                            {
                                var runText = runChild as Text;
                                var runBreak = runChild as Break;

                                if (runText != null)
                                {
                                    runTexts.Add(runText.Text);
                                }
                                else if (runBreak != null)
                                {
                                    runTexts.Add(Environment.NewLine);
                                }
                            }
                        }

                        var stringBuilder = new StringBuilder();

                        foreach (var item in runTexts)
                        {
                            stringBuilder.Append(item);
                        }

                        result = stringBuilder.ToString();
                    }
                    else
                    {
                        result = contentControl.InnerText;
                    }
                }
                else
                {
                    result = contentControl.InnerText;
                }
            }

            return result;
        }
        /// <summary>
        /// Gets the tag.
        /// </summary>
        /// <param name="element">The SDT element.</param>
        /// <returns>
        /// Returns Tag of content control
        /// </returns>
        public static Tag GetTag(SdtElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            return element.SdtProperties.Elements<Tag>().FirstOrDefault();
        }
        void SetLogo(SdtElement element, byte[] image, WordprocessingDocument document)
        {
            try
            {
                // Assuming that the first image is the logo placeholder. This is a reasonable assumption.
                var blip = element.Descendants<Blip>().FirstOrDefault();

                string imgId = "LogoPicture";

                //assuming only one headerpart will have images
                var imagePart = document.MainDocumentPart.HeaderParts.First(p => p.ImageParts.Any())
                                    .AddImagePart(ImagePartType.Png, imgId);

                using (var imageStream = new MemoryStream(image))
                {
                    imagePart.FeedData(imageStream);
                }
                if (blip != null)
                {
                    blip.Embed = imgId;
                }
            }
            catch (Exception e)
            {
                //do nothing - just image will not be replaced
            }
        }
Beispiel #13
0
        /// <summary>
        /// Sets the content of content control.
        /// </summary>
        /// <param name="contentControl">The content control.</param>
        /// <param name="content">The content.</param>
        public void SetContentOfContentControl(SdtElement contentControl, string content)
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            content = string.IsNullOrEmpty(content) ? string.Empty : content;
            bool isCombobox = contentControl.SdtProperties.Descendants<SdtContentDropDownList>().FirstOrDefault() != null;

            if (isCombobox)
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                Run run = CreateRun(openXmlCompositeElement, content);
                SetSdtContentKeepingPermissionElements(openXmlCompositeElement, run);
            }
            else
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                contentControl.SdtProperties.RemoveAllChildren<ShowingPlaceholder>();
                List<Run> runs = new List<Run>();

                if (IsContentControlMultiline(contentControl))
                {
                    List<string> textSplitted = content.Split(Environment.NewLine.ToCharArray()).ToList();
                    bool addBreak = false;

                    foreach (string textSplit in textSplitted)
                    {
                        Run run = CreateRun(openXmlCompositeElement, textSplit);

                        if (addBreak)
                        {
                            run.AppendChild<Break>(new Break());
                        }

                        if (!addBreak)
                        {
                            addBreak = true;
                        }

                        runs.Add(run);
                    }
                }
                else
                {
                    runs.Add(CreateRun(openXmlCompositeElement, content));
                }

                if (openXmlCompositeElement is SdtContentCell)
                {
                    AddRunsToSdtContentCell(openXmlCompositeElement as SdtContentCell, runs);
                }
                else if (openXmlCompositeElement is SdtContentBlock)
                {
                    Paragraph para = CreateParagraph(openXmlCompositeElement, runs);
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, para);
                }
                else
                {
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, runs);
                }
            }
        }
Beispiel #14
0
        /// <summary>
        /// Gets the SDT content of content control.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public OpenXmlCompositeElement GetSdtContentOfContentControl(SdtElement element)
        {
            SdtRun sdtRunELement = element as SdtRun;
            SdtBlock sdtBlockElement = element as SdtBlock;
            SdtCell sdtCellElement = element as SdtCell;
            SdtRow sdtRowElement = element as SdtRow;

            if (sdtRunELement != null)
            {
                return sdtRunELement.SdtContentRun;
            }
            else if (sdtBlockElement != null)
            {
                return sdtBlockElement.SdtContentBlock;
            }
            else if (sdtCellElement != null)
            {
                return sdtCellElement.SdtContentCell;
            }
            else if (sdtRowElement != null)
            {
                return sdtRowElement.SdtContentRow;
            }

            return null;
        }
Beispiel #15
0
        /// <summary>
        /// Determines whether [is template tag equal] [the specified element].
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="placeholderName">Name of the placeholder.</param>
        /// <returns>
        ///   <c>true</c> if [is template tag equal] [the specified element]; otherwise, <c>false</c>.
        /// </returns>
        protected bool IsTemplateTagEqual(SdtElement element, string placeholderName)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            if (placeholderName == null)
            {
                throw new ArgumentNullException("placeholderName");
            }

            string templateTagPart = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(element, out templateTagPart, out tagGuidPart);
            return placeholderName.Equals(templateTagPart);
        }
 /// <summary>
 /// Sets the tag value.
 /// </summary>
 /// <param name="element">The element.</param>
 /// <param name="tagValue">The tag value.</param>
 public static void SetTagValue(SdtElement element, string tagValue)
 {
     var tag = GetTag(element);
     tag.Val.Value = tagValue;
 }
Beispiel #17
0
 /// <summary>
 /// Sets the content of content control.
 /// </summary>
 /// <param name="element">The element.</param>
 /// <param name="content">The content.</param>
 protected void SetContentOfContentControl(SdtElement element, string content)
 {
     // Set text without data binding
     this.openXmlHelper.SetContentOfContentControl(element, content);
 }
        public void AssignContentFromCustomXmlPartForDataboundControl(SdtElement element, XmlElement customPartDocElement)
        {
            // This fix is applied only for data bound content controls. It was found MergeDocuments method was not picking up data from CustomXmlPart. Thus
            // default text of the content control was there in the Final report instead of the XPath value.
            // This method copies the text from the CustomXmlPart using XPath specified while creating the Binding element and assigns that to the
            // content control
            var binding = element.SdtProperties.GetFirstChild<DataBinding>();

            if (binding == null)
            {
                return;
            }

            if (binding.XPath.HasValue)
            {
                var path = binding.XPath.Value;

                if (customPartDocElement != null)
                {
                    var mgr = new XmlNamespaceManager(new NameTable());
                    mgr.AddNamespace("ns0", this.namespaceUri.ToString());
                    var node = customPartDocElement.SelectSingleNode(path, mgr);

                    if (node != null)
                    {
                        SetContentOfContentControl(element, node.InnerText);
                    }
                }
            }
        }
Beispiel #19
0
 /// <summary>
 /// Sets the tag value.
 /// </summary>
 /// <param name="element">The element.</param>
 /// <param name="fullTagValue">The full tag value.</param>
 protected void SetTagValue(SdtElement element, string fullTagValue)
 {
     // Set the tag for the content control
     if (!string.IsNullOrEmpty(fullTagValue))
     {
         this.openXmlHelper.SetTagValue(element, fullTagValue);
     }
 }
        void SetLogo(SdtElement element, byte[] image, WordprocessingDocument document)
        {
            try
            {
                var blip = element.Descendants<Blip>().FirstOrDefault();

                var imgId = "LogoPicture";

                var existingImage = document.MainDocumentPart.GetPartById(blip.Embed);

                var imagePart = document.MainDocumentPart.HeaderParts.First(p => p.ImageParts.Any())
                                    .AddImagePart(ImagePartType.Png, imgId);

                using (var imageStream = new MemoryStream(image))
                {
                    imagePart.FeedData(imageStream);
                }
                if (blip != null)
                {
                    blip.Embed = imgId;
                }
            }
            catch (Exception e)
            {
                //do nothing - just image will not be replaced
            }
        }