private IEnumerable <OpenXmlElement> GetBookmarkElements()
        {
            var list = BkmStart.ElementsAfter()
                       .Where(p => p.IsBefore(BkmEnd));

            return(list);
        }
        private RunProperties GetDefaultRunProperties()
        {
            RunProperties rProp = null;
            var           r     = BkmStart.GetParent <Run>();

            if (r != null)
            {
                rProp = r.GetFirstDescendant <RunProperties>();
            }
            if (rProp != null)
            {
                return(rProp);
            }
            var p     = BkmStart.GetParent <Paragraph>();
            var pProp = p?.GetFirstDescendant <ParagraphMarkRunProperties>();

            if (pProp == null)
            {
                return(null);
            }
            rProp = new RunProperties();
            foreach (var pr in pProp)
            {
                rProp.AppendChild(pr.CloneNode(true));
            }
            return(rProp);
        }
 private void InsertXmlELement(OpenXmlElement element)
 {
     if (SiblingElement != null)
     {
         SiblingElement.InsertAfterSelf(element);
     }
     else
     {
         BkmStart.GetParent <Paragraph>().InsertAfter(element, BkmStart);
     }
 }
        private Text GetTextInColumn()
        {
            var text = new Text();
            var cell = BkmStart.GetParent <TableCell>();
            var res  = new StringBuilder();
            var list = cell.Descendants <Text>();

            foreach (var t in list)
            {
                res.AppendLine(t.Text);
            }
            text.Text = res.ToString().TrimEnd();
            return(text);
        }
        private void ReplaceOneParagraph(string text)
        {
            var rProp = GetDefaultRunProperties();

            if (IsOneParagraphText(text))
            {
                if (BkmStart.PreviousSibling <Run>() == null && BkmEnd.ElementsAfter() != null &&
                    BkmEnd.ElementsAfter().All(e => e.GetType() != typeof(Run)))
                {
                    BkmStart.Parent.RemoveAllChildren <Run>();
                }
                else
                {
                    var list  = GetBookmarkElements().ToList();
                    var trRun = list
                                .Where(rp => rp.GetType() == typeof(Run) && ((Run)rp).RunProperties != null)
                                .Select(rp => ((Run)rp).RunProperties).FirstOrDefault();
                    if (trRun != null)
                    {
                        rProp = (RunProperties)trRun.Clone();
                    }
                    for (var n = list.Count; n > 0; n--)
                    {
                        list[n - 1].Remove();
                    }
                }
                var nRun = new Run();
                if (rProp != null)
                {
                    nRun.RunProperties = (RunProperties)rProp.Clone();
                }
                var nText = new Text
                {
                    Text = text
                };
                nRun.Append(nText);
                BkmStart.InsertAfterSelf(nRun);
            }
            else
            {
                var paraProp = (ParagraphProperties)BkmStart.GetParent <Paragraph>().ParagraphProperties.Clone();
                BkmStart.Parent.Remove();
                InsertMultiLineText(text, SiblingElement, paraProp, rProp);
            }
        }
        private void ReplaceMoreParagraphs(string text)
        {
            var paraProp = (ParagraphProperties)BkmStart.GetParent <Paragraph>().ParagraphProperties.Clone();
            var list     = GetBookmarkRangeElements().ToList();

            for (var n = list.Count; n > 0; n--)
            {
                list[n - 1].Remove();
            }
            BkmStart.Parent.Remove();
            if (IsOneParagraphText(text))
            {
                BkmStart = new BookmarkStart
                {
                    Name = BkmStart.Name,
                    Id   = BkmStart.Id
                };
                var newPara = new Paragraph
                {
                    ParagraphProperties = (ParagraphProperties)paraProp.Clone()
                };
                newPara.Append(BkmStart);
                var nRun  = new Run();
                var nText = new Text
                {
                    Text = text
                };
                nRun.Append(nText);
                newPara.Append(nRun);
                BkmEnd = new BookmarkEnd
                {
                    Id = BkmStart.Id
                };
                newPara.Append(BkmEnd);
                InsertXmlELement(newPara);
            }
            else
            {
                var rProp = GetDefaultRunProperties();
                InsertMultiLineText(text, SiblingElement, paraProp, rProp);
            }
        }
        private Tuple <BookmarkStart, BookmarkEnd> GetBookmarkCopy()
        {
            if (BkmStart == null || BkmEnd == null)
            {
                return(null);
            }
            int id;

            if (!int.TryParse(BkmStart.Id, out id))
            {
                return(null);
            }
            var newBkmStart = (BookmarkStart)BkmStart.Clone();

            newBkmStart.Name = BkmStart.Name + "_copy";
            newBkmStart.Id   = (id + 1000).ToString();
            var newBkmEnd = (BookmarkEnd)BkmEnd.Clone();

            newBkmEnd.Id = newBkmStart.Id;
            return(new Tuple <BookmarkStart, BookmarkEnd>(newBkmStart, newBkmEnd));
        }