Пример #1
0
        public void DoCheck(WordprocessingDocument document)
        {
            //Получить текущие стили
            var defaultStyles = document.GetDefaultStyles();
            var allStyles     = document.GetStyles();

            //Собрать все параграфы
            var paragraphs = document.MainDocumentPart.Document.Body.Descendants <Paragraph>();

            foreach (var paragraph in paragraphs)
            {
                foreach (var run in paragraph.Descendants <Run>())
                {
                    var comment       = new StringBuilder();
                    var runProperties = run.Descendants <RunProperties>();

                    bool fontSizeErr    = !(FontSizes.Length > 0);
                    bool runFontsErr    = !(RunFontsNames.Length > 0);
                    bool lineSpacingErr = !(LineSpacing.Length > 0);

                    foreach (var rp in runProperties)
                    {
                        if (!fontSizeErr && rp.FontSize != null)
                        {
                            if (!FontSizes.Contains(rp.FontSize.Val.ToString()))
                            {
                                comment.AppendLine(Resources.CheckFontSize);
                                fontSizeErr = true;
                            }
                        }
                        else if (!fontSizeErr && rp.RunStyle != null)
                        {
                            var currentStyle = allStyles.FindById(rp.RunStyle.Val);
                            if (currentStyle == null)
                            {
                                continue;
                            }

                            var fontSize = FindFirstFontSize(currentStyle, allStyles);
                            if (fontSize != null && !FontSizes.Contains(fontSize))
                            {
                                comment.AppendLine(Resources.CheckFontSize);
                                fontSizeErr = true;
                            }
                            else
                            {
                                fontSizeErr = CheckDefaultFontSize(defaultStyles, comment);
                            }
                        }
                        else if (!fontSizeErr)
                        {
                            fontSizeErr = CheckDefaultFontSize(defaultStyles, comment);
                        }



                        if (!runFontsErr && rp.RunFonts != null)
                        {
                            if ((rp.RunFonts.Ascii != null && rp.RunFonts.Ascii.HasValue && !RunFontsNames.Contains(rp.RunFonts.Ascii.Value)) ||
                                (rp.RunFonts.ComplexScript != null && rp.RunFonts.ComplexScript.HasValue &&
                                 !RunFontsNames.Contains(rp.RunFonts.ComplexScript.Value)) ||
                                (rp.RunFonts.HighAnsi != null && rp.RunFonts.HighAnsi.HasValue && !RunFontsNames.Contains(rp.RunFonts.HighAnsi.Value)))
                            {
                                comment.AppendLine(Resources.CheckFont);
                                fontSizeErr = true;
                            }
                        }
                        else if (!runFontsErr && rp.RunStyle != null)
                        {
                            var currentStyle = allStyles.FindById(rp.RunStyle.Val);
                            if (currentStyle == null)
                            {
                                continue;
                            }

                            var runFonts = FindFonts(currentStyle, allStyles);
                            if (runFonts != null &&
                                (
                                    (runFonts.Ascii != null && runFonts.Ascii.HasValue && !RunFontsNames.Contains(runFonts.Ascii.Value)) ||
                                    (runFonts.ComplexScript != null && runFonts.ComplexScript.HasValue &&
                                     !RunFontsNames.Contains(runFonts.ComplexScript.Value))
                                ))
                            {
                                comment.AppendLine(Resources.CheckFont);
                                runFontsErr = true;
                            }
                            else
                            {
                                runFontsErr = CheckDefaultRunFonts(defaultStyles, comment);
                            }
                        }
                        else if (!runFontsErr)
                        {
                            runFontsErr = CheckDefaultRunFonts(defaultStyles, comment);
                        }
                        //Otstupi

                        if (!lineSpacingErr && rp.Spacing != null)
                        {
                            if (!LineSpacing.Contains(rp.Spacing.Val.ToString()))
                            {
                                comment.AppendLine(Resources.CheckLineSpacing);
                                fontSizeErr = true;
                            }
                        }
                        else if (!fontSizeErr && rp.RunStyle != null)
                        {
                            var currentStyle = allStyles.FindById(rp.RunStyle.Val);
                            if (currentStyle == null)
                            {
                                continue;
                            }


                            //Тут остановился. Ищем межстрочний интервал
                            var fontSize = FindFirstFontSize(currentStyle, allStyles);
                            if (fontSize != null && !FontSizes.Contains(fontSize))
                            {
                                comment.AppendLine(Resources.CheckFontSize);
                                fontSizeErr = true;
                            }
                            else
                            {
                                fontSizeErr = CheckDefaultFontSize(defaultStyles, comment);
                            }
                        }
                        else if (!fontSizeErr)
                        {
                            fontSizeErr = CheckDefaultFontSize(defaultStyles, comment);
                        }
                    }

                    //Добавим коммент
                    if (comment.Length > 0)
                    {
                        document.AddCommentNearElement(run, _author, comment.ToString());
                    }
                }
            }


            //Проверить интервалы
        }