/// <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()); } }
/// <summary> /// Sets the document background color /// </summary> /// <param name="colorValue">String representation of the hexadecimal RGB color</param> public static OpenXmlPowerToolsDocument SetColor(WmlDocument doc, string colorValue) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { XDocument mainDocument = document.MainDocumentPart.GetXDocument(); // If the background element already exists, deletes it XElement backgroundElement = BackgroundElement(document); if (backgroundElement != null) { backgroundElement.Remove(); } mainDocument.Root.AddFirst( new XElement(ns + "background", new XAttribute(ns + "color", colorValue) ) ); // Enables background displaying by adding "displayBackgroundShape" tag if (SettingAccessor.DisplayBackgroundShapeElements(document) == null) { SettingAccessor.AddBackgroundShapeElement(document); } document.MainDocumentPart.PutXDocument(); } return(streamDoc.GetModifiedDocument()); } }
/// <summary> /// Sets the document background image /// </summary> /// <param name="imagePath">Path of the background image</param> public static OpenXmlPowerToolsDocument SetImage(WmlDocument doc, string imagePath) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { XDocument mainDocument = document.MainDocumentPart.GetXDocument(); // Adds the image to the package ImagePart imagePart = document.MainDocumentPart.AddImagePart(ImagePartType.Bmp); Stream imageStream = new StreamReader(imagePath).BaseStream; byte[] imageBytes = new byte[imageStream.Length]; imageStream.Read(imageBytes, 0, imageBytes.Length); imagePart.GetStream().Write(imageBytes, 0, imageBytes.Length); // Creates a "background" element relating the image and the document // If the background element already exists, deletes it XElement backgroundElement = BackgroundElement(document); if (backgroundElement != null) { backgroundElement.Remove(); } // Background element construction mainDocument.Root.Add( new XElement(ns + "background", new XAttribute(ns + "color", defaultBackgroundColor), new XElement(vmlns + "background", new XAttribute(vmlns + "id", defaultVmlBackgroundImageId), new XAttribute(officens + "bwmode", defaultBWMode), new XAttribute(officens + "targetscreensize", defaultTargetScreenSize), new XElement(vmlns + "fill", new XAttribute(relationshipsns + "id", document.MainDocumentPart.GetIdOfPart(imagePart)), new XAttribute("recolor", defaultImageRecolor), new XAttribute("type", defaultImageType) ) ) ) ); // Enables background displaying by adding "displayBackgroundShape" tag if (SettingAccessor.DisplayBackgroundShapeElements(document) == null) { SettingAccessor.AddBackgroundShapeElement(document); } document.MainDocumentPart.PutXDocument(); } return(streamDoc.GetModifiedDocument()); } }
/// <summary> /// Set a new footer in a document /// </summary> /// <param name="footer">XDocument containing the footer to add in the document</param> /// <param name="type">The footer part type</param> public static OpenXmlPowerToolsDocument SetFooter(WmlDocument doc, XDocument footer, FooterType type, int sectionIndex) { using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(doc)) { using (WordprocessingDocument document = streamDoc.GetWordprocessingDocument()) { // Removes the reference in the document.xml and the footer part if those already // exist XElement footerReferenceElement = GetFooterReference(document, type, sectionIndex); if (footerReferenceElement != null) { GetFooterPart(document, type, sectionIndex).RemovePart(); footerReferenceElement.Remove(); } // Add the new footer FooterPart footerPart = document.MainDocumentPart.AddNewPart <FooterPart>(); footerPart.PutXDocument(footer); // If the document does not have a property section a new one must be created if (SectionPropertiesElements(document).Count() == 0) { AddDefaultSectionProperties(document); } // Creates the relationship of the footer inside the section properties in the document string relID = document.MainDocumentPart.GetIdOfPart(footerPart); string kindName = ""; switch ((FooterType)type) { case FooterType.First: kindName = "first"; break; case FooterType.Even: kindName = "even"; break; case FooterType.Default: kindName = "default"; break; } XElement sectionPropertyElement = SectionPropertiesElements(document).Skip(sectionIndex).FirstOrDefault(); if (sectionPropertyElement != null) { sectionPropertyElement.Add( new XElement(ns + "footerReference", new XAttribute(ns + "type", kindName), new XAttribute(relationshipns + "id", relID))); if (sectionPropertyElement.Element(ns + "titlePg") == null) { sectionPropertyElement.Add( new XElement(ns + "titlePg") ); } } document.MainDocumentPart.PutXDocument(); // add in the settings part the EvendAndOddHeaders. this element // allow to see the odd and even footers and headers in the document. SettingAccessor.AddEvenAndOddHeadersElement(document); } return(streamDoc.GetModifiedDocument()); } }