public void ClearHighlightColors(Document document, HighlightScope highlightScope)
        {
            var segments = highlightScope == HighlightScope.Filtered
                                ? document?.FilteredSegmentPairs?.ToList()
                                : new List <ISegmentPair> {
                document.GetActiveSegmentPair()
            };

            if (segments == null)
            {
                return;
            }

            foreach (var segmentPair in segments)
            {
                var item = segmentPair.Target.AllSubItems.FirstOrDefault();
                if (item is ITagPair tagPair)
                {
                    if (tagPair.StartTagProperties?.Formatting == null)
                    {
                        continue;
                    }

                    if (!tagPair.StartTagProperties.MetaDataContainsKey(CadfBackgroundColorKey))
                    {
                        continue;
                    }

                    segmentPair.Target.Clear();
                    tagPair.MoveAllItemsTo(segmentPair.Target);

                    document.UpdateSegmentPair(segmentPair);
                }
            }
        }
        public void ApplyHighlightColor(Document document, HighlightScope highlightScope, HighlightColor highlightColor)
        {
            var segments = highlightScope == HighlightScope.Filtered
                                ? document?.FilteredSegmentPairs?.ToList()
                                : new List <ISegmentPair> {
                document.GetActiveSegmentPair()
            };

            if (segments == null)
            {
                return;
            }

            var allTagIds = GetAllTagIds(document);
            var seed      = GetLargestSeedValue(allTagIds);

            var itemFactory       = document.ItemFactory;
            var propertyFactory   = itemFactory.PropertiesFactory;
            var formattingFactory = propertyFactory.FormattingItemFactory;
            var formattingItem    = formattingFactory.CreateFormattingItem("BackgroundColor", highlightColor.GetArgb());

            foreach (var segmentPair in segments)
            {
                var hashighlightColor = HasHighlightColor(segmentPair, out var existingHighlightColor);
                if (hashighlightColor)
                {
                    if (existingHighlightColor != null &&
                        string.Equals(existingHighlightColor.Name, highlightColor.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    RemoveHighlightColor(segmentPair);
                }

                var tagId = GetNextTagId(allTagIds, seed);
                seed = tagId + 1;

                var startTagProperties = CreateStartTagProperties(propertyFactory, formattingFactory, formattingItem, tagId, highlightColor.Name);
                var endTagProperties   = CreateEndTagProperties(propertyFactory);

                var tagPairNew = document.ItemFactory.CreateTagPair(startTagProperties, endTagProperties);
                segmentPair.Target.MoveAllItemsTo(tagPairNew);
                segmentPair.Target.Add(tagPairNew);

                document.UpdateSegmentPair(segmentPair);
            }
        }