Exemple #1
0
    public static string?GetAlias(this SdtElement run)
    {
        var properties = run.GetFirstChild <SdtProperties>() !;
        var aliace     = properties.GetFirstChild <SdtAlias>() !.Val;

        return(aliace);
    }
Exemple #2
0
    public static string?GetTag(this SdtElement run)
    {
        var properties = run.GetFirstChild <SdtProperties>()
                         ?? throw new InvalidOperationException("Не найден узел с параметрами");

        var tag = properties.Elements <Tag>().FirstOrDefault();

        return(tag?.Val);
    }
Exemple #3
0
        // protected override string OldProcessControl(string variableIdentifier, IVariableSource variableSource,
        //     ContentControl original, List<string> otherParameters)
        // {
        //     try
        //     {
        //         GetParameters(otherParameters, out var inline, out var separator, out var lastSeparator);
        //
        //         //This is the original content. We will add all new children in it
        //         var originalSdtContent = GetSdtContent(original.SdtElement);
        //
        //         //This we will use to make copies:
        //         var originalSdtContentCopy = originalSdtContent.CloneNode(true);
        //
        //         //Remove all children from the original, so that it is free for the new children,
        //         //we have the original ones in the copy.
        //         originalSdtContent.RemoveAllChildren();
        //
        //         //Get the data and the control type
        //         var dataItems = variableSource.GetVariable<IList>(variableIdentifier);
        //         var originalContentControlType = original.Type;
        //
        //
        //         //If the element is inline we want to create a single paragraph that will hold everything
        //         OpenXmlElement masterElement;
        //         if (originalSdtContent.GetType() == typeof(SdtContentBlock))
        //             masterElement = new Paragraph();
        //         else masterElement = new Run();
        //
        //         if (inline)
        //             originalSdtContent.AppendChild(masterElement);
        //
        //         //Keep track of the last separator, so that we remove it after adding everything
        //         Text lastSeparatorText = null;
        //
        //         //Copy the original content as many times as we have items and substitute all the variables in it
        //         var itemsCount = dataItems.Count;
        //         for (var i = 0; i < itemsCount; i++)
        //         {
        //             var dataItem = dataItems[i];
        //
        //             //Make a copy of the original content. Then modify it according to the data
        //             var sdtContentCopy = originalSdtContentCopy.CloneNode(true);
        //
        //             var separatorToUse = i == itemsCount - 2 ? lastSeparator : separator;
        //
        //             var separatorText = new Text(separatorToUse)
        //                 {Space = SpaceProcessingModeValues.Preserve};
        //
        //             lastSeparatorText = separatorText;
        //
        //
        //             //If the data is of a primitive type, just insert it as text
        //             if (dataItem is string || dataItem is int || dataItem is float || dataItem is double ||
        //                 dataItem is decimal)
        //             {
        //                 var newRun = sdtContentCopy.Descendants<Run>()?.FirstOrDefault()?.CloneNode(true) ??
        //                              new Run();
        //
        //                 sdtContentCopy.RemoveAllChildren();
        //                 SetTextAndRemovePlaceholderFormat(newRun, dataItem.ToString());
        //                 newRun.AppendChild(separatorText);
        //
        //
        //                 if (originalSdtContent.GetType() == typeof(SdtContentBlock))
        //                 {
        //                     if (inline)
        //                         masterElement.AppendChild(newRun);
        //                     else
        //                         originalSdtContent.AppendChild(new Paragraph(newRun));
        //                 }
        //                 else
        //                 {
        //                     if (!inline)
        //                         originalSdtContent.AppendChild(new Run(new Break()));
        //                     originalSdtContent.AppendChild(newRun);
        //                 }
        //             }
        //             else if (originalContentControlType == OpenXmlExtensions.ContentControlType.RichText &&
        //                      dataItem is Dictionary<string, object> dictItem)
        //             {
        //                 //If the data is a complex and the content control is a rich text type, account for nested controls
        //
        //                 if (dictItem.ContainsKey("index") == false)
        //                     dictItem.Add("index", i + 1);
        //
        //                 var innerVariableSource = new VariableSource(dictItem);
        //                 //Leave those for legacy support of repeatingitem and repeatingconditional tags
        //                 var innerVReplacer = new InnerRepeatingVariableControlReplacer();
        //                 var innerCReplacer = new InnerRepeatingConditionalRemoveControlReplacer();
        //                 innerVReplacer.ReplaceAll(sdtContentCopy, innerVariableSource);
        //                 innerCReplacer.ReplaceAll(sdtContentCopy, innerVariableSource);
        //
        //
        //                 var lastRun = sdtContentCopy.Descendants<Run>().LastOrDefault();
        //                 lastRun?.AppendChild(separatorText);
        //
        //                 var children = sdtContentCopy.ChildElements.ToList();
        //                 var newContentControls = new List<SdtElement>();
        //                 foreach (var newChild in children)
        //                 {
        //                     newChild.Remove();
        //                     originalSdtContent.AppendChild(newChild);
        //                     newContentControls.AddRange(newChild.ContentControls());
        //                 }
        //
        //                 Enqueue(new ControlReplacementExecutionData
        //                     {Controls = newContentControls, VariableSource = innerVariableSource});
        //             }
        //         }
        //
        //         try
        //         {
        //             lastSeparatorText?.Remove();
        //         }
        //         catch
        //         {
        //             // ignored
        //         }
        //
        //         return null;
        //     }
        //     catch (VariableNotFoundException)
        //     {
        //         return null;
        //     }
        // }

        private static OpenXmlElement GetSdtContent(SdtElement original)
        {
            return(original.GetFirstChild <SdtContentRun>() ??
                   original.GetFirstChild <SdtContentBlock>() ??
                   (OpenXmlElement)original.GetFirstChild <SdtContentRow>());
        }