public Stream GetImageStream(string imageId) { var imagePart = _mainDocumentPart.GetPartById(imageId); var stream = imagePart.GetStream(); return(stream); }
/// <summary> /// Replace the image part with image memory stream. /// </summary> /// <param name="relId">The relationship id of the placeholder image.</param> /// <param name="imageStream">Image memory stream to replace the placeholder image.</param> /// <param name="width">Width of placeholder image.</param> /// <param name="height">Height of placeholder image.</param> private void UpdateImagePart(string relId, MemoryStream imageStream, int width, int height) { var originalBitmap = Image.FromStream(imageStream); var bitmap = originalBitmap; // resize image if (width != -1) { bitmap = new Bitmap(originalBitmap, width, height); } // Save image data to ImagePart var stream = new MemoryStream(); bitmap.Save(stream, originalBitmap.RawFormat); // Get the ImagePart var imagePart = (ImagePart)mainDocPart.GetPartById(relId); // Create a writer to the ImagePart var writer = new BinaryWriter(imagePart.GetStream()); // Overwrite the current image in the docx file package writer.Write(stream.ToArray()); // Close the ImagePart writer.Close(); }
/* * function:the first page and the second should have no header * params: list:the list of sectPrs * intlist:the location of sectPrs in body */ protected void detectFirstSection(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); MainDocumentPart Mpart = docx.MainDocumentPart; List <SectionProperties> list = secPrList(body); SectionProperties s = null; if (list.Count == 0) { return; } s = list[0]; IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (HeaderReference headr in headrs) { string ID = headr.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header != null && header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { PaperFormatDetection.Tools.Util.printError("论文封面和独创性声明应无页眉"); break; } } } foreach (FooterReference footr in footrs) { string ID = footr.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer != null && footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { PaperFormatDetection.Tools.Util.printError("论文封面和独创性声明应无页脚"); break; } } } }
public static string CopyImagePart(this WordprocessingDocument newDoc, string relId, MainDocumentPart mainDocumentPart) { var p = mainDocumentPart.GetPartById(relId) as ImagePart; var newPart = newDoc.MainDocumentPart.AddImagePart(p.ContentType); newPart.FeedData(p.GetStream(FileMode.Open, FileAccess.Read)); return(newDoc.MainDocumentPart.CreateRelationshipToPart(newPart)); //return newDoc.MainDocumentPart.GetIdOfPart(newPart); }
protected void detectLastSection(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; MainDocumentPart Mpart = docx.MainDocumentPart; int l = body.ChildElements.Count; if (body.ChildElements.GetItem(l - 1).GetType().ToString() == "DocumentFormat.OpenXml.Wordprocessing.SectionProperties") { SectionProperties s = body.GetFirstChild <SectionProperties>(); IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (FooterReference footr in footrs) { string ID = footr.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer != null && footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { Util.printError("论文版权使用授权书应无页脚"); break; } } } IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { string ID = headr.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header != null && header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { if (header.InnerText.Trim() != oddHeaderText) { Util.printError("论文版权使用授权书内容错误,应为:" + oddHeaderText); break; } } } } } }
public void replaceOriginalImages(Dictionary <string, string> imagesToChange) { foreach (string key in imagesToChange.Keys) { string newImagePath = key; string imageID = imagesToChange[newImagePath]; // go through the document and pull out the inline image elements IEnumerable <Drawing> imageElements = from run in m_mainDocPart.Document.Descendants <DocumentFormat.OpenXml.Wordprocessing.Run>() where run.Descendants <Drawing>().First() != null select run.Descendants <Drawing>().First(); ImagePart imagePart = (ImagePart)m_mainDocPart.GetPartById(imageID); m_imageInBytes = File.ReadAllBytes(newImagePath); BinaryWriter writer = new BinaryWriter(imagePart.GetStream()); writer.Write(m_imageInBytes); writer.Close(); } m_wordProcessingDocument.Close(); }
public static Header FindHeader( this MainDocumentPart mainDocumentPart, string headerReferenceId) { if (headerReferenceId == null) { return(null); } var headerPart = (HeaderPart)mainDocumentPart.GetPartById(headerReferenceId); return(headerPart.Header); }
public static Footer FindFooter( this MainDocumentPart mainDocumentPart, string footerReferenceId) { if (footerReferenceId == null) { return(null); } var footerPart = (FooterPart)mainDocumentPart.GetPartById(footerReferenceId); return(footerPart.Footer); }
/// <summary> /// Updates the image in an image placeholder /// </summary> /// <param name="placeholderName">Image placeholder tag name</param> /// <param name="newImage">physical path to the new image file</param> public void UpdateImage(string placeholderName, string newImage) { // Get the id of the image placeholder string relId = GetImageRelID(placeholderName); // Get the Imagepart ImagePart imagePart = (ImagePart)mainDocPart.GetPartById(relId); // Read the new image file byte[] imageBytes = File.ReadAllBytes(newImage); // Create a writer to the imagepart BinaryWriter writer = new BinaryWriter(imagePart.GetStream()); // Overwrite the current image in the docx file package writer.Write(imageBytes); // Close the imagepart writer.Close(); }
/// <summary> /// Adds three headers to the document (default, even and first). /// </summary> public void AddHeaders() { var sectionProperty = GetBodySectionProperty(); var references = sectionProperty.Descendants <HeaderReference>(); foreach (var reference in references) { var part = MainDocumentPart.GetPartById(reference.Id); MainDocumentPart.DeletePart(reference.Id); } sectionProperty.RemoveAllChildren <HeaderReference>(); headers = new List <Header> { AddHeader(HeaderFooterValues.Default), AddHeader(HeaderFooterValues.Even), AddHeader(HeaderFooterValues.First) }; }
public void ReplaceImage(string destinationFile, string mapImageUrl, string imageId) { WordprocessingDocument m_wordProcessingDocument = WordprocessingDocument.Open(destinationFile, true); MainDocumentPart m_mainDocPart = m_wordProcessingDocument.MainDocumentPart; // go through the document and pull out the inline image elements IEnumerable <Drawing> imageElements = from run in m_mainDocPart.Document.Descendants <DocumentFormat.OpenXml.Wordprocessing.Run>() where run.Descendants <Drawing>().First() != null select run.Descendants <Drawing>().First(); ImagePart imagePart = (ImagePart)m_mainDocPart.GetPartById(imageId); var webClient = new WebClient(); byte[] m_imageInBytes = webClient.DownloadData(mapImageUrl); BinaryWriter writer = new BinaryWriter(imagePart.GetStream()); writer.Write(m_imageInBytes); writer.Close(); m_wordProcessingDocument.Close(); }
protected void detectOddEven(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; MainDocumentPart Mpart = docx.MainDocumentPart; List <SectionProperties> list = secPrList(body); List <int> intlist = secPrListInt(body); SectionProperties s = null; for (int i = 2; i <= list.Count <SectionProperties>(); i++) { s = list[i - 1]; string chapter = null; if (intlist[i - 1] < body.ChildElements.Count) { chapter = getPicMassage(intlist[i - 1], body) + "----所在章的"; } IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); HeaderReference headereven = null; HeaderReference headerdefault = null; foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.Default) { headerdefault = headr; } if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } } if (headerdefault != null) { //奇数页 string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; Paragraph p = header.GetFirstChild <Paragraph>(); if (header.InnerText != null) { string headername = header.InnerText.Trim(); if (headername != oddHeaderText) { Util.printError("页眉内容错误,应为:" + oddHeaderText + "----" + chapter + "奇数页"); } } } //偶数页 if (headereven != null) { string ID = headereven.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; Paragraph p = header.GetFirstChild <Paragraph>(); if (header.InnerText != null) { if (header.InnerText != "") { string headername = header.InnerText.Trim(); if (headername != evenHeaderText && !flag) { // Util.printError("页眉内容错误,应为:" + evenHeaderText + "----" + chapter + "奇数页"); } } } } } }
//本科论文页眉页脚检测 private void detectUnderHeaderFooter(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); SectionProperties scetpr = body.GetFirstChild <SectionProperties>(); MainDocumentPart Mpart = docx.MainDocumentPart; List <SectionProperties> list = secPrList(body); list.Add(scetpr); //List<int> intlist = secPrListInt(body); //节的位置 string docname = getChineseTitleOfDocx(paras, indexOfChineseTitleOfDocx); //论文中文题目 if (list.Count == 0) { return; } for (int i = 2; i <= list.Count <SectionProperties>(); i++) { SectionProperties s = list[i - 1]; IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { #region 页眉 if (headr != null && headr.Type == HeaderFooterValues.Default) { string ID = headr.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText.Trim() != "") { Paragraph p = header.GetFirstChild <Paragraph>(); string headername = header.InnerText.Trim(); if (Util.correctfonts(p, docx, CNheaderFonts, ENHeaderFonts) == false) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页眉字体错误,应为:" + CNheaderFonts); } if (Util.correctsize(p, docx, headerFontsize) == false) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页眉字号错误,应为:" + headerFontsize); } if (Util.correctJustification(p, docx, headerJustification) == false) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页眉对齐错误,应为:" + headerJustification); } if (headername != docname) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页眉内容错误,应为:" + docname); } } } #endregion } IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (FooterReference footr in footrs) { if (footr != null) { string ID = footr.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; Paragraph p = footer.GetFirstChild <Paragraph>(); if (footer.InnerText != null && footer.InnerText.Trim() != "") { if (Util.correctfonts(p, docx, CNFooterFonts, ENFooterFonts) == false) { Util.printError("第" + i + "节" + "页脚字体错误,应为:" + CNFooterFonts); } if (Util.correctsize(p, docx, footerFontsize) == false) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页脚字号错误,应为:" + footerFontsize); } if (Util.correctJustification(p, docx, footerJustification) == false) { PaperFormatDetection.Tools.Util.printError("第" + i + "节" + "页脚对齐错误,应为:" + footerJustification); } } } } } }
//判断偶数页 protected void detectEven(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); MainDocumentPart Mpart = docx.MainDocumentPart; List <SectionProperties> list = secPrList(body); SectionProperties scetpr = body.GetFirstChild <SectionProperties>(); list.Add(scetpr); HeaderReference headereven = null; FooterReference footereven = null; if (list.Count == 0) { return; } bool[] flag = { false, false, false, false }; //终止循环 bool[] flagyj = { false, false, false }; //终止循环 for (int i = 2; i <= list.Count <SectionProperties>(); i++) { SectionProperties s = list[i - 1]; IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } } if (headereven != null) { string ID = headereven.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; Paragraph p = header.GetFirstChild <Paragraph>(); if (header.InnerText != null) { string headername = header.InnerText.Trim(); if (flag[0] == false && Util.correctfonts(p, docx, CNheaderFonts, ENHeaderFonts) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页眉字体错误,应为:" + CNheaderFonts); flag[0] = true; } if (flag[1] == false && Util.correctsize(p, docx, headerFontsize) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页眉字号错误,应为:" + headerFontsize); flag[1] = true; } if (flag[2] == false && JustificationCenter(p, Mpart) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页眉未居中"); flag[2] = true; } if (flag[3] == false && headername != evenHeaderText) { PaperFormatDetection.Tools.Util.printError("偶数页页眉内容错误,应为:" + evenHeaderText); flag[3] = true; } } } //页脚 IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (FooterReference footr in footrs) { if (footr.Type == HeaderFooterValues.Default) { footereven = footr; } } if (footereven != null) { string ID = footereven.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; Paragraph p = footer.GetFirstChild <Paragraph>(); if (footer.InnerText != null) { if (flagyj[0] == false && Util.correctfonts(p, docx, CNFooterFonts, ENFooterFonts) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页脚字体错误,应为" + CNFooterFonts); flagyj[0] = true; } if (flagyj[1] == false && Util.correctsize(p, docx, footerFontsize) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页脚字号错误,应为:" + footerFontsize); flagyj[1] = true; } if (flagyj[2] == false && JustificationCenter(p, Mpart) == false) { PaperFormatDetection.Tools.Util.printError("偶数页页脚未居中"); flagyj[2] = true; } } } } }
private void otherSectionHeader(List <SectionProperties> list, List <int> intlist, WordprocessingDocument wordpro, string docxPath, XmlNode root, XmlDocument xmlDocx, string name, int flag) { MainDocumentPart Mpart = wordpro.MainDocumentPart; // List<int> sectionnumber = new List<int>(20); HeaderReference headerfirst = null; HeaderReference headereven = null; HeaderReference headerdefault = null; if (list.Count == 0) { return; } SectionProperties s = list[0]; IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.First) { headerfirst = headr; } if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } if (headr.Type == HeaderFooterValues.Default) { headerdefault = headr; } } for (int i = 2; i <= list.Count <SectionProperties>(); i++) { bool no2pageinsecondsection = !no2PageInfirstSection(intlist[0], wordpro.MainDocumentPart.Document.Body); s = list[i - 1]; List <int> chapterTitle = Tool.getTitlePosition(wordpro); string chapter = Tool.Chapter(chapterTitle, intlist[i - 1], wordpro.MainDocumentPart.Document.Body); TitlePage tp = s.GetFirstChild <TitlePage>(); headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.First) { headerfirst = headr; } if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } if (headr.Type == HeaderFooterValues.Default) { headerdefault = headr; } } if (tp != null) { //合乎规范 XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉命名不规范,不应该设置首页不同||" + chapter; root.AppendChild(xml); } else { //奇数页 if (headerdefault == null) { if (no2pageinsecondsection)//检测独创性声明页在不在第二节 { continue; } /*else * { * * }*/ } string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; Paragraph p = header.GetFirstChild <Paragraph>(); if (header.InnerText != null) { string headername = header.InnerText.Trim(); //字体 if (!Tool.correctfonts(p, wordpro, "宋体", "Times New Roman")) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉字体应为宋体||" + chapter; root.AppendChild(xml); } //字号 if (!Tool.correctsize(p, wordpro, "21")) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉字号应为五号||" + chapter; root.AppendChild(xml); } //居中 if (!JustificationCenter(p, Mpart)) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉应居中||" + chapter; root.AppendChild(xml); } if (flag == 1) { //合乎规范 if (!no2pageinsecondsection) { if (headername != "大连理工大学专业学位硕士学位论文") { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "奇数页页眉命名不规范应为:“大连理工大学专业学位硕士学位论文”||" + chapter; root.AppendChild(xml); } } else { if (i != 2) { if (headername != "大连理工大学专业学位硕士学位论文") { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "奇数页页眉命名不规范应为:“大连理工大学专业学位硕士学位论文”||" + chapter; root.AppendChild(xml); } } } } if (flag == 0) { if (!no2pageinsecondsection) { if (headername != "大连理工大学硕士学位论文") { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "奇数页页眉命名不规范应为:“大连理工大学硕士学位论文”||" + chapter; root.AppendChild(xml); } } else { if (i != 2) { if (headername != "大连理工大学硕士学位论文") { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "奇数页页眉命名不规范应为:“大连理工大学硕士学位论文”||" + chapter; root.AppendChild(xml); } } } } } else { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "缺少奇数页页眉||" + chapter; root.AppendChild(xml); } //偶数页 ID = headereven.Id.ToString(); hp = (HeaderPart)Mpart.GetPartById(ID); header = hp.Header; if (header.InnerText != null) { //字体 if (!Tool.correctfonts(p, wordpro, "宋体", "Times New Roman")) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉字体应为宋体||" + chapter; root.AppendChild(xml); } //字号 if (!Tool.correctsize(p, wordpro, "21")) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉字号应为五号||" + chapter; root.AppendChild(xml); } //居中 if (!JustificationCenter(p, Mpart)) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "页眉应居中||" + chapter; root.AppendChild(xml); } string headername = header.GetFirstChild <Paragraph>().InnerText.Trim(); //合乎规范 if (!no2pageinsecondsection) { if (headername != name) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "偶数页页眉命名不规范,应为论文中文题目||" + chapter; root.AppendChild(xml); } } else { if (i != 2) { if (headername != name) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "偶数页页眉命名不规范,应为论文中文题目||" + chapter; root.AppendChild(xml); } } } } else { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "缺少偶数页页眉||" + chapter; root.AppendChild(xml); } } } }
protected void detectHeaderFooter(WordprocessingDocument docx) { Body body = docx.MainDocumentPart.Document.Body; IEnumerable <Paragraph> paras = body.Elements <Paragraph>(); MainDocumentPart Mpart = docx.MainDocumentPart; List <SectionProperties> list = secPrList(body); List <int> intlist = secPrListInt(body); SectionProperties scetpr = body.GetFirstChild <SectionProperties>(); //list.Add(scetpr); //intlist.Add(body.ChildElements.Count() - 1); SectionProperties s = null; for (int i = 2; i <= list.Count <SectionProperties>(); i++) { s = list[i - 1]; string chapter = null; if (intlist[i - 1] < body.ChildElements.Count) { chapter = getPicMassage(intlist[i - 1], body) + "---所在章的"; } //页眉 IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); foreach (HeaderReference headr in headrs) { string headertype = null; if (headr.Type == HeaderFooterValues.Default) { headertype = "奇数页"; } if (headr.Type == HeaderFooterValues.Even) { headertype = "偶数页"; } string ID = headr.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; Paragraph p = header.GetFirstChild <Paragraph>(); if (header.InnerText != null) { if (Util.correctfonts(p, docx, CNheaderFonts, ENHeaderFonts) == false) { PaperFormatDetection.Tools.Util.printError("页眉字体错误,应为:" + CNheaderFonts + "----" + chapter + headertype); } if (Util.correctsize(p, docx, headerFontsize) == false) { PaperFormatDetection.Tools.Util.printError("页眉字号错误,应为:" + headerFontsize + "----" + headertype); } if (JustificationCenter(p, Mpart) == false) { PaperFormatDetection.Tools.Util.printError("页眉未居中" + "----" + chapter + headertype); } } } //页脚 IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (FooterReference footr in footrs) { string type = null; if (footr.Type == HeaderFooterValues.Default) { type = "奇数页"; } if (footr.Type == HeaderFooterValues.Even) { type = "偶数页"; } string ID = footr.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; Paragraph p = footer.GetFirstChild <Paragraph>(); if (footer.InnerText.Trim() != "") { if (Util.correctfonts(p, docx, CNFooterFonts, ENFooterFonts) == false) { Util.printError("页脚字体错误,应为:" + CNFooterFonts + "----" + chapter + type); } if (Util.correctsize(p, docx, footerFontsize) == false) { Util.printError("页脚字号错误,应为:" + footerFontsize + "----" + chapter + type); } if (JustificationCenter(p, Mpart) == false) { Util.printError("页脚未居中" + "----" + chapter + type); } } } } }
/* * function:the first page and the second should have no header * params: list:the list of sectPrs * intlist:the location of sectPrs in body * wordpro: * docxpath: * root:xml root * xmlDocx * return:void */ static private void firstSection(List <SectionProperties> list, List <int> intlist, WordprocessingDocument wordpro, string docxPath, XmlNode root, XmlDocument xmlDocx) { MainDocumentPart Mpart = wordpro.MainDocumentPart; SectionProperties s = null; if (list.Count == 0) { return; } s = list[0]; TitlePage tp = s.GetFirstChild <TitlePage>(); int location = intlist[0]; bool no2page = no2PageInfirstSection(intlist[0], Mpart.Document.Body); IEnumerable <HeaderReference> headrs = s.Elements <HeaderReference>(); HeaderReference headerfirst = null; HeaderReference headereven = null; HeaderReference headerdefault = null; FooterReference footerfirst = null; FooterReference footereven = null; FooterReference footerdefault = null; IEnumerable <FooterReference> footrs = s.Elements <FooterReference>(); foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.First) { headerfirst = headr; } if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } if (headr.Type == HeaderFooterValues.Default) { headerdefault = headr; } } foreach (FooterReference footr in footrs) { if (footr.Type == HeaderFooterValues.First) { footerfirst = footr; } if (footr.Type == HeaderFooterValues.Even) { footereven = footr; } if (footr.Type == HeaderFooterValues.Default) { footerdefault = footr; } } //如果设置了首页不同 if (tp != null) { if (headerfirst != null) { string ID = headerfirst.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "封面应无页眉"; root.AppendChild(xml); } } } if (footerfirst != null) { string ID = footerfirst.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "封面应无页脚"; root.AppendChild(xml); } } } if (no2page)//若独创性说明在第一节内 { //查看是否设置了奇偶页不同 Settings setting = Mpart.DocumentSettingsPart.Settings; if (setting.GetFirstChild <EvenAndOddHeaders>() != null)//若设置了奇偶页不同 { if (headereven != null) { string ID = headereven.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footereven != null) { string ID = footereven.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "封面应无页脚"; root.AppendChild(xml); } } } } else//若没有设置奇偶页不同 { if (headerdefault != null) { string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footerdefault != null) { string ID = footerdefault.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } } } //若没有设置首页不同 else { if (headerdefault != null) { string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "封面应无页眉"; root.AppendChild(xml); } } } if (footerdefault != null) { string ID = footerdefault.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "封面应无页脚"; root.AppendChild(xml); } } } if (no2page)//如果独创性说明在第一节 { Settings setting = Mpart.DocumentSettingsPart.Settings; if (setting.GetFirstChild <EvenAndOddHeaders>() != null)//设置了奇偶页不同,则独创性说明是偶数页设置 { if (headereven != null) { string ID = headereven.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footereven != null) { string ID = footereven.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } else//没有设置奇偶页不同 { if (headerdefault != null) { string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footerdefault != null) { string ID = footerdefault.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } } } if (!no2page)//第二页独创性说明不在第一节 { //确定独创性说明在不在第二节,不在报错 if (!no2PageInfirstSection(intlist[1] - 1, Mpart.Document.Body)) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "缺少独创性声明页"; root.AppendChild(xml); } else { s = list[1]; headrs = s.Elements <HeaderReference>(); tp = s.GetFirstChild <TitlePage>(); foreach (HeaderReference headr in headrs) { if (headr.Type == HeaderFooterValues.First) { headerfirst = headr; } if (headr.Type == HeaderFooterValues.Even) { headereven = headr; } if (headr.Type == HeaderFooterValues.Default) { headerdefault = headr; } } if (tp != null)//第二节设置了首页不同 { if (headerfirst != null) { string ID = headerfirst.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footerfirst != null) { string ID = footerfirst.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } else//没有设置首页不同 { Settings setting = Mpart.DocumentSettingsPart.Settings; if (setting.GetFirstChild <EvenAndOddHeaders>() != null)//设置了奇偶页不同 { if (headereven != null) { string ID = headereven.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footereven != null) { string ID = footereven.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } else//没有设置奇偶页不同 { if (headerdefault != null) { string ID = headerdefault.Id.ToString(); HeaderPart hp = (HeaderPart)Mpart.GetPartById(ID); Header header = hp.Header; if (header.InnerText != null) { if (header.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页眉"; root.AppendChild(xml); } } } if (footerdefault != null) { string ID = footerdefault.Id.ToString(); FooterPart fp = (FooterPart)Mpart.GetPartById(ID); Footer footer = fp.Footer; if (footer.InnerText != null) { if (footer.InnerText.Trim().Length > 0) { XmlElement xml = xmlDocx.CreateElement("Text"); xml.InnerText = "独创性声明所在的页应无页脚"; root.AppendChild(xml); } } } } } } } }