示例#1
0
        private int?TryFindNumberingIdUsingStyleId(string styleId)
        {
            while (styleId != null)
            {
                var style =
                    _styles
                    .Elements <DocumentFormat.OpenXml.Wordprocessing.Style>()
                    .Single(x => x.StyleId?.Value == styleId);

                var result =
                    style
                    .Descendants <NumberingId>()
                    .SingleOrDefault()?.Val?.Value;

                if (result.HasValue)
                {
                    return(result);
                }

                styleId =
                    style
                    .Descendants <BasedOn>()
                    .SingleOrDefault()?.Val?.Value;
            }

            return(null);
        }
示例#2
0
        // Return true if the style id is in the document, false otherwise.
        public static bool IsStyleIdInDocument(WordprocessingDocument doc,
                                               string styleid)
        {
            // Get access to the Styles element for this document.
            Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;

            // Check that there are styles and how many.
            int n = s.Elements <Style>().Count();

            if (n == 0)
            {
                return(false);
            }

            // Look for a match on styleid.
            Style style = s.Elements <Style>()
                          .Where(st => (st.StyleId == styleid) &&
                                 ((st.Type == StyleValues.Paragraph) || (st.Type == StyleValues.Table))
                                 )
                          .FirstOrDefault();

            if (style == null)
            {
                return(false);
            }

            return(true);
        }
示例#3
0
 private void InitRDefaultStyle()
 {
     _rDefaultStyle =
         _styles
         .Elements <WStyle>()
         .FirstOrDefault(x =>
                         x.Type.Value == StyleValues.Character && x.Default?.Value == true)?
         .StyleId;
     _rDefaultStyleInitialized = true;
 }
示例#4
0
        //____________________________________________________________________
        //

        #region PrepareStyles

        /// <summary>
        /// Preload the styles in the document to match localized style name.
        /// </summary>
        internal void PrepareStyles(MainDocumentPart mainPart)
        {
            knownStyles = new OpenXmlDocumentStyleCollection();
            if (mainPart.StyleDefinitionsPart == null)
            {
                return;
            }

            Styles styles = mainPart.StyleDefinitionsPart.Styles;

            foreach (var s in styles.Elements <Style>())
            {
                StyleName n = s.StyleName;
                if (n != null)
                {
                    String name = n.Val.Value;
                    if (name != s.StyleId)
                    {
                        knownStyles[name] = s;
                    }
                }

                knownStyles.Add(s.StyleId, s);
            }
        }
示例#5
0
 private int?FindAnumFromStyleId(string styleId) => _styles
 .Elements <DocumentFormat.OpenXml.Wordprocessing.Style>()
 .SingleOrDefault(x => x.StyleId == styleId)
 .StyleParagraphProperties?
 .NumberingProperties?
 .NumberingId?
 .Val?.
 Value;
示例#6
0
        /// <summary>
        /// Gets the name of the character style identifier from the character style name.
        /// Can be used to determine whether a character style with the given name is present in the document.
        /// </summary>
        /// <param name="styleName">Style name.</param>
        /// <returns>The character style identifier, or null, if no style with the given name was found.</returns>
        public string GetIdFromCharacterStyleName(string styleName)
        {
            // Get access to the Styles element for this document.
            Styles s = _wordDocument.MainDocumentPart.StyleDefinitionsPart.Styles;

            // Check that there are styles and how many.
            int n = s.Elements <Style>().Count();

            if (n == 0)
            {
                return(null);
            }

            // Look for a match on styleid.
            Style style = s.Elements <Style>()
                          .Where(st => (st.StyleName.Val == styleName) && (st.Type == StyleValues.Character))
                          .FirstOrDefault();

            return(style == null ? null : style.StyleId);
        }
示例#7
0
        private static bool IsStyleIdInDocument(WordprocessingDocument doc, string styleid)
        {
            Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;

            int n = s.Elements <Style>().Count();

            if (n == 0)
            {
                return(false);
            }

            //寻找Styleid
            Style style = s.Elements <Style>()
                          .Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph))
                          .FirstOrDefault();

            if (style == null)
            {
                return(false);
            }
            return(true);
        }
        private OpenXmlPackageVisitor Fold([NotNull] OpenXmlPackageVisitor subject)
        {
            Document document = Document.Concat(subject.Document);

            Footnotes footnotes = Footnotes.Concat(subject.Footnotes);

            XElement styles =
                new XElement(
                    Styles.Name,
                    Styles.Attributes(),
                    Styles.Elements()
                    .Union(
                        subject.Styles
                        .Elements()
                        .Where(x => x.Name != W + "docDefaults")
                        .Where(x => (string)x.Attribute(W + "styleId") != "Normal"),
                        XNode.EqualityComparer));

            XElement numbering =
                new XElement(
                    Numbering.Name,
                    Numbering.Attributes(),
                    Numbering.Elements()
                    .Union(
                        subject.Numbering.Elements(),
                        XNode.EqualityComparer));

            // TODO: write a ThemeVisit
//            XElement theme =
//                new XElement(
//                    Theme.TargetUri,
//                    Theme.Attributes(),
//                    Theme.Elements()
//                          .Union(
//                              subject.Theme.Elements(),
//                              XNode.EqualityComparer));

            return(With(document, footnotes, styles, numbering, subject.Theme1));
        }
示例#9
0
 private int GetNumberingFromStyleId(string linkedStyleId) =>
 _styles.Elements <DocumentFormat.OpenXml.Wordprocessing.Style>()
 .Single(x => x.StyleId.Value == linkedStyleId)
 .Descendants <NumberingId>()
 .Single()
 .Val.Value;
 public static Style FindById(this Styles styles, string id)
 {
     return(styles.Elements <Style>()
            .FirstOrDefault(s => s.StyleId.HasValue && s.StyleId.Value == id));
 }