Пример #1
0
        public DqStyleTable(IReadOnlyCollection <DqStyle> styles, DqStyleBasis defaultStyle)
        {
            var styleByID      = styles.ToDictionary(s => s.ID);
            var dqDefaultStyle = new DqStyle {
                Current = defaultStyle, Type = DqStyleType.Paragraph
            };

            foreach (var dqStyle in styleByID.Values)
            {
                dqStyle.BaseStyle = dqStyle.BaseStyleID != null
                    ? styleByID[dqStyle.BaseStyleID]
                    : dqDefaultStyle;
            }

            Paragraph = new DqStyleList(styleByID.Values.Where(s => s.Type == DqStyleType.Paragraph));
        }
Пример #2
0
        public DqStyle GetParagraphStyle(Paragraph paragraph, DqStyleTable styleTable, DqFontScheme fontScheme, DqNumberingTable dqNumberingTable)
        {
            var value      = paragraph.ParagraphProperties?.ParagraphStyleId?.Val?.Value;
            var basicStyle = styleTable.Paragraph[value] ?? styleTable.Paragraph.Default;

            var pPr = paragraph.ParagraphProperties;

            if (pPr == null)
            {
                return(basicStyle);
            }

            var localStyle = new DqStyle {
                BaseStyle = basicStyle
            };

            if (pPr.SpacingBetweenLines != null)
            {
                localStyle.Current.SpacingBetweenLines = GetSpacingBetweenLines(pPr.SpacingBetweenLines.Line);
            }

            if (pPr.Indentation?.FirstLine != null)
            {
                localStyle.Current.Indent = Convertion.TwipToCm(int.Parse(pPr.Indentation.FirstLine.Value));
            }

            if (pPr.Justification != null)
            {
                localStyle.Current.Aligment = Convert(pPr.Justification.Val);
            }

            if (pPr.GetFirstChild <NumberingProperties>() != null)
            {
                var id        = pPr.GetFirstChild <NumberingProperties>().NumberingId;
                var numbering = dqNumberingTable[id.Val];
                localStyle.Current.Numbering = numbering;

                var level = pPr.GetFirstChild <NumberingProperties>().NumberingLevelReference;
                if (level != null && level.Val < numbering.Levels.Count)
                {
                    localStyle.Current.Indent = numbering.Levels[level.Val].Indent ?? localStyle.Indent;
                }
            }

            var rPr = (OpenXmlElement)pPr.ParagraphMarkRunProperties;

            if (rPr != null)
            {
                if (rPr.GetFirstChild <RunFonts>() != null)
                {
                    localStyle.Current.FontName = GetFontName(rPr.GetFirstChild <RunFonts>(), fontScheme);
                }

                if (rPr.GetFirstChild <FontSize>() != null)
                {
                    localStyle.Current.FontSize = ConvertFontSize(rPr.GetFirstChild <FontSize>());
                }

                if (rPr.GetFirstChild <Bold>() != null)
                {
                    localStyle.Current.IsBold = ConvertBold(rPr.GetFirstChild <Bold>());
                }
            }

            var runs = paragraph.Descendants <Run>().ToList();

            if (runs.Count == 1)
            {
                rPr = runs.Single().RunProperties;
                if (rPr != null)
                {
                    if (rPr.GetFirstChild <RunFonts>() != null)
                    {
                        localStyle.Current.FontName = GetFontName(rPr.GetFirstChild <RunFonts>(), fontScheme);
                    }

                    if (rPr.GetFirstChild <FontSize>() != null)
                    {
                        localStyle.Current.FontSize = ConvertFontSize(rPr.GetFirstChild <FontSize>());
                    }

                    if (rPr.GetFirstChild <Bold>() != null)
                    {
                        localStyle.Current.IsBold = ConvertBold(rPr.GetFirstChild <Bold>());
                    }
                }
            }

            return(localStyle);
        }