Beispiel #1
0
        private static AbstractNum GetAbstractNumElement(Numbering numbering, int abstractNumId)
        {
            var oxmlAbstractNum = numbering.Elements <AbstractNum>().FirstOrDefault(an =>
            {
                return(an.AbstractNumberId.Value == abstractNumId);
            });

            if (oxmlAbstractNum == null)
            {
                throw new OpenXmlPackageException(string.Format("Couldn't find the AbstractNum element by the AbstractNum ID (ID:{0})", abstractNumId));
            }

            return(oxmlAbstractNum);
        }
Beispiel #2
0
        private static NumberingInstance GetNumberingInstanceElement(Numbering numbering, int numberingId)
        {
            var oxmlNumberingInstance = numbering.Elements <NumberingInstance>().FirstOrDefault(ni =>
            {
                return(ni.NumberID.Value == numberingId);
            });

            if (oxmlNumberingInstance == null)
            {
                throw new OpenXmlPackageException(string.Format("Couldn't find the NumberingInstance element by the Number ID (ID:{0})", numberingId));
            }

            return(oxmlNumberingInstance);
        }
        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));
        }
Beispiel #4
0
        public int ProcessItem(HtmlEnumerator en)
        {
            if (!firstItem)
            {
                return(this.InstanceID);
            }

            firstItem = false;

            // in case a margin has been specifically specified, we need to create a new list template
            // on the fly with a different AbsNumId, in order to let Word doesn't merge the style with its predecessor.
            Margin margin = en.StyleAttributes.GetAsMargin("margin");

            if (margin.Left.Value > 0 && margin.Left.Type == UnitMetric.Pixel)
            {
                Numbering numbering = mainPart.NumberingDefinitionsPart.Numbering;
                foreach (AbstractNum absNum in numbering.Elements <AbstractNum>())
                {
                    if (absNum.AbstractNumberId == absNumId)
                    {
                        Level lvl          = absNum.GetFirstChild <Level>();
                        Int32 currentNumId = ++nextInstanceID;

                        numbering.Append(
                            new AbstractNum(
                                new MultiLevelType()
                        {
                            Val = MultiLevelValues.SingleLevel
                        },
                                new Level {
                            StartNumberingValue = new StartNumberingValue()
                            {
                                Val = 1
                            },
                            NumberingFormat = new NumberingFormat()
                            {
                                Val = lvl.NumberingFormat.Val
                            },
                            LevelIndex = 0,
                            LevelText  = new LevelText()
                            {
                                Val = lvl.LevelText.Val
                            }
                        }
                                )
                        {
                            AbstractNumberId = currentNumId
                        });
                        numbering.Save(mainPart.NumberingDefinitionsPart);
                        numbering.Append(
                            new NumberingInstance(
                                new AbstractNumId()
                        {
                            Val = currentNumId
                        }
                                )
                        {
                            NumberID = currentNumId
                        });
                        numbering.Save(mainPart.NumberingDefinitionsPart);
                        mainPart.NumberingDefinitionsPart.Numbering.Reload();
                        break;
                    }
                }
            }

            return(this.InstanceID);
        }
Beispiel #5
0
        public void detectTitle(Paragraph p, WordprocessingDocument doc)
        {
            //匹配标题的正则数组
            Regex[] reg = new Regex[3];
            //一级标题
            reg[0] = new Regex(@"[1-9][0-9]*");
            //二级标题
            reg[1] = new Regex(@"[1-9][0-9]*\.[1-9][0-9]*");
            //三级标题
            reg[2] = new Regex(@"[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*");

            string            title = Tool.getFullText(p).Trim();
            string            order = "";
            IEnumerable <Run> run   = p.Elements <Run>();
            int   index;
            Match m = reg[2].Match(title);

            for (index = 2; index > -1; index--)
            {
                m = reg[index].Match(title);
                if (m.Success == true)
                {
                    break;
                }
            }
            if (index > -1)
            {
                if (m.Index == 0)
                {
                    if (title.IndexOf("  ") < 0)
                    {
                        Util.printError("该标题序号后缺少两个空格----" + title);
                    }
                    else if (Util.getSubstrCount(title.Substring(0, m.Value.Length + 3), " ") > 2)
                    {
                        Util.printError("该标题序号后多于两个空格----" + title);
                    }

                    order = m.Value;
                    if (Util.getSubstrCount(order, ".") == 0)
                    {
                        if (order != one_level)
                        {
                            Util.printError("该标题序号错误,序号编号应为" + one_level + "----" + title);
                        }
                        two_level = one_level + ".1";
                        one_level = (Convert.ToInt32(one_level) + 1).ToString();
                    }
                    else if (Util.getSubstrCount(order, ".") == 1)
                    {
                        if (order != two_level)
                        {
                            Util.printError("该标题序号错误,序号编号应为" + two_level + "----" + title);
                        }
                        three_level = two_level + ".1";
                        int i = two_level.IndexOf(".");
                        //二级标题加一
                        two_level = two_level.Substring(0, i + 1) + (Convert.ToInt32(two_level.Substring(i + 1, two_level.Length - i - 1)) + 1).ToString();
                    }
                    else if (Util.getSubstrCount(order, ".") == 2)
                    {
                        if (order != three_level)
                        {
                            Util.printError("该标题序号错误,序号编号应为" + three_level + "----" + title);
                        }
                        int i = three_level.LastIndexOf(".");
                        //三级标题加一
                        three_level = three_level.Substring(0, i + 1) + (Convert.ToInt32(three_level.Substring(i + 1, three_level.Length - i - 1)) + 1).ToString();
                    }
                    else
                    {
                        Util.printError("该标题序号错误,不能超过三级标题" + "----" + title);
                    }
                }
                else
                {
                    return;
                }

                /* index = 0 对应一级标题
                *   index = 1 对应二级标题
                *   index = 2 对应三级标题 */
                if (!Util.correctfonts(p, doc, tTitle[index, 0], "Cambria"))
                {
                    Util.printError("该标题字体错误,应为" + show[index, 0] + "----" + title);
                }
                if (!Util.correctsize(p, doc, tTitle[index, 1]))
                {
                    Util.printError("该标题字号错误,应为" + show[index, 1] + "----" + title);
                }
                if (!Util.correctJustification(p, doc, tTitle[index, 2]) && !Util.correctJustification(p, doc, "两端对齐"))
                {
                    Util.printError("该标题位置错误,应为" + show[index, 2] + "----" + title);
                }
                if (!Util.correctSpacingBetweenLines_Be(p, doc, tTitle[index, 3]))
                {
                    Util.printError("该标题段前间距错误,应为" + show[index, 3] + "----" + title);
                }
                if (!Util.correctSpacingBetweenLines_Af(p, doc, tTitle[index, 4]))
                {
                    Util.printError("该标题段后间距错误,应为" + show[index, 4] + "----" + title);
                }
                if (!Util.correctSpacingBetweenLines_line(p, doc, tTitle[index, 5]))
                {
                    Util.printError("该标题行距错误,应为" + show[index, 5] + "----" + title);
                }
            }
            else
            {
                if (p.ParagraphProperties != null)
                {
                    if (p.ParagraphProperties.NumberingProperties == null)
                    {
                        if (title.Length != 0)
                        {
                            Util.printError("标题序号应为阿拉伯数字----" + title);
                        }
                    }
                    //检测是否自动编号
                    else
                    {
                        string    numberingId = p.ParagraphProperties.NumberingProperties.NumberingId.Val;
                        string    ilvl        = p.ParagraphProperties.NumberingProperties.NumberingLevelReference.Val;
                        Numbering numbering1  = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;
                        IEnumerable <NumberingInstance> nums         = numbering1.Elements <NumberingInstance>();
                        IEnumerable <AbstractNum>       abstractNums = numbering1.Elements <AbstractNum>();
                        foreach (NumberingInstance num in nums)
                        {
                            if (num.NumberID == numberingId)
                            {
                                Int32 abstractNumId1 = num.AbstractNumId.Val;
                                foreach (AbstractNum abstractNum in abstractNums)
                                {
                                    if (abstractNum.AbstractNumberId == abstractNumId1)
                                    {
                                        Level level = abstractNum.GetFirstChild <Level>();
                                        if (level.GetFirstChild <NumberingFormat>().Val != "decimal")
                                        {
                                            Util.printError("标题序号应为阿拉伯数字----" + title);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    Util.printError("标题序号应为阿拉伯数字----" + title);
                }
            }
        }
Beispiel #6
0
        public void detectText(Paragraph p, string curTitle, WordprocessingDocument doc)
        {
            IEnumerable <Run> runList  = p.Elements <Run>();
            string            paratext = p.InnerText.Trim();

            //手动输入的项目符号检测
            if (paratext.Length > 3)
            {
                int position;
                if (paratext.Substring(0, 4).IndexOf(")") > 0)
                {
                    ChineseNumbering = true;
                    if (EnglishNumbering)
                    {
                        Util.printError("一级项目标题圆括号格式不统一:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                    position = paratext.Substring(0, 4).IndexOf(")");
                    if (paratext.Substring(0, 4).IndexOf("(") < 0)
                    {
                        Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                    else
                    {
                        Match match = Regex.Match(paratext.Substring(0, position), @"[1-9][0-9]*");
                        if (!match.Success)
                        {
                            Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        }
                    }
                }
                else if (paratext.Substring(0, 4).IndexOf(")") > 0)
                {
                    EnglishNumbering = true;
                    if (ChineseNumbering)
                    {
                        Util.printError("一级项目标题圆括号格式不统一:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                    position = paratext.IndexOf(")");
                    if (position > 0)
                    {
                        if (position < 4)
                        {
                            Match match = Regex.Match(paratext.Substring(0, position), @"[1-9][0-9]*");
                            if (!match.Success)
                            {
                                Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                            }
                        }
                    }
                    else
                    {
                        Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
            }
            //自动生成的项目符号检测
            if (p.ParagraphProperties != null)
            {
                if (p.ParagraphProperties.NumberingProperties != null)
                {
                    string    numberingId = p.ParagraphProperties.NumberingProperties.NumberingId.Val;
                    string    ilvl        = p.ParagraphProperties.NumberingProperties.NumberingLevelReference.Val;
                    Numbering numbering1  = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;
                    IEnumerable <NumberingInstance> nums         = numbering1.Elements <NumberingInstance>();
                    IEnumerable <AbstractNum>       abstractNums = numbering1.Elements <AbstractNum>();
                    foreach (NumberingInstance num in nums)
                    {
                        if (num.NumberID == numberingId)
                        {
                            Int32 abstractNumId1 = num.AbstractNumId.Val;
                            foreach (AbstractNum abstractNum in abstractNums)
                            {
                                if (abstractNum.AbstractNumberId == abstractNumId1)
                                {
                                    Level level = abstractNum.GetFirstChild <Level>();
                                    if (level.GetFirstChild <NumberingFormat>().Val != "decimal")
                                    {
                                        Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                    else if (level.GetFirstChild <LevelText>().Val.InnerText != "(%1)" || level.GetFirstChild <LevelText>().Val.InnerText != "(%1)")
                                    {
                                        Util.printError("一级项目编号必须是一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (runList != null && p.ParagraphProperties != null)
            {
                if (!Util.correctIndentation(p, doc, tText[0]))
                {
                    Util.printError("此段落段落缩进应为2字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctfonts(p, doc, tText[1], "Times New Roman"))
                {
                    Util.printError("此段落存在字体错误,应为中文宋体,英文Times New Roman:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctsize(p, doc, tText[2]))
                {
                    Util.printError("此段落存在字号错误,应为小四:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctJustification(p, doc, tText[3]))
                {
                    Util.printError("此段落对齐方式应为两端对齐:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_Be(p, doc, tText[4]))
                {
                    Util.printError("此段落段前间距应为0行:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_Af(p, doc, tText[5]))
                {
                    Util.printError("此段落段后间距应为0行:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_line(p, doc, tText[6]))
                {
                    Util.printError("此段落行间距应为1.25倍:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
            }
        }
Beispiel #7
0
        public bool isTitle(Paragraph p, List <string> content, WordprocessingDocument doc)
        {
            bool   b = false;
            int    index, counter = 0;
            string title = Util.getFullText(p).Trim();

            Regex[] reg = new Regex[4];
            //一级标题
            reg[0] = new Regex(@"[1-9][0-9]*[\s]+?");
            reg[1] = new Regex(@"[1-9][0-9]*[..、\u4E00-\u9FA5]");
            //二级标题
            reg[2] = new Regex(@"[1-9][0-9]*\.[1-9][0-9]*");
            //三级标题
            reg[3] = new Regex(@"[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*");


            if (countWords(title) < 50)
            {
                Match m = reg[3].Match(title);

                for (index = 3; index > -1; index--)
                {
                    m = reg[index].Match(title);
                    if (m.Success == true)
                    {
                        break;
                    }
                }
                if (m.Index == 0)
                {
                    if (index > -1)
                    {
                        if (index == 1)
                        {
                            foreach (string s in content)
                            {
                                if (s.Contains(title) && Util.correctsize(p, doc, tTitle[0, 1]))
                                {
                                    b = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            b = true;
                        }
                    }
                    else
                    {
                        if (p.ParagraphProperties != null)
                        {
                            if (p.ParagraphProperties.NumberingProperties == null)
                            {
                                foreach (string s in content)
                                {
                                    if (s.Contains(title))
                                    {
                                        double similarity = title.Length / s.Length;
                                        if (similarity > 0.8)
                                        {
                                            b = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                string    numberingId = p.ParagraphProperties.NumberingProperties.NumberingId.Val;
                                Numbering numbering   = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;
                                IEnumerable <NumberingInstance> nums         = numbering.Elements <NumberingInstance>();
                                IEnumerable <AbstractNum>       abstractNums = numbering.Elements <AbstractNum>();
                                foreach (NumberingInstance num in nums)
                                {
                                    if (num.NumberID == numberingId)
                                    {
                                        Int32 abstractNumId1 = num.AbstractNumId.Val;
                                        foreach (AbstractNum abstractNum in abstractNums)
                                        {
                                            if (abstractNum.AbstractNumberId == abstractNumId1)
                                            {
                                                IEnumerable <Level> levels = abstractNum.Elements <Level>();
                                                foreach (Level level in levels)
                                                {
                                                    counter = counter + 1;
                                                    if (counter == 1)
                                                    {
                                                        if (level.GetFirstChild <LevelText>().Val.InnerText != "%1." && level.GetFirstChild <LevelText>().Val.InnerText != "%1")
                                                        {
                                                            break;
                                                        }
                                                    }
                                                    else if (counter == 2)
                                                    {
                                                        if (level.GetFirstChild <LevelText>().Val.InnerText != "%1.%2")
                                                        {
                                                            foreach (string s in content)
                                                            {
                                                                double similarity = title.Length / s.Length;
                                                                if (similarity > 0.8)
                                                                {
                                                                    b = true;
                                                                    break;
                                                                }
                                                            }
                                                            break;
                                                        }
                                                    }
                                                    else if (counter == 3)
                                                    {
                                                        if (level.GetFirstChild <LevelText>().Val.InnerText != "%1.%2.%3")
                                                        {
                                                            break;
                                                        }
                                                        b = true;
                                                    }
                                                    else
                                                    {
                                                        break;
                                                    }
                                                }
                                                break;
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(b);
        }
Beispiel #8
0
        public void detectText(Paragraph p, string curTitle, WordprocessingDocument doc)
        {
            IEnumerable <Run> runList  = p.Elements <Run>();
            string            paratext = Util.getFullText(p).Trim();
            bool isPro = false;

            //特殊手动输入的项目符号检测
            if (Regex.IsMatch(p.InnerText, @"^\ eq \\o\\ac\(○,[0-9]\)"))
            {
                if (!paratext.StartsWith(" ") || paratext.StartsWith("  "))
                {
                    Util.printError("正文项目编号与内容之间应有一个空格间隔:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
            }
            else if (Regex.IsMatch(p.InnerText, @"^\ eq \\o\\ac\(○,[^0-9]\)"))
            {
                Util.printError("正文项目编号错误,应为(1)(2)格式,子编号应为①②格式:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
            }
            //正常手动编号的项目符号检测
            isPro = detectProTitle(paratext, curTitle);
            //自动生成的项目符号检测
            if (p.ParagraphProperties != null)
            {
                if (p.ParagraphProperties.NumberingProperties != null)
                {
                    isPro = true;
                    string    numberingId = p.ParagraphProperties.NumberingProperties.NumberingId.Val;
                    string    ilvl        = p.ParagraphProperties.NumberingProperties.NumberingLevelReference.Val;
                    Numbering numbering1  = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;
                    IEnumerable <NumberingInstance> nums         = numbering1.Elements <NumberingInstance>();
                    IEnumerable <AbstractNum>       abstractNums = numbering1.Elements <AbstractNum>();
                    foreach (NumberingInstance num in nums)
                    {
                        if (num.NumberID == numberingId)
                        {
                            Int32 abstractNumId1 = num.AbstractNumId.Val;
                            foreach (AbstractNum abstractNum in abstractNums)
                            {
                                if (abstractNum.AbstractNumberId == abstractNumId1)
                                {
                                    Level level = abstractNum.GetFirstChild <Level>();
                                    if (level.GetFirstChild <NumberingFormat>().Val == "decimalEnclosedCircle" || level.GetFirstChild <NumberingFormat>().Val == "decimalEnclosedCircleChinese")
                                    {
                                        //采用自动编号的二级项目编号
                                    }
                                    else if (level.GetFirstChild <NumberingFormat>().Val != "decimal")
                                    {
                                        Util.printError("正文项目编号错误,应为(1)(2)格式,子编号应为①②格式:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                    else if (level.GetFirstChild <LevelText>().Val.InnerText != "(%1)" && level.GetFirstChild <LevelText>().Val.InnerText != "(%1)")
                                    {
                                        Util.printError("正文项目编号错误,应为(1)(2)格式,子编号应为①②格式:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            if (runList != null && p.ParagraphProperties != null)
            {
                Match m = Regex.Match(Util.getFullText(p), @"\s+\S");
                if (!Util.correctIndentation(p, doc, tText[0]))
                {
                    if (Util.getFullText(p).IndexOf("    ") == 0)
                    {
                        m = Regex.Match(Util.getFullText(p).Substring(4), @"\s+\S");
                        if (m.Success && m.Index == 0 && !isPro)
                        {
                            Util.printError("正文段落前存在多余空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        }
                    }
                    else
                    {
                        Util.printError("正文段落缩进错误,应为左侧缩进0字符,首行缩进" + tText[0] + "字符:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                else
                {
                    if (m.Success && m.Index == 0 && !isPro)
                    {
                        Util.printError("正文段落前存在多余空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                if (!Util.correctfonts(p, doc, tText[1], "Times New Roman"))
                {
                    Util.printError("正文段落字体错误,应为" + tText[1] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctsize(p, doc, tText[2]))
                {
                    if (isParagraphBreak)
                    {
                        isParagraphBreak = false;
                    }
                    else
                    {
                        if (isPro)
                        {
                            Util.printError("正文段落字号错误,应为" + tText[2] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        }
                        else
                        {
                            m = Regex.Match(paratext.Substring(paratext.Length - 1, 1), @"[,、“;0-9a-zA-Z\u4E00-\u9FA5]");
                            if (m.Success)
                            {
                                isParagraphBreak = false;
                                Util.printError("正文段落字号错误,应为" + tText[2] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                            }
                            else
                            {
                                isParagraphBreak = true;
                            }
                        }
                    }
                }
                //if (!Util.correctJustification(p, doc, tText[3]) && !Util.correctJustification(p, doc, "两端对齐"))
                //Util.printError("正文段落未" + tText[3] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                if (!Util.correctSpacingBetweenLines_Be(p, doc, tText[4]))
                {
                    Util.printError("正文段落段前距应为" + Util.getLine(tText[4]) + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_Af(p, doc, tText[5]))
                {
                    Util.printError("正文段落段后距应为" + Util.getLine(tText[5]) + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_line(p, doc, tText[6]))
                {
                    Util.printError("正文段落行间距应为" + Convert.ToDouble(tText[6]) / 240 + "倍行距:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctBold(p, doc, Convert.ToBoolean(tText[7])))
                {
                    Util.printError("正文段落" + (Convert.ToBoolean(tText[7]) ? "需要" : "不需") + "加粗:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
            }
        }
Beispiel #9
0
        public void detectText(Paragraph p, string curTitle, WordprocessingDocument doc)
        {
            IEnumerable <Run> runList  = p.Elements <Run>();
            string            paratext = Util.getFullText(p).Trim();
            bool isProTitle            = false;

            //手动输入的项目符号检测
            if (paratext.Length > 3)
            {
                Match m = Regex.Match(paratext, "[(].*?[)]");
                if (m.Success && m.Index == 0)
                {
                    isProTitle       = true;
                    ChineseNumbering = true;
                    if (count < 1 && EnglishNumbering)
                    {
                        Util.printError("正文段落项目编号错误,应为全文统一的中文圆括号:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        count++;
                    }
                    if (!Regex.Match(m.Value.Substring(1, m.Length - 2), "[0-9][1-9]*").Success)
                    {
                        Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                    if (paratext.Substring(m.Length, 1) != " " || paratext.Substring(m.Length, 2) == "  ")
                    {
                        Util.printError("正文段落项目编号与标题之间应为一个空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                m = Regex.Match(paratext, "[(].*?[)]");
                if (m.Success && m.Index == 0)
                {
                    isProTitle       = true;
                    EnglishNumbering = true;
                    if (count < 1 && ChineseNumbering)
                    {
                        Util.printError("正文段落项目编号错误,应为全文统一的中文圆括号:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        count++;
                    }
                    if (!Regex.Match(m.Value.Substring(1, m.Length - 2), "[0-9][1-9]*").Success)
                    {
                        Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                    if (paratext.Substring(m.Length, 1) != " " || paratext.Substring(m.Length, 2) == "  ")
                    {
                        Util.printError("正文段落项目编号与标题之间应为一个空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                m = Regex.Match(paratext, "[①②③④⑤⑥⑦⑧⑨⑩]");
                if (m.Success && m.Index == 0)
                {
                    isProTitle = true;
                    if (paratext.Substring(1, 1) != " " || paratext.Substring(1, 2) == "  ")
                    {
                        Util.printError("正文段落项目编号与标题之间应为一个空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                m = Regex.Match(paratext, ".[))、..]");
                if (m.Success && m.Index == 0)
                {
                    isProTitle = true;
                    Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                m = Regex.Match(paratext, "[◆●]");
                if (m.Success && m.Index == 0)
                {
                    isProTitle = true;
                    Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
            }
            //自动生成的项目符号检测
            if (p.ParagraphProperties != null)
            {
                if (p.ParagraphProperties.NumberingProperties != null)
                {
                    isProTitle = true;
                    string    numberingId = p.ParagraphProperties.NumberingProperties.NumberingId.Val;
                    string    ilvl        = p.ParagraphProperties.NumberingProperties.NumberingLevelReference.Val;
                    Numbering numbering1  = doc.MainDocumentPart.NumberingDefinitionsPart.Numbering;
                    IEnumerable <NumberingInstance> nums         = numbering1.Elements <NumberingInstance>();
                    IEnumerable <AbstractNum>       abstractNums = numbering1.Elements <AbstractNum>();
                    foreach (NumberingInstance num in nums)
                    {
                        if (num.NumberID == numberingId)
                        {
                            Int32 abstractNumId1 = num.AbstractNumId.Val;
                            foreach (AbstractNum abstractNum in abstractNums)
                            {
                                if (abstractNum.AbstractNumberId == abstractNumId1)
                                {
                                    Level level = abstractNum.GetFirstChild <Level>();
                                    if (level.GetFirstChild <NumberingFormat>().Val != "decimal")
                                    {
                                        Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                    else if (level.GetFirstChild <LevelText>().Val.InnerText != "(%1)" && level.GetFirstChild <LevelText>().Val.InnerText != "(%1)")
                                    {
                                        Util.printError("正文段落项目编号错误,应为一对圆括号,内为一个阿拉伯数字:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (runList != null && p.ParagraphProperties != null)
            {
                Match m = Regex.Match(Util.getFullText(p), @"\s+\S");
                if (!Util.correctIndentation(p, doc, tText[0]))
                {
                    if (Util.getFullText(p).IndexOf("    ") == 0)
                    {
                        m = Regex.Match(Util.getFullText(p).Substring(4), @"\s+\S");
                        if (m.Success && m.Index == 0 && !isProTitle)
                        {
                            Util.printError("正文段落前存在多余空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        }
                    }
                    else
                    {
                        Util.printError("正文段落缩进错误,应为首行缩进" + tText[0] + "字符:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                else
                {
                    if (m.Success && m.Index == 0 && !isProTitle)
                    {
                        Util.printError("正文段落前存在多余空格:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                    }
                }
                if (!Util.correctfonts(p, doc, tText[1], "Times New Roman"))
                {
                    Util.printError("正文段落字体错误,应为" + tText[1] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctsize(p, doc, tText[2]))
                {
                    if (isParagraphBreak)
                    {
                        isParagraphBreak = false;
                    }
                    else
                    {
                        if (isProTitle)
                        {
                            Util.printError("正文段落字号错误,应为" + tText[2] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                        }
                        else
                        {
                            m = Regex.Match(paratext.Substring(paratext.Length - 1, 1), @"[,、“;0-9a-zA-Z\u4E00-\u9FA5]");
                            if (m.Success)
                            {
                                isParagraphBreak = false;
                                Util.printError("正文段落字号错误,应为" + tText[2] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                            }
                            else
                            {
                                isParagraphBreak = true;
                            }
                        }
                    }
                }
                //if (!Util.correctJustification(p, doc, tText[3]) && !Util.correctJustification(p, doc, "两端对齐"))
                //Util.printError("正文段落未" + tText[3] + ":" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                if (!Util.correctSpacingBetweenLines_Be(p, doc, tText[4]))
                {
                    Util.printError("正文段落段前距应为" + Convert.ToDouble(tText[4]) / 240 + "行:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_Af(p, doc, tText[5]))
                {
                    Util.printError("正文段落段后距应为" + Convert.ToDouble(tText[5]) / 240 + "行:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctSpacingBetweenLines_line(p, doc, tText[6]))
                {
                    Util.printError("正文段落行间距应为" + Convert.ToDouble(tText[6]) / 240 + "倍行距:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
                if (!Util.correctBold(p, doc, Convert.ToBoolean(tText[7])))
                {
                    Util.printError("正文段落" + (Convert.ToBoolean(tText[7])? "需要":"不需") + "加粗:" + curTitle + "----" + (paratext.Length < 10 ? paratext : (paratext.Substring(0, 10) + "...")));
                }
            }
        }