//gavdcodeend 10 //gavdcodebegin 11 public static void WordOpenXmlCreateHeader() { using (WordprocessingDocument myWordDoc = WordprocessingDocument.Open(@"C:\Temporary\WordDoc01.docx", true)) { MainDocumentPart docMainPart = myWordDoc.MainDocumentPart; docMainPart.DeleteParts(docMainPart.HeaderParts); HeaderPart headerPart = docMainPart.AddNewPart <HeaderPart>(); GenerateHeaderPartContentFirst(headerPart); string headerPartId = docMainPart.GetIdOfPart(headerPart); IEnumerable <SectionProperties> allSections = docMainPart.Document.Body.Elements <SectionProperties>(); foreach (var oneSection in allSections) { oneSection.RemoveAllChildren <HeaderReference>(); oneSection.RemoveAllChildren <TitlePage>(); oneSection.PrependChild <HeaderReference>(new HeaderReference() { Id = headerPartId, Type = HeaderFooterValues.First }); oneSection.PrependChild <TitlePage>(new TitlePage()); } docMainPart.Document.Save(); } }
/// <summary> /// Set a new header in a document /// </summary> /// <param name="header">XDocument containing the header to add in the document</param> /// <param name="type">The header part type</param> public static OpenXmlPowerToolsDocument SetHeader(WmlDocument doc, XDocument header, HeaderType type, int sectionIndex) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { // Removes the reference in the document.xml and the header part if those already // exist XElement headerReferenceElement = GetHeaderReference(document, type, sectionIndex); if (headerReferenceElement != null) { GetHeaderPart(document, type, sectionIndex).RemovePart(); headerReferenceElement.Remove(); } // Add the new header HeaderPart headerPart = document.MainDocumentPart.AddNewPart <HeaderPart>(); headerPart.PutXDocument(header); // Creates the relationship of the header inside the section properties in the document string relID = document.MainDocumentPart.GetIdOfPart(headerPart); AddHeaderReference(document, type, relID, sectionIndex); // add in the settings part the EvendAndOddHeaders. this element // allow to see the odd and even headers and headers in the document. SettingAccessor.AddEvenAndOddHeadersElement(document); } return(streamDoc.GetModifiedDocument()); } }
private void ApplyHeader(WordprocessingDocument doc, string input) { if (!string.IsNullOrWhiteSpace(input)) { MainDocumentPart mainDocPart = doc.MainDocumentPart; HeaderPart headerPart = mainDocPart.AddNewPart <HeaderPart>("r97"); Header header = new Header(); Paragraph paragraph = new Paragraph() { }; Run run = new Run(); run.Append(ConvertHtmlToOpenXml(input)); paragraph.Append(run); header.Append(paragraph); headerPart.Header = header; SectionProperties sectionProperties = mainDocPart.Document.Body.Descendants <SectionProperties>().FirstOrDefault(); if (sectionProperties == null) { sectionProperties = new SectionProperties() { }; mainDocPart.Document.Body.Append(sectionProperties); } HeaderReference headerReference = new HeaderReference() { Type = DocumentFormat.OpenXml.Wordprocessing.HeaderFooterValues.Default, Id = "r97" }; sectionProperties.InsertAt(headerReference, 0); } }
private BookmarkStart FindBookmarkStartInHeader(HeaderPart header, string name) { var bkmStart = header.RootElement.Descendants <BookmarkStart>() .FirstOrDefault(b => string.Equals(b.Name.ToString(), name, StringComparison.OrdinalIgnoreCase)); return(bkmStart); }
private BookmarkEnd FindBookmarkEndInHeader(HeaderPart header, string idBkm) { var bkmEnd = header.RootElement.Descendants <BookmarkEnd>() .FirstOrDefault(b => string.Equals(b.Id, idBkm, StringComparison.OrdinalIgnoreCase)); return(bkmEnd); }
private static void ApplyHeader2(WordprocessingDocument doc, CommitteeVM data) { // Get the main document part. MainDocumentPart mainDocPart = doc.MainDocumentPart; HeaderPart headerPart1 = mainDocPart.AddNewPart <HeaderPart>("r97"); Header header1 = new Header(); header1.Append(CreateParagraph(" استمارة رقم 14 د ", JustificationValues.Right, "", "12pt")); header1.Append(CreateParagraph($"أسماء السادة أعضاء لجنة {data.Name} رقم ({data.Number}) ", JustificationValues.Center, "", "15pt", true)); header1.Append(CreateParagraph($"الدور {data.Term} {data.SchoolYear} ", JustificationValues.Left, "", "12pt")); headerPart1.Header = header1; SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants <SectionProperties>().FirstOrDefault(); if (sectionProperties1 == null) { sectionProperties1 = new SectionProperties() { }; mainDocPart.Document.Body.Append(sectionProperties1); } HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" }; sectionProperties1.InsertAt(headerReference1, 0); }
/// <summary> /// Set a new header in a document /// </summary> /// <param name="header">XDocument containing the header to add in the document</param> /// <param name="type">The header part type</param> public void SetHeader(XDocument header, HeaderType type) { // Removes the reference in the document.xml and the header part if those already // exist XElement headerReferenceElement = GetHeaderReference(type); if (headerReferenceElement != null) { RemoveHeader(type); headerReferenceElement.Remove(); } // Add the new header XDocument newHeader; HeaderPart headerPart = parentDocument.Document.MainDocumentPart.AddNewPart <HeaderPart>(); newHeader = parentDocument.GetXDocument(headerPart); newHeader.Add(header.Root); // Creates the relationship of the header inside the section properties in the document string relID = parentDocument.Document.MainDocumentPart.GetIdOfPart(headerPart); AddHeaderReference(type, relID); // add in the settings part the EvendAndOddHeaders. this element // allow to see the odd and even headers and headers in the document. parentDocument.Setting.AddEvendAndOddHeadersElement(); }
//private void OpenHeaderFooter(OXmlOpenHeaderFooter element) //{ // CreateSectionProperties(); // OpenXmlCompositeElement headerFooter; // if (element.Header) // { // HeaderPart headerPart = _mainPart.AddNewPart<HeaderPart>(); // headerFooter = new Header(); // headerPart.Header = (Header)headerFooter; // string headerPartId = _mainPart.GetIdOfPart(headerPart); // _sectionProperties.AppendChild(new HeaderReference { Id = headerPartId, Type = element.HeaderType }); // } // else // { // FooterPart footerPart = _mainPart.AddNewPart<FooterPart>(); // headerFooter = new Footer(); // footerPart.Footer = (Footer)headerFooter; // string footerPartId = _mainPart.GetIdOfPart(footerPart); // _sectionProperties.AppendChild(new FooterReference { Id = footerPartId, Type = element.HeaderType }); // } // AddHeaderFooterNamespaceDeclaration((OpenXmlPartRootElement)headerFooter); // SetHeaderFooterProperties(element.HeaderType); // _element = headerFooter; // _headerFooter = true; //} private void OpenHeader(OXmlOpenHeaderElement element) { CreateSectionProperties(); //if (element.Header) //{ HeaderPart headerPart = _mainPart.AddNewPart <HeaderPart>(); OpenXmlCompositeElement header = new Header(); headerPart.Header = (Header)header; string headerPartId = _mainPart.GetIdOfPart(headerPart); _sectionProperties.AppendChild(new HeaderReference { Id = headerPartId, Type = element.HeaderType }); //} //else //{ // FooterPart footerPart = _mainPart.AddNewPart<FooterPart>(); // headerFooter = new Footer(); // footerPart.Footer = (Footer)headerFooter; // string footerPartId = _mainPart.GetIdOfPart(footerPart); // _sectionProperties.AppendChild(new FooterReference { Id = footerPartId, Type = element.HeaderType }); //} AddHeaderFooterNamespaceDeclaration((OpenXmlPartRootElement)header); SetHeaderFooterProperties(element.HeaderType); _element = header; //_headerFooter = true; _currentElement = OXmlDocElementType.Header; }
public static ImagePart AddImagePart(this HeaderPart headerPart, Stream stream) { var imagePart = headerPart.AddImagePart(ImagePartType.Jpeg); imagePart.FeedData(stream); return(imagePart); }
internal Header(HeaderPart part, DocX document, HeaderFooterValues type) { headerPart = part; header = part.Header; this.document = document; this.type = type; }
public static void GenerateHeaderPart2Content(HeaderPart headerPart2) { Header header2 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 w15 w16se w16cid wp14" } }; header2.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); header2.AddNamespaceDeclaration("cx", "http://schemas.microsoft.com/office/drawing/2014/chartex"); header2.AddNamespaceDeclaration("cx1", "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"); header2.AddNamespaceDeclaration("cx2", "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"); header2.AddNamespaceDeclaration("cx3", "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"); header2.AddNamespaceDeclaration("cx4", "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"); header2.AddNamespaceDeclaration("cx5", "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"); header2.AddNamespaceDeclaration("cx6", "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"); header2.AddNamespaceDeclaration("cx7", "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"); header2.AddNamespaceDeclaration("cx8", "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"); header2.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); header2.AddNamespaceDeclaration("aink", "http://schemas.microsoft.com/office/drawing/2016/ink"); header2.AddNamespaceDeclaration("am3d", "http://schemas.microsoft.com/office/drawing/2017/model3d"); header2.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); header2.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); header2.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); header2.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); header2.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); header2.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); header2.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); header2.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); header2.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); header2.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml"); header2.AddNamespaceDeclaration("w16cid", "http://schemas.microsoft.com/office/word/2016/wordml/cid"); header2.AddNamespaceDeclaration("w16se", "http://schemas.microsoft.com/office/word/2015/wordml/symex"); header2.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); header2.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); header2.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); header2.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Paragraph paragraph277 = new Paragraph() { RsidParagraphAddition = "00F225EA", RsidRunAdditionDefault = "009E39C2", ParagraphId = "637CAE62", TextId = "77777777" }; Run run406 = new Run(); CarriageReturn carriageReturn1 = new CarriageReturn(); run406.Append(carriageReturn1); paragraph277.Append(run406); header2.Append(paragraph277); headerPart2.Header = header2; }
public static void CreateDoc(string fileName, CommitteeVM data, byte part = 1) { // Create a Wordprocessing document. using (WordprocessingDocument myDoc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)) { // Add a new main document part. MainDocumentPart mainPart = myDoc.AddMainDocumentPart(); HeaderPart headerPart = mainPart.AddNewPart <HeaderPart>(); FooterPart footerPart = mainPart.AddNewPart <FooterPart>(); //Create DOM tree for simple document. mainPart.Document = new Document(); Body body = new Body(); mainPart.Document.Append(body); SectionProperties sectionProps = new SectionProperties(); PageMargin pageMargin = new PageMargin() { Top = 1008, Right = (UInt32Value)1008U, Bottom = 1008, Left = (UInt32Value)1008U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U }; sectionProps.Append(pageMargin); mainPart.Document.Body.Append(sectionProps); switch (part) { case 1: //Add master Table AddMasterTable(data, mainPart); ApplyHeader(myDoc, data); break; case 2: //Add third table AddThirdTable(data, mainPart); ApplyHeader2(myDoc, data); break; case 3: //Add second table AddSecondTable(data, mainPart); ApplyHeader2(myDoc, data); break; } ApplyFooter(myDoc); // Save changes to the main document part. mainPart.Document.Save(); } }
public void ApplyHeader(WordprocessingDocument doc) { MainDocumentPart mainDocPart = doc.MainDocumentPart; HeaderPart headerPart1 = mainDocPart.AddNewPart <HeaderPart>("r97"); //ImagePart imagePart = mainDocPart.AddNewPart<ImagePart>("image/jpeg", fileName); ImagePart imagePart = mainDocPart.AddImagePart(ImagePartType.Jpeg); string fileName = @"C:\multigestion\MultiFiscalia\Images\MultiFiscalia\Enviar.png"; //System.Web.HttpContext.Current.Server.MapPath(); using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { imagePart.FeedData(stream); } //var imagePart = doc.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rIdImagePart1"); //GenerateImagePart(imagePart); MemoryStream ms = new MemoryStream(); Header header1 = new Header(); Paragraph paragraph1 = new Paragraph() { }; Run run1 = new Run(); Text text1 = new Text(); text1.Text = "un texto cualquiera"; //run1.Append(ms.ToArray()); run1.Append(text1); paragraph1.Append(run1); header1.Append(paragraph1); headerPart1.Header = header1; SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants <SectionProperties>().FirstOrDefault(); if (sectionProperties1 == null) { sectionProperties1 = new SectionProperties() { }; mainDocPart.Document.Body.Append(sectionProperties1); } HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" }; sectionProperties1.InsertAt(headerReference1, 0); //var element = "imagen"; //mainDocPart.Document.Body.PrependChild(new Paragraph(new Run(element))); }
internal static void GenerateHeaderPartContent(HeaderPart part) { var header = new DocumentFormat.OpenXml.Wordprocessing.Header() { MCAttributes = MarkupCompatibilityAttributes }; Schemas.AddNamespaceDeclarations(header); part.Header = header; }
public static HeaderPartDrawer Create(HeaderPart headerPart, bool drawSelectionRect, bool drawMouseoverRect, [CanBeNull] Texture drawTexture, string tooltip, OnHeaderPartClicked onPartClicked, OnHeaderPartClicked onPartRightClicked = null, bool selectable = true) { HeaderPartDrawer result; if (!Pool.TryGet(out result)) { result = new HeaderPartDrawer(); } result.Setup(headerPart, drawSelectionRect, drawMouseoverRect, drawTexture, tooltip, onPartClicked, onPartRightClicked, selectable); return(result); }
public static HeaderPartDrawer Create(HeaderPart headerPart, bool drawSelectionRect, bool drawMouseoverRect, string tooltip, OnHeaderPartClicked onPartClicked, OnHeaderPartClicked onPartRightClicked = null, bool selectable = true, CalculatePosition overrideCalculatePosition = null) { HeaderPartDrawer result; if (!Pool.TryGet(out result)) { result = new HeaderPartDrawer(); } result.Setup(headerPart, drawSelectionRect, drawMouseoverRect, tooltip, onPartClicked, onPartRightClicked, selectable, overrideCalculatePosition); return(result); }
public static void GenerateHeaderPartContent(HeaderPart part) { Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } }; header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"); header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"); header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office"); header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"); header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"); header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml"); header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"); header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"); header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word"); header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml"); header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"); header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk"); header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml"); header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"); Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" }; ParagraphProperties paragraphProperties1 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" }; paragraphProperties1.Append(paragraphStyleId1); Run run1 = new Run(); Text text1 = new Text(); text1.Text = "Header"; run1.Append(text1); paragraph1.Append(paragraphProperties1); paragraph1.Append(run1); header1.Append(paragraph1); part.Header = header1; }
private void Setup(HeaderPart headerPart, bool drawsSelectionRect, bool drawsMouseoverRect, string tooltip, OnHeaderPartClicked onPartClicked, OnHeaderPartClicked onPartRightClicked, bool isSelectable) { part = headerPart; rect = default(Rect); drawSelectionRect = drawsSelectionRect; drawMouseoverRect = drawsMouseoverRect; onClicked = onPartClicked; onRightClicked = onPartRightClicked; selectable = isSelectable; label.image = null; label.tooltip = tooltip; }
public static string CreateHeader(MainDocumentPart mainPart, OpenXmlCompositeElement element) { HeaderPart headerPart = mainPart.AddNewPart <HeaderPart>(); Header header = new Header(); AddHeaderFooterNamespaceDeclaration(header); if (element != null) { header.AppendChild(element); } headerPart.Header = header; return(mainPart.GetIdOfPart(headerPart)); }
private void Setup(HeaderPart headerPart, bool drawsSelectionRect, bool drawsMouseoverRect, string tooltip, [CanBeNull] OnHeaderPartClicked onPartClicked, [CanBeNull] OnHeaderPartClicked onPartRightClicked, bool isSelectable, CalculatePosition setOverrideCalculatePosition) { part = headerPart; rect = default(Rect); drawSelectionRect = drawsSelectionRect; drawMouseoverRect = drawsMouseoverRect; onClicked = onPartClicked; onRightClicked = onPartRightClicked; selectable = isSelectable; overrideCalculatePosition = setOverrideCalculatePosition; label.image = null; label.tooltip = tooltip == null ? "" : tooltip; }
private void Setup(HeaderPart headerPart, bool drawsSelectionRect, bool drawsMouseoverRect, [CanBeNull] Texture drawTexture, string tooltip, [CanBeNull] OnHeaderPartClicked onPartClicked, [CanBeNull] OnHeaderPartClicked onPartRightClicked, bool isSelectable) { part = headerPart; rect = default(Rect); drawSelectionRect = drawsSelectionRect; drawMouseoverRect = drawsMouseoverRect; texture = drawTexture; onClicked = onPartClicked; onRightClicked = onPartRightClicked; selectable = isSelectable; label.image = drawTexture; label.tooltip = tooltip == null ? "" : tooltip; }
public static void ApplyHeader(WordprocessingDocument doc) { // Get the main document part. MainDocumentPart mainDocPart = doc.MainDocumentPart; HeaderPart headerPart1 = mainDocPart.AddNewPart <HeaderPart>("r97"); Header header1 = new Header(); Paragraph paragraph1 = new Paragraph() { }; Run run1 = new Run(); Text text1 = new Text(); text1.Text = "Header stuff"; run1.Append(text1); paragraph1.Append(run1); header1.Append(paragraph1); headerPart1.Header = header1; SectionProperties sectionProperties1 = mainDocPart.Document.Body.Descendants <SectionProperties>().FirstOrDefault(); if (sectionProperties1 == null) { sectionProperties1 = new SectionProperties() { }; mainDocPart.Document.Body.Append(sectionProperties1); } HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "r97" }; sectionProperties1.InsertAt(headerReference1, 0); }
/// <summary> /// Adds a new header part in the document /// </summary> /// <param name="type">The footer part type</param> /// <returns>A XDocument contaning the added header</returns> public static XDocument AddNewHeader(WordprocessingDocument document, HeaderType type) { // Creates the new header part HeaderPart newHeaderPart = document.MainDocumentPart.AddNewPart <HeaderPart>(); XDocument emptyHeader = CreateEmptyHeaderDocument(); newHeaderPart.PutXDocument(emptyHeader); string newHeaderPartId = document.MainDocumentPart.GetIdOfPart(newHeaderPart); AddHeaderReference(document, type, newHeaderPartId, 0); return(emptyHeader); }
/// <summary> Gets the item responsible for handling the given HeaderPart. </summary> /// <param name="part"> The HeaderPart. </param> /// <returns> The indexed item. </returns> public HeaderPartDrawer this[HeaderPart part] { get { for (int n = parts.Count - 1; n >= 0; n--) { var test = parts[n]; if (test == part) { return(test); } } return(null); } }
/* * 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; } } } }
//..................................................................... /// <summary> /// /// </summary> /// <param name="doc"></param> private static void AddHeader(WordprocessingDocument doc) { // Declare a string for the header text. string newHeaderText = "New header via Open XML Format SDK 2.0 classes"; // Get the main document part. MainDocumentPart mainDocPart = doc.MainDocumentPart; // Delete the existing header parts. mainDocPart.DeleteParts(mainDocPart.HeaderParts); // Create a new header part and get its relationship id. HeaderPart newHeaderPart = mainDocPart.AddNewPart <HeaderPart>( ); string rId = mainDocPart.GetIdOfPart(newHeaderPart); // Call the GeneratePageHeaderPart helper method, passing in // the header text, to create the header markup and then save // that markup to the header part. GeneratePageHeaderPart(newHeaderText).Save(newHeaderPart); // Loop through all section properties in the document // which is where header references are defined. foreach (SectionProperties sectProperties in mainDocPart.Document.Descendants <SectionProperties>( )) { // Delete any existing references to headers. foreach (HeaderReference headerReference in sectProperties.Descendants <HeaderReference>( )) { sectProperties.RemoveChild(headerReference); } // Create a new header reference that points to the new // header part and add it to the section properties. HeaderReference newHeaderReference = new HeaderReference( ) { Id = rId, Type = HeaderFooterValues.Default }; sectProperties.Append(newHeaderReference); } // Save the changes to the main document part. //mainDocPart.Document.Save(); }
public static void ChangeHeader(String documentPath) { // Replace header in target document with header of source document. using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true)) { // Get the main document part MainDocumentPart mainDocumentPart = document.MainDocumentPart; // Delete the existing header and footer parts mainDocumentPart.DeleteParts(mainDocumentPart.HeaderParts); mainDocumentPart.DeleteParts(mainDocumentPart.FooterParts); // Create a new header and footer part HeaderPart headerPart = mainDocumentPart.AddNewPart <HeaderPart>(); FooterPart footerPart = mainDocumentPart.AddNewPart <FooterPart>(); // Get Id of the headerPart and footer parts string headerPartId = mainDocumentPart.GetIdOfPart(headerPart); string footerPartId = mainDocumentPart.GetIdOfPart(footerPart); GenerateHeaderPartContent(headerPart); GenerateFooterPartContent(footerPart); // Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id IEnumerable <SectionProperties> sections = mainDocumentPart.Document.Body.Elements <SectionProperties>(); foreach (var section in sections) { // Delete existing references to headers and footers section.RemoveAllChildren <HeaderReference>(); section.RemoveAllChildren <FooterReference>(); // Create the new header and footer reference node section.PrependChild <HeaderReference>(new HeaderReference() { Id = headerPartId }); section.PrependChild <FooterReference>(new FooterReference() { Id = footerPartId }); } } }
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 static void AddHeaderFromTo(string filepathFrom, string filepathTo) { // Replace header in target document with header of source document. using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(filepathTo, true)) { MainDocumentPart mainPart = wdDoc.MainDocumentPart; // Delete the existing header part. mainPart.DeleteParts(mainPart.HeaderParts); // Create a new header part. HeaderPart headerPart = mainPart.AddNewPart <HeaderPart>(); // Get Id of the headerPart. string rId = mainPart.GetIdOfPart(headerPart); // Feed target headerPart with source headerPart. using (WordprocessingDocument wdDocSource = WordprocessingDocument.Open(filepathFrom, true)) { HeaderPart firstHeader = wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault(); wdDocSource.MainDocumentPart.HeaderParts.FirstOrDefault(); if (firstHeader != null) { headerPart.FeedData(firstHeader.GetStream()); } } // Get SectionProperties and Replace HeaderReference with new Id. IEnumerable <SectionProperties> sectPrs = mainPart.Document.Body.Elements <SectionProperties>(); foreach (var sectPr in sectPrs) { // Delete existing references to headers. sectPr.RemoveAllChildren <HeaderReference>(); // Create the new header reference node. sectPr.PrependChild <HeaderReference>(new HeaderReference() { Id = rId }); } } }
public MemoryStream Generate() { string hierarchicalStructure = ParseComponentToNestedXMLList(ChartableComponents.First(x => x.Parentid == 0)).ToString(); var mem = new MemoryStream(); byte[] byteArray = File.ReadAllBytes("Templates/Organization_Chart_Template.docx"); mem.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(mem, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; HeaderPart headerPart1 = mainPart.AddNewPart <HeaderPart>(); //FooterPart footerPart1 = mainPart.AddNewPart<FooterPart>(); string headerPartId = mainPart.GetIdOfPart(headerPart1); //string footerPartId = mainPart.GetIdOfPart(footerPart1); GenerateHeaderPart1Content(headerPart1); //GenerateFooterPartContent(footerPart1); // Get SectionProperties and Replace HeaderReference and FooterRefernce with new Id IEnumerable <SectionProperties> sections = mainPart.Document.Body.Elements <SectionProperties>(); foreach (var section in sections) { // Delete existing references to headers and footers section.RemoveAllChildren <HeaderReference>(); section.RemoveAllChildren <FooterReference>(); // Create the new header and footer reference node section.PrependChild(new HeaderReference() { Id = headerPartId }); //section.PrependChild(new FooterReference() { Id = footerPartId }); } DiagramDataPart diagram = wordDoc.MainDocumentPart.DiagramDataParts.First(); XmlDocument xmlDoc = new XmlDocument();//load xml xmlDoc.LoadXml(hierarchicalStructure); AddNodesToDiagram("", xmlDoc.ChildNodes[0], diagram.DataModelRoot.PointList, diagram.DataModelRoot.ConnectionList, 0U, true); diagram.DataModelRoot.Save(); wordDoc.MainDocumentPart.Document.Save(); } mem.Seek(0, SeekOrigin.Begin); return(mem); }
/// <summary> /// The custom header consists of three parts: the method, the application ID and the signature. /// This method returns the specified part if it exists. /// </summary> /// <param name="part">The zero indexed part to be returned</param> /// <param name="header">The HTTP header value from which to extract the part</param> /// <returns>The specified part from the header or an empty string if not existent</returns> private static string GetPartFromHeader(HeaderPart headerPart, string header) { int part = (int)headerPart; if (part < 0) { return String.Empty; } if (!String.IsNullOrEmpty(header)) { string[] parts = header.Split(AUTHORIZATION_HEADER_FIELD_SEPARATOR); if (parts.Length > part) { return parts[part]; } } return String.Empty; }