/** * 该方法用于获取目录类型的段落中的目录文本, * 目录类型为Hyperlink时,使用该方法 */ private static String getHyperlinkFullText(Hyperlink p) { String text = ""; IEnumerable <Run> list = p.Elements <Run>(); TabChar pTab = new TabChar(); foreach (Run r in list) { Text pText = r.GetFirstChild <Text>(); if (pText != null) { text += pText.Text; } if (r.LastChild.GetType() == pTab.GetType()) { text += '\t'; } } return(text); }
public void detectCatalog(WordprocessingDocument doc) { Body body = doc.MainDocumentPart.Document.Body; MainDocumentPart mainPart = doc.MainDocumentPart; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); bool isContents = false; foreach (Paragraph p in paras) { Run r = p.GetFirstChild <Run>(); String fullText = ""; if (r != null) { fullText = Util.getFullText(p).Trim(); } //检测目录标题 if (fullText.Replace(" ", "") == "目录" && !isContents) { isContents = true; if (Util.getFullText(p).Trim().Length - 2 != int.Parse(Title[1])) { Util.printError("目录标题“目录”两字之间应有" + Title[1] + "个空格"); } if (!Util.correctJustification(p, doc, Title[2])) { Util.printError("目录标题未" + Title[2]); } if (!Util.correctSpacingBetweenLines_line(p, doc, Title[3])) { Util.printError("目录标题行间距错误,应为" + Util.DSmap[Title[3]]); } if (!Util.correctSpacingBetweenLines_Be(p, doc, Title[4])) { Util.printError("目录标题段前距错误,应为段前" + Util.getLine(Title[4])); } if (!Util.correctSpacingBetweenLines_Af(p, doc, Title[5])) { Util.printError("目录标题段后距错误,应为段后" + Util.getLine(Title[5])); } if (!Util.correctfonts(p, doc, Title[6], Title[6])) { Util.printError("目录标题字体错误,应为" + Title[6]); } if (!Util.correctsize(p, doc, Title[8])) { Util.printError("目录标题字号错误,应为" + Title[8]); } if (!Util.correctBold(p, doc, bool.Parse(Title[9]))) { if (bool.Parse(Title[9])) { Util.printError("目录标题未加粗"); } else { Util.printError("目录标题不需加粗"); } } continue; } if (isContents) { //hyperlink Hyperlink h = p.GetFirstChild <Hyperlink>(); IEnumerable <Run> runList = p.Elements <Run>(); //fieldchar bool flag = false; foreach (Run run in runList) { if (run.GetFirstChild <FieldChar>() != null) { flag = true; } } //判断是更新过的 if (h != null || flag) { String str = Util.getFullText(p).Replace(" ", ""); if (str.Length > 0) { if (str.Trim().Substring(str.Trim().Length - 1) == "-") { Util.printError("目录页码两端不应有\"-\"" + " ----" + str); } if (!Util.correctSpacingBetweenLines_line(p, doc, Text[1])) { Util.printError("目录行间距错误,应为" + Util.DSmap[Text[1]] + " ----" + str); } if (!Util.correctSpacingBetweenLines_Be(p, doc, Text[2])) { Util.printError("目录段前距错误,应为段前" + Util.getLine(Text[2]) + " ----" + str); } if (!Util.correctSpacingBetweenLines_Af(p, doc, Text[3])) { Util.printError("目录段后距错误,应为段后" + Util.getLine(Text[3]) + " ----" + str); } if (!Util.correctHyperlinkFonts(p, doc, Text[4], Text[5])) { Util.printError("目录字体错误,中文应为" + Text[4] + " 英文和数字应为" + Text[5] + " ----" + str); } if (!correctSize(p, doc, Text[6])) { Util.printError("目录字号错误,应为" + Text[6] + " ----" + str); } if (str.Length > 1 && (Regex.IsMatch(str.Substring(0, 1), @"[0-9]") || Regex.IsMatch(str.Substring(0, 1), @"[一二三四五六七八九十]"))) { calatogOrderDetect(p, doc); } else { if (!Util.correctIndentation(p, doc, Text[0])) { Util.printError("目录缩进错误,应为总体缩进 " + Text[0] + "字符 ----" + str); } } } IEnumerable <Run> hyperRun = null; if (h != null) { hyperRun = h.Elements <Run>(); } else { hyperRun = runList; } foreach (Run run in hyperRun) { if (run.GetFirstChild <TabChar>() != null) { RunProperties rPr = run.GetFirstChild <RunProperties>(); if (rPr != null) { if (rPr.GetFirstChild <RunFonts>() != null) { //if (rPr.GetFirstChild<RunFonts>().Ascii != "Times New Roman") // Util.printError("目录中标题与页码之间的小圆点字体应为Times New Roman" + " ----" + str); } } } } } else { break; } } } if (isContents == false) { Util.printError("论文缺少目录或目录格式不规范!"); } }
/* 目录检测 */ private void getContentsXml(WordprocessingDocument doc, String xmlFullPath) { Body body = doc.MainDocumentPart.Document.Body; MainDocumentPart mainPart = doc.MainDocumentPart; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); ParagraphProperties pPr = null; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFullPath); XmlNode root = xmlDoc.SelectSingleNode("Contents/ContentsTitle"); XmlNode spRoot = xmlDoc.SelectSingleNode("Contents/spErroInfo"); bool isContents = false; bool overFour = false; String curCount = ""; List <String> titleList = new List <string>(); foreach (Paragraph p in paras) { Run r = p.GetFirstChild <Run>(); String fullText = ""; if (r != null) { fullText = Tool.getFullText(p).Trim(); } //是否到了目录标题 if (fullText.Replace(" ", "") == "目录" && !isContents) { isContents = true; pageNum = this.getPageNum(pageNum, fullText); IEnumerable <Run> pRunList = p.Elements <Run>(); int spaceCount = Tool.getSpaceCount(fullText); //空格判断 if (spaceCount != 4) { XmlElement xe = xmlDoc.CreateElement("Text"); xe.InnerText = this.addPageInfo(pageNum) + "目录标题“目录”两字之间应有4个空格"; spRoot.AppendChild(xe); } pPr = p.GetFirstChild <ParagraphProperties>(); if (pPr != null) { if (pPr.GetFirstChild <Justification>() != null) { if (pPr.GetFirstChild <Justification>().Val.ToString().ToLower() != "center") { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录标题未居中"; spRoot.AppendChild(xe1); } } } //取出段落所有run,判断有文本的run的设置时候正确 if (pRunList != null) { bool flag1 = true; bool flag2 = true; foreach (Run pr in pRunList) { Text t = pr.GetFirstChild <Text>(); if (t != null) { if (pr != null) { RunProperties Rrpr = pr.GetFirstChild <RunProperties>(); if (Rrpr != null) { if (Rrpr.GetFirstChild <RunFonts>() != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != "黑体") { flag1 = false; } } } if (Rrpr.GetFirstChild <FontSize>() != null) { if (Rrpr.GetFirstChild <FontSize>().Val != null) { if (Rrpr.GetFirstChild <FontSize>().Val != "30") { flag2 = false; } } } } } } } if (!flag1) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录标题字体错误,应为黑体"; spRoot.AppendChild(xe1); } if (!flag2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录标题字号错误,应为小三号"; spRoot.AppendChild(xe1); } } continue; } if (isContents) { Hyperlink h = p.GetFirstChild <Hyperlink>();//取这个标签 Regex titleOne = new Regex("[1-9]"); IEnumerable <Run> runList = p.Elements <Run>(); bool flag = false; if (runList != null) { foreach (Run rr in runList) { //这个是为了处理未更新域的目录的 //更新域过的目录会有一个Hyperlinkd 的标签,没有跟新过的会有一个FieldChar标签 if (rr.GetFirstChild <FieldChar>() != null) { flag = true; } } } //判断是更新过的 if (h != null) { IEnumerable <Run> pRunList = h.Elements <Run>(); String str = getHyperlinkFullText(h); pageNum = this.getPageNum(str); bool flag1 = true; bool flag2 = true; foreach (Run pr in pRunList) { if (pr != null) { Text t = pr.GetFirstChild <Text>(); RunProperties Rrpr = pr.GetFirstChild <RunProperties>(); if (Rrpr != null) { if (Rrpr.GetFirstChild <RunFonts>() != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != "宋体" && Rrpr.GetFirstChild <RunFonts>().Ascii != "Times New Roman") { if (t != null) { if (t.Text.Replace(" ", "") != "") { flag1 = false; } } } } } /*if (Rrpr.GetFirstChild<FontSize>() != null) * { * if (Rrpr.GetFirstChild<FontSize>().Val != null) * { * if (Rrpr.GetFirstChild<FontSize>().Val != "24") * { * flag2 = false; * } * } * }*/ if (Tool.correctsize(p, doc, "24") == false) { flag2 = false; } } } } if (!flag1) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题字体错误,中文应为“宋体”英文为“Times New Roman”:{" + str + "}"; spRoot.AppendChild(xe1); } if (!flag2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题字号错误,应为小四号:{" + str + "}"; spRoot.AppendChild(xe1); } //通过.来判断是否超过3级 if (str.Split('.').Length > 4) { overFour = true; } if (str.Length > 1) { int spaceP = str.Trim().IndexOf(" "); Match m = titleOne.Match(str.Substring(0, 1)); if (m.Success) { if (getSpaceCount(str.Trim().Substring(0, spaceP + 3)) != 2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题与序号之间应空两个空格:" + "{" + str + "}"; spRoot.AppendChild(xe1); } else { if (str.Trim().Length > spaceP + 5) { if (getSpaceCount(str.Trim().Substring(0, spaceP + 4)) > 2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题与序号之间应空两个空格:" + "{" + str + "}"; spRoot.AppendChild(xe1); } } } } } xmlDoc.Save(xmlFullPath); } else if (flag) { //上面的是跟新过的,这个部分是没跟新过的,检测方法一样 IEnumerable <Run> pRunList = p.Elements <Run>(); String str = Tool.getFullText(p); pageNum = this.getPageNum(str); bool flag1 = true; bool flag2 = true; foreach (Run pr in pRunList) { if (pr != null) { Text t = pr.GetFirstChild <Text>(); RunProperties Rrpr = pr.GetFirstChild <RunProperties>(); if (Rrpr != null) { if (Rrpr.GetFirstChild <RunFonts>() != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != null) { if (Rrpr.GetFirstChild <RunFonts>().Ascii != "宋体" && Rrpr.GetFirstChild <RunFonts>().Ascii != "Times New Roman") { if (t != null) { if (t.Text.Replace(" ", "") != "") { flag1 = false; } } } } } /*if (Rrpr.GetFirstChild<FontSize>() != null) * { * if (Rrpr.GetFirstChild<FontSize>().Val != null) * { * if (Rrpr.GetFirstChild<FontSize>().Val != "24") * { * flag2 = false; * } * } * }*/ if (Tool.correctsize(p, doc, "24") == false) { flag2 = false; } } } } //不一致时flag为假,向xml中写入错误提示 if (!flag1) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题字体错误,中文应为“宋体”英文为“Times New Roman”:{" + str + "}"; spRoot.AppendChild(xe1); } if (!flag2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题字号错误,应为小四号:{" + str + "}"; spRoot.AppendChild(xe1); } if (str.Trim().Length > 7) { if (!str.Trim().Substring(0, 7).Contains(" ")) { if (str.Trim().Substring(0, 7).Split('.').Length > 4) { overFour = true; } } } if (str.Length > 1) { int spaceP = str.Trim().IndexOf(" "); Match m = titleOne.Match(str.Substring(0, 1)); if (m.Success) { curCount = str.Substring(0, 1); if (str.Substring(0, 1) != curCount) { titleList.Clear(); } titleList.Add(str); if (str.Substring(0, 1) != curCount) { } if (getSpaceCount(str.Trim().Substring(0, spaceP + 3)) != 2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题与序号之间应空两个空格:" + "{" + str + "}"; spRoot.AppendChild(xe1); } else { if (str.Trim().Length > spaceP + 5) { if (getSpaceCount(str.Trim().Substring(0, spaceP + 4)) > 2) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录中该章节标题与序号之间应空两个空格:" + "{" + str + "}"; spRoot.AppendChild(xe1); } } } } } xmlDoc.Save(xmlFullPath); } else { break; } } } if (overFour) { XmlElement xe1 = xmlDoc.CreateElement("Text"); xe1.InnerText = this.addPageInfo(pageNum) + "目录章节最多不应超过3级"; spRoot.AppendChild(xe1); xmlDoc.Save(xmlFullPath); } }