private void SetTagElementValue(XElement element, string value)
        {
            var textElement = DocxHelper.CreateTextElement(element.Element(WordMl.SdtContentName), element, value);

            element.Element(WordMl.SdtContentName).RemoveAll();
            element.Element(WordMl.SdtContentName).Add();
        }
        public void TestAddTextElementWithEmptyStyle()
        {
            var          textElement = new XElement("Text");
            const string TextValue   = "Some text";
            var          body        = new XElement(WordMl.BodyName, textElement);

            var created = DocxHelper.CreateTextElement(textElement, textElement.Parent, TextValue, WordMl.SmartTagName, string.Empty);

            Console.WriteLine(created);
        }
        public void TestCreateTextElementWithinParagraph()
        {
            var          textElement      = new XElement("Text");
            const string TextValue        = "Some text";
            var          paragprahElement = new XElement(WordMl.ParagraphName, new XElement(WordMl.ParagraphPropertiesName), textElement);

            var created = DocxHelper.CreateTextElement(textElement, textElement.Parent, TextValue);

            Assert.AreEqual(WordMl.TextRunName, created.Name);
            Assert.AreEqual(TextValue, created.Value);
        }
        public void TestCreateTextElementWithinBodyNamedWrap()
        {
            var          textElement = new XElement("Text");
            const string TextValue   = "Some text";
            var          body        = new XElement(WordMl.BodyName, textElement);

            var created = DocxHelper.CreateTextElement(textElement, textElement.Parent, TextValue, WordMl.SmartTagName);

            Assert.AreEqual(WordMl.SmartTagName, created.Name);
            var createdTextRun = created.Element(WordMl.TextRunName);

            Assert.IsNotNull(createdTextRun);
            Assert.AreEqual(TextValue, createdTextRun.Value);
        }
        public void TestAddTextElementWithStyle()
        {
            var textElement = new XElement(WordMl.SdtName,
                                           new XElement(WordMl.SdtPrName, new XElement(WordMl.SdtName, "TagWidthStyle")),
                                           new XElement(WordMl.SdtContentName, new XElement(WordMl.TextRunName, "text")));
            const string TextValue       = "Text with style";
            const string expectedStyleId = "SomeStyle";
            var          body            = new XElement(WordMl.BodyName, textElement);

            var created = DocxHelper.CreateTextElement(textElement, textElement.Parent, TextValue, expectedStyleId);

            Console.WriteLine(created);
            var textRunProperties = created.Element(WordMl.TextRunName).Element(WordMl.TextRunPropertiesName);

            Assert.IsNotNull(textRunProperties);
        }
Пример #6
0
        private XElement ProcessElements(IEnumerable <ItemRepeaterElement> itemRepeaterElements, DataReader dataReader, XElement start, XElement parent, int index, out ICollection <XElement> elementsBeforeNestedRepeaters, bool nested = false)
        {
            XElement result   = null;
            XElement previous = start;
            ICollection <XElement> tempElementsBeforeItemRepeaters = new List <XElement>();

            foreach (var itemRepeaterElement in itemRepeaterElements)
            {
                if (!itemRepeaterElement.IsVisible || itemRepeaterElement.IsItemIf || itemRepeaterElement.IsEndItemIf)
                {
                    continue;
                }
                if (itemRepeaterElement.IsItemHtmlContent)
                {
                    result = HtmlContentProcessor.MakeHtmlContentProcessed(itemRepeaterElement.XElement, dataReader.ReadText(itemRepeaterElement.Expression), true);
                }
                else if (itemRepeaterElement.IsIndex)
                {
                    result = DocxHelper.CreateTextElement(itemRepeaterElement.XElement,
                                                          itemRepeaterElement.XElement.Parent,
                                                          index.ToString(CultureInfo.CurrentCulture),
                                                          !nested
                                                          );
                }
                else if (itemRepeaterElement.IsItem && itemRepeaterElement.HasExpression)
                {
                    result = DocxHelper.CreateTextElement(itemRepeaterElement.XElement,
                                                          itemRepeaterElement.XElement.Parent,
                                                          dataReader.ReadText(itemRepeaterElement.Expression),
                                                          !nested
                                                          );
                }
                else
                {
                    var element = new XElement(itemRepeaterElement.XElement);
                    element.RemoveNodes();
                    result = element;
                    if (itemRepeaterElement.HasElements)
                    {
                        ICollection <XElement> bisectNestedElements = new List <XElement>();
                        this.ProcessElements(itemRepeaterElement.Elements, dataReader, null, result, index, out bisectNestedElements, true);
                        if (bisectNestedElements.Count != 0)
                        {
                            tempElementsBeforeItemRepeaters.Add(bisectNestedElements.ElementAt(0));
                        }
                    }
                    else
                    {
                        element.Value = itemRepeaterElement.XElement.Value;
                    }
                }

                if (previous != null)
                {
                    previous.AddAfterSelf(result);
                    previous = result;
                }
                else
                {
                    parent.Add(result);
                }
                if (itemRepeaterElement.IsBeforeNestedRepeater)
                {
                    tempElementsBeforeItemRepeaters.Add(result);
                }
            }
            elementsBeforeNestedRepeaters = tempElementsBeforeItemRepeaters;
            return(result);
        }
Пример #7
0
        private XElement ProcessElements(IEnumerable <ItemRepeaterElement> elements, DataReader dataReader, XElement start, XElement parent, int index, ref XElement nestedRepeaterEndElement, ref XElement endIfElement, bool nestedElement = false)
        {
            XElement result   = null;
            XElement previous = start;

            foreach (var itemRepeaterElement in elements)
            {
                var flgStucturedElementProcessed = this.CheckAndProcessStartIfElementForContinue(itemRepeaterElement,
                                                                                                 dataReader,
                                                                                                 ref endIfElement)
                                                   ||
                                                   this.CheckAndProcessEndIfElementForContinue(itemRepeaterElement,
                                                                                               ref endIfElement)
                                                   ||
                                                   this.CheckAndProcessEndItemRepeaterElementForContinue(
                    itemRepeaterElement, ref nestedRepeaterEndElement) ||
                                                   this.CheckTableElementForContinue(itemRepeaterElement);
                if (!flgStucturedElementProcessed)
                {
                    var flgNestedElementCheckedForContinue = this.CheckNestedConditionElementForContinue(
                        itemRepeaterElement, endIfElement)
                                                             ||
                                                             this.CheckNestedElementForContinue(elements.First(),
                                                                                                itemRepeaterElement,
                                                                                                nestedRepeaterEndElement);

                    if (!flgNestedElementCheckedForContinue)
                    {
                        if (itemRepeaterElement.IsItemHtmlContent)
                        {
                            result = HtmlContentProcessor.MakeHtmlContentProcessed(itemRepeaterElement.XElement,
                                                                                   dataReader.ReadText(
                                                                                       itemRepeaterElement.Expression),
                                                                                   true);
                        }
                        else if (itemRepeaterElement.IsItemRepeater)
                        {
                            var itemRepeaterTag = new ItemRepeaterTag()
                            {
                                StartItemRepeater = itemRepeaterElement.StartTag,
                                EndItemRepeater   = itemRepeaterElement.EndTag,
                                Source            = itemRepeaterElement.Expression
                            };
                            var itemRepeaterGenerator = new ItemRepeaterGenerator();
                            previous = itemRepeaterGenerator.Generate(itemRepeaterTag,
                                                                      dataReader.GetReaders(itemRepeaterTag.Source),
                                                                      previous, parent);
                            nestedRepeaterEndElement = itemRepeaterTag.EndItemRepeater;
                            result = null;
                        }
                        else if (itemRepeaterElement.IsIndex)
                        {
                            result = DocxHelper.CreateTextElement(itemRepeaterElement.XElement,
                                                                  itemRepeaterElement.XElement.Parent,
                                                                  index.ToString(CultureInfo.CurrentCulture),
                                                                  !nestedElement);
                        }
                        else if (itemRepeaterElement.IsItem)
                        {
                            result = DocxHelper.CreateTextElement(itemRepeaterElement.XElement,
                                                                  itemRepeaterElement.XElement.Parent,
                                                                  dataReader.ReadText(itemRepeaterElement.Expression),
                                                                  dataReader.ReadAttribute(itemRepeaterElement.Expression, "style"),
                                                                  !nestedElement);
                        }
                        else if (itemRepeaterElement.IsItemTable)
                        {
                            result = ItemTableGenerator.ProcessItemTableElement(itemRepeaterElement.StartTag, itemRepeaterElement.EndTag,
                                                                                dataReader);
                            if (nestedElement)
                            {
                                previous.AddAfterSelf(result);
                                previous = result;
                                result   = null;
                            }
                        }
                        else
                        {
                            var element = new XElement(itemRepeaterElement.XElement);
                            element.RemoveNodes();
                            result = element;
                            if (itemRepeaterElement.HasElements)
                            {
                                var parsedLastElement = this.ProcessElements(itemRepeaterElement.Elements, dataReader,
                                                                             previous,
                                                                             result, index, ref nestedRepeaterEndElement,
                                                                             ref endIfElement, true);
                                if (itemRepeaterElement.Elements.Any(ire => ire.XElement.IsSdt()) && DocxHelper.IsEmptyParagraph(result))
                                {
                                    result = null;
                                }
                                if (itemRepeaterElement.Elements.Any(ire => ire.IsItemTable) ||
                                    itemRepeaterElement.Elements.Any(
                                        ire =>
                                        ire.IsItemRepeater && !CheckInlineWrappingMode(ire.StartTag, ire.EndTag)))
                                {
                                    previous = parsedLastElement;
                                }
                            }
                            else
                            {
                                element.Value = itemRepeaterElement.XElement.Value;
                            }
                        }
                        if (result != null)
                        {
                            if (!nestedElement)
                            {
                                previous.AddAfterSelf(result);
                                previous = result;
                            }
                            else
                            {
                                parent.Add(result);
                            }
                        }
                    }
                    else
                    {
                        result = null;
                    }
                }
                else
                {
                    result = null;
                }
            }
            return(result ?? previous);
        }