Exemplo n.º 1
0
        public static OnOffValue GetOnOffValue <T>(this ParagraphProperties pPr, WordprocessingDocument document)
            where T : OnOffType
        {
            var element = pPr.Descendants <T>().FirstOrDefault();

            if (element != null)
            {
                return(element.Val.HasValue ? element.Val : new OnOffValue(true));
            }

            var style = pPr.GetParagraphStyle(document);

            return(style != null?style.GetOnOffValue <T>() : null);
        }
Exemplo n.º 2
0
        public static T GetLeafElement <T>(this ParagraphProperties pPr, WordprocessingDocument document)
            where T : OpenXmlLeafElement
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            var leaf = pPr.Descendants <T>().FirstOrDefault();

            if (leaf != null)
            {
                return(leaf);
            }

            var style = pPr.GetParagraphStyle(document);

            return(style != null?style.GetLeafElement <T>() : null);
        }
Exemplo n.º 3
0
        private int?FindNumInstanceId(ParagraphProperties pProps)
        {
            var numberingId =
                pProps
                .Descendants <NumberingId>()
                .SingleOrDefault()?.Val?.Value;

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

            var paragraphStyleId =
                pProps
                .Elements <ParagraphStyleId>()
                .SingleOrDefault()?
                .Val?.Value;

            return(TryFindNumberingIdUsingStyleId(paragraphStyleId));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Add format paragraph
        /// </summary>
        /// <param name="paragraphProperties"></param>
        /// <returns></returns>
        private string AddFormatParagraph(ParagraphProperties paragraphProperties)
        {
            List <string> cssStyleElements = new List <string>();

            string paragraphStyle = "";

            if (paragraphProperties != null)
            {
                ParagraphStyleId paragraphStyleId = paragraphProperties.Descendants <ParagraphStyleId>().FirstOrDefault();

                if (paragraphStyleId != null)
                {
                    paragraphStyle = paragraphStyleId.Val;
                }
            }

            paragraphStyle = "\"" + paragraphStyle + "\"";


            Indentation indentation = paragraphProperties.Indentation;

            /*
             * if (indentation != null)
             * {
             *  if (indentation.Left != null)
             *      cssStyleElements.Add(string.Format("margin-left:{0}pt", ConvertDxaToPoint(indentation.Left.Value)));
             *  else if (indentation.FirstLine != null)
             *      //cssStyleElements.Add(string.Format("text-indent:{0}pt", ConvertDxaToPoint(indentation.FirstLine.Value)));
             *      cssStyleElements.Add(string.Format("margin-left:{0}pt", ConvertDxaToPoint(indentation.FirstLine.Value)));
             * }*/

            NumberingProperties numberingProperties = paragraphProperties.NumberingProperties;

            if (numberingProperties != null && numberingProperties.NumberingLevelReference != null && numberingProperties.NumberingId != null)
            {
                int level = numberingProperties.NumberingLevelReference.Val.Value;
                int numId = numberingProperties.NumberingId.Val.Value;

                int abstractNumId = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants <NumberingInstance>().Where(x => x.NumberID == numId).FirstOrDefault().AbstractNumId.Val.Value;
                indentation = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants <AbstractNum>().Where(x => x.AbstractNumberId == abstractNumId).FirstOrDefault().Descendants <Level>().Where(x => x.LevelIndex.Value == level).FirstOrDefault().Descendants <Indentation>().FirstOrDefault();

                Level numberingLevel = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants <AbstractNum>().Where(x => x.AbstractNumberId == abstractNumId).FirstOrDefault().Descendants <Level>().Where(x => x.LevelIndex.Value == level).FirstOrDefault();

                if (numberingLevel.LevelText.Val.Value.Contains("%"))
                {
                    cssStyleElements.Add(string.Format("mso-list:l{0} level{1} ol", numId, level + 1));
                }
                else
                {
                    cssStyleElements.Add(string.Format("mso-list:l{0} level{1} ul", numId, level + 1));
                }
            }

            string result = "";

            foreach (string element in cssStyleElements)
            {
                result += element + ";";
            }

            if (result != "")
            {
                result = paragraphStyle + string.Format(" style='{0}'", result);
            }
            else
            {
                result = paragraphStyle;
            }

            return(result.Trim());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add format paragraph
        /// </summary>
        /// <param name="paragraphProperties"></param>
        /// <returns></returns>
        private string AddFormatParagraph(ParagraphProperties paragraphProperties)
        {
            List<string> cssStyleElements = new List<string>();

            string paragraphStyle = "";
         
            if (paragraphProperties != null)
            {
                ParagraphStyleId paragraphStyleId = paragraphProperties.Descendants<ParagraphStyleId>().FirstOrDefault();
                
                if (paragraphStyleId != null)
                    paragraphStyle = paragraphStyleId.Val;
            }

            paragraphStyle = "\"" + paragraphStyle + "\"";

           
            Indentation indentation = paragraphProperties.Indentation;

            /*
            if (indentation != null)
            {
                if (indentation.Left != null)
                    cssStyleElements.Add(string.Format("margin-left:{0}pt", ConvertDxaToPoint(indentation.Left.Value)));
                else if (indentation.FirstLine != null)
                    //cssStyleElements.Add(string.Format("text-indent:{0}pt", ConvertDxaToPoint(indentation.FirstLine.Value)));
                    cssStyleElements.Add(string.Format("margin-left:{0}pt", ConvertDxaToPoint(indentation.FirstLine.Value)));
            }*/

            NumberingProperties numberingProperties = paragraphProperties.NumberingProperties;

            if (numberingProperties != null && numberingProperties.NumberingLevelReference != null && numberingProperties.NumberingId != null)
            {
                int level = numberingProperties.NumberingLevelReference.Val.Value;
                int numId = numberingProperties.NumberingId.Val.Value;

                int abstractNumId = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants<NumberingInstance>().Where(x => x.NumberID == numId).FirstOrDefault().AbstractNumId.Val.Value;
                indentation = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants<AbstractNum>().Where(x => x.AbstractNumberId == abstractNumId).FirstOrDefault().Descendants<Level>().Where(x => x.LevelIndex.Value == level).FirstOrDefault().Descendants<Indentation>().FirstOrDefault();
               
                Level numberingLevel = document.MainDocumentPart.NumberingDefinitionsPart.Numbering.Descendants<AbstractNum>().Where(x => x.AbstractNumberId == abstractNumId).FirstOrDefault().Descendants<Level>().Where(x => x.LevelIndex.Value == level).FirstOrDefault();
               
                if (numberingLevel.LevelText.Val.Value.Contains("%"))
                    cssStyleElements.Add(string.Format("mso-list:l{0} level{1} ol", numId, level + 1));
                else
                    cssStyleElements.Add(string.Format("mso-list:l{0} level{1} ul", numId, level + 1));
            }

            string result = "";

            foreach (string element in cssStyleElements)
                result += element + ";";

            if (result != "")
                result = paragraphStyle + string.Format(" style='{0}'", result);
            else
                result = paragraphStyle;

            return result.Trim();
        }