/// <summary> /// replace the current theme with a user specified theme /// </summary> /// <param name="document">document file lcoation</param> /// <param name="themeFile">theme xml file location</param> /// <param name="app">which app is the document</param> public static void ReplaceTheme(string document, string themeFile, string app) { if (app == "Word") { using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; // Delete the old document part. mainPart.DeletePart(mainPart.ThemePart); // Add a new document part and then add content. ThemePart themePart = mainPart.AddNewPart <ThemePart>(); using (StreamReader streamReader = new StreamReader(themeFile)) using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create))) { streamWriter.Write(streamReader.ReadToEnd()); } } } else if (app == "PowerPoint") { using (PresentationDocument presDoc = PresentationDocument.Open(document, true)) { PresentationPart mainPart = presDoc.PresentationPart; mainPart.DeletePart(mainPart.ThemePart); ThemePart themePart = mainPart.AddNewPart <ThemePart>(); using (StreamReader streamReader = new StreamReader(themeFile)) using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create))) { streamWriter.Write(streamReader.ReadToEnd()); } } } else { using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Open(document, true)) { WorkbookPart mainPart = excelDoc.WorkbookPart; mainPart.DeletePart(mainPart.ThemePart); ThemePart themePart = mainPart.AddNewPart <ThemePart>(); using (StreamReader streamReader = new StreamReader(themeFile)) using (StreamWriter streamWriter = new StreamWriter(themePart.GetStream(FileMode.Create))) { streamWriter.Write(streamReader.ReadToEnd()); } } } }
/// <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 static void RemoveComments(string filename) { using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filename, true)) { MainDocumentPart mainPart = myDoc.MainDocumentPart; //Delete the comment part, plus any other part referenced, like image parts mainPart.DeletePart(mainPart.WordprocessingCommentsPart); //Find all elements that are associated with comments IEnumerable <OpenXmlElement> elementList = mainPart.Document.Descendants() .Where(el => el is CommentRangeStart || el is CommentRangeEnd || el is CommentReference); //Delete every found element foreach (OpenXmlElement e in elementList) { e.Remove(); } //Save changes mainPart.Document.Save(); } }