Esempio n. 1
0
        private static void InitializeListItemRetrieverForPart(WordprocessingDocument wordDoc, OpenXmlPart part, ListItemRetrieverSettings settings)
        {
            var mainXDoc = part.GetXDocument();

            var numPart = wordDoc.MainDocumentPart.NumberingDefinitionsPart;

            if (numPart == null)
            {
                return;
            }
            var numXDoc = numPart.GetXDocument();

            var stylesPart = wordDoc.MainDocumentPart.StyleDefinitionsPart;

            if (stylesPart == null)
            {
                return;
            }
            var stylesXDoc = stylesPart.GetXDocument();

            var rootNode = mainXDoc.Root;

            InitializeListItemRetrieverForStory(numXDoc, stylesXDoc, rootNode);

            var textBoxes = mainXDoc
                            .Root
                            .Descendants(W.txbxContent);

            foreach (var textBox in textBoxes)
            {
                InitializeListItemRetrieverForStory(numXDoc, stylesXDoc, textBox);
            }
        }
Esempio n. 2
0
        public static string RetrieveListItem(WordprocessingDocument wordDoc, XElement paragraph, ListItemRetrieverSettings settings)
        {
            if (wordDoc.MainDocumentPart.NumberingDefinitionsPart == null)
            {
                return(null);
            }

            var listItemInfo = paragraph.Annotation <ListItemInfo>();

            if (listItemInfo == null)
            {
                InitializeListItemRetriever(wordDoc, settings);
            }

            listItemInfo = paragraph.Annotation <ListItemInfo>();
            if (!listItemInfo.IsListItem)
            {
                return(null);
            }

            var numberingDefinitionsPart = wordDoc
                                           .MainDocumentPart
                                           .NumberingDefinitionsPart;

            if (numberingDefinitionsPart == null)
            {
                return(null);
            }

            StyleDefinitionsPart styleDefinitionsPart = wordDoc
                                                        .MainDocumentPart
                                                        .StyleDefinitionsPart;

            if (styleDefinitionsPart == null)
            {
                return(null);
            }

            var numXDoc    = numberingDefinitionsPart.GetXDocument();
            var stylesXDoc = styleDefinitionsPart.GetXDocument();

            var lvl = listItemInfo.Lvl(GetParagraphLevel(paragraph));

            string lvlText = (string)lvl.Elements(W.lvlText).Attributes(W.val).FirstOrDefault();

            if (lvlText == null)
            {
                return(null);
            }

            var levelNumbersAnnotation = paragraph.Annotation <LevelNumbers>();

            if (levelNumbersAnnotation == null)
            {
                throw new OpenXmlPowerToolsException("Internal error");
            }

            int[]  levelNumbers       = levelNumbersAnnotation.LevelNumbersArray;
            string languageIdentifier = GetLanguageIdentifier(paragraph, stylesXDoc);
            string listItem           = FormatListItem(listItemInfo, levelNumbers, GetParagraphLevel(paragraph),
                                                       lvlText, stylesXDoc, languageIdentifier, settings);

            return(listItem);
        }
Esempio n. 3
0
        private static void InitializeListItemRetriever(WordprocessingDocument wordDoc, ListItemRetrieverSettings settings)
        {
            foreach (var part in wordDoc.ContentParts())
            {
                InitializeListItemRetrieverForPart(wordDoc, part, settings);
            }

#if false
            foreach (var part in wordDoc.ContentParts())
            {
                var xDoc  = part.GetXDocument();
                var paras = xDoc
                            .Descendants(W.p)
                            .Where(p =>
                                   p.Annotation <ListItemInfo>() == null);
                if (paras.Any())
                {
                    Console.WriteLine("Error");
                }
            }
#endif
        }
Esempio n. 4
0
        private static string FormatListItem(ListItemInfo lii, int[] levelNumbers, int ilvl,
                                             string lvlText, XDocument styles, string languageCultureName, ListItemRetrieverSettings settings)
        {
            string[] formatTokens = GetFormatTokens(lvlText).ToArray();
            XElement lvl          = lii.Lvl(ilvl);
            bool     isLgl        = lvl.Elements(W.isLgl).Any();
            string   listItem     = formatTokens.Select((t, l) =>
            {
                if (t.Substring(0, 1) != "%")
                {
                    return(t);
                }
                int indentationLevel;
                if (!Int32.TryParse(t.Substring(1), out indentationLevel))
                {
                    return(t);
                }
                indentationLevel -= 1;
                if (indentationLevel >= levelNumbers.Length)
                {
                    indentationLevel = levelNumbers.Length - 1;
                }
                int levelNumber       = levelNumbers[indentationLevel];
                string levelText      = null;
                XElement rlvl         = lii.Lvl(indentationLevel);
                string numFmtForLevel = (string)rlvl.Elements(W.numFmt).Attributes(W.val).FirstOrDefault();
                if (numFmtForLevel == null)
                {
                    var numFmtElement = rlvl.Elements(MC.AlternateContent).Elements(MC.Choice).Elements(W.numFmt).FirstOrDefault();
                    if (numFmtElement != null && (string)numFmtElement.Attribute(W.val) == "custom")
                    {
                        numFmtForLevel = (string)numFmtElement.Attribute(W.format);
                    }
                }
                if (numFmtForLevel != "none")
                {
                    if (isLgl && numFmtForLevel != "decimalZero")
                    {
                        numFmtForLevel = "decimal";
                    }
                }
                if (languageCultureName != null && settings != null)
                {
                    if (settings.ListItemTextImplementations.ContainsKey(languageCultureName))
                    {
                        var impl  = settings.ListItemTextImplementations[languageCultureName];
                        levelText = impl(languageCultureName, levelNumber, numFmtForLevel);
                    }
                }
                if (levelText == null)
                {
                    levelText = ListItemTextGetter_Default.GetListItemText(languageCultureName, levelNumber, numFmtForLevel);
                }
                return(levelText);
            }).StringConcatenate();

            return(listItem);
        }