/// <summary> /// Sets the document theme /// </summary> /// <param name="theme">Theme package</param> public void SetTheme(Package theme) { // Gets the theme manager part PackageRelationship themeManagerRelationship = theme.GetRelationshipsByType(mainDocumentRelationshipType).FirstOrDefault(); if (themeManagerRelationship != null) { PackagePart themeManagerPart = theme.GetPart(themeManagerRelationship.TargetUri); // Gets the theme main part PackageRelationship themeRelationship = themeManagerPart.GetRelationshipsByType(themeRelationshipType).FirstOrDefault(); if (themeRelationship != null) { PackagePart themePart = theme.GetPart(themeRelationship.TargetUri); XDocument newThemeDocument = XDocument.Load(XmlReader.Create(themePart.GetStream(FileMode.Open, FileAccess.Read))); // Removes existing theme part from document if (parentDocument.Document.MainDocumentPart.ThemePart != null) { parentDocument.Document.MainDocumentPart.DeletePart(parentDocument.Document.MainDocumentPart.ThemePart); } // Creates a new theme part ThemePart documentThemePart = parentDocument.Document.MainDocumentPart.AddNewPart <ThemePart>(); XDocument themeDocument = parentDocument.GetXDocument(documentThemePart); themeDocument.Add(newThemeDocument.Root); var embeddedItems = themeDocument .Descendants() .Attributes(relationshipns + "embed"); foreach (PackageRelationship imageRelationship in themePart.GetRelationships()) { // Adds an image part to the theme part and stores contents inside PackagePart imagePart = theme.GetPart(imageRelationship.TargetUri); ImagePart newImagePart = documentThemePart.AddImagePart(GetImagePartType(imagePart.ContentType)); newImagePart.FeedData(imagePart.GetStream(FileMode.Open, FileAccess.Read)); // Updates relationship references into the theme XDocument XAttribute relationshipAttribute = embeddedItems.FirstOrDefault(e => e.Value == imageRelationship.Id); if (relationshipAttribute != null) { relationshipAttribute.Value = documentThemePart.GetIdOfPart(newImagePart); } } // Updates the theme part with actual contents of the theme XDocument using (XmlWriter newThemeWriter = XmlWriter.Create(documentThemePart.GetStream(FileMode.Create, FileAccess.Write))) themeDocument.WriteTo(newThemeWriter); } } }
/// <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> /// Transforms a document into a html file /// </summary> /// <param name="packing">Whether save results to a package or not</param> /// <param name="resourcesPackageName">Name of output package</param> /// <param name="htmlOutputName">Name of the output html file</param> /// <param name="outputPath">Path where the files should be placed</param> /// <param name="xslFilePath">Xslt file to use to perform transformation</param> public override void TransformToHtml(bool packing, string resourcesPackageName, string htmlOutputName, string outputPath, string xslFilePath, XsltArgumentList arguments = null) { try { Package htmlPackage = null; StreamWriter htmlWriter = null; /* * // Creates an xmlReader to get the xml of the main document part * XmlReader xmlReader = * XmlReader.Create(parentDocument.Document.MainDocumentPart.GetStream(FileMode.Open, FileAccess.Read)); */ // Load document and important related parts, like the styles XmlDocument mainDoc = new XmlDocument(); mainDoc.Load(parentDocument.Document.MainDocumentPart.GetStream()); XmlNamespaceManager nsm = createNameSpaceManager(mainDoc.NameTable); XmlNode docNode = mainDoc.SelectSingleNode("./w:document", nsm); StyleDefinitionsPart styles = parentDocument.Document.MainDocumentPart.StyleDefinitionsPart; if (styles != null) { LoadRelatedPart(mainDoc, docNode, styles.GetStream()); } NumberingDefinitionsPart numbering = parentDocument.Document.MainDocumentPart.NumberingDefinitionsPart; if (numbering != null) { LoadRelatedPart(mainDoc, docNode, numbering.GetStream()); } ThemePart theme = parentDocument.Document.MainDocumentPart.ThemePart; if (theme != null) { LoadRelatedPart(mainDoc, docNode, theme.GetStream()); } FontTablePart fontTable = parentDocument.Document.MainDocumentPart.FontTablePart; if (fontTable != null) { LoadRelatedPart(mainDoc, docNode, fontTable.GetStream()); } if (packing) { // New package that will contain the html file, with the images htmlPackage = Package.Open(outputPath + @"\" + resourcesPackageName, FileMode.Create); } else { // Create the directory where images will be stored Directory.CreateDirectory(outputPath + @"\images\"); } HandleNumberedLists(mainDoc, nsm); HandleImages(mainDoc, nsm, packing, outputPath, htmlPackage); HandleLinks(mainDoc, nsm); XslCompiledTransform OpenXmlTransformer = new XslCompiledTransform(); OpenXmlTransformer.Load(xslFilePath); // The Transform method apply the xslt transformation to convert the XML to HTML StringWriter strWriterHtml = new StringWriter(); // Manage the mapping of paragraphs with specific style templates to elements OpenXmlTransformer.Transform(mainDoc, arguments, strWriterHtml); string strHtml = strWriterHtml.ToString(); // Closes the package if created if (packing) { // Finally, creates the html file inside the html package Uri uri = null; if (htmlOutputName == string.Empty) { uri = new Uri("/inputFileName.html", UriKind.Relative); } else { uri = new Uri("/" + htmlOutputName, UriKind.Relative); } PackagePart htmlPart = htmlPackage.CreatePart(uri, "text/html"); htmlWriter = new StreamWriter(htmlPart.GetStream()); htmlWriter.Write(strHtml); htmlWriter.Close(); htmlPackage.Close(); } else { // Writes the html file htmlWriter = File.CreateText(outputPath + @"\" + htmlOutputName); htmlWriter.Write(strHtml); htmlWriter.Close(); } } catch (XsltCompileException) { throw new Exception("Invalid XSLT"); } catch (XsltException) { throw new Exception("Invalid XSLT"); } }