From ad4f7ce2c339e77a3e57798b418bc38df48429d0 Mon Sep 17 00:00:00 2001 From: krystyna_hurieieva Date: Tue, 7 May 2024 11:53:32 +0300 Subject: [PATCH] Embed custom colour palettes --- Clippit.Tests/PowerPoint/PtUtilTests.cs | 21 ++++++++++- Clippit/PowerPoint/PresentationBuilder.cs | 46 ++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Clippit.Tests/PowerPoint/PtUtilTests.cs b/Clippit.Tests/PowerPoint/PtUtilTests.cs index 084d2232..fcac6337 100644 --- a/Clippit.Tests/PowerPoint/PtUtilTests.cs +++ b/Clippit.Tests/PowerPoint/PtUtilTests.cs @@ -2,6 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.IO; +using System.Linq; +using Clippit.PowerPoint; +using DocumentFormat.OpenXml.Packaging; using Xunit; #if !ELIDE_XUNIT_TESTS @@ -10,6 +13,10 @@ namespace Clippit.Tests.PowerPoint { public class PtUtilTests { + public static string SourceDirectory = "../../../../TestFiles/PublishSlides/"; + public static string TargetDirectory = "../../../../TestFiles/PublishSlides/output"; + private static string customColorsFilePath = "../../../../TestFiles/PublishSlides/custom_colors.xml"; + [Theory] [InlineData("PU/PU001-Test001.mht")] public void PU001(string name) @@ -22,8 +29,20 @@ public void PU001(string name) Assert.True(p.MimeVersion != null); Assert.True(p.Parts.Length != 0); Assert.DoesNotContain(p.Parts, part => part.ContentType == null || part.ContentLocation == null); - } + } + + [Theory] + [InlineData("EPAM_AccountStory.pptx")] + public void AddCustomColorsToPresentation(string fileName) + { + var file = Path.Combine(SourceDirectory, fileName); + var document = new PmlDocument(file); + + // Load custom colors from XML file + var customColors = PresentationBuilder.LoadCustomColors(customColorsFilePath); + PresentationBuilder.ModifyPresentationWithCustomColors(file, customColors); + } } } diff --git a/Clippit/PowerPoint/PresentationBuilder.cs b/Clippit/PowerPoint/PresentationBuilder.cs index dca2f8a7..89bd8ef6 100644 --- a/Clippit/PowerPoint/PresentationBuilder.cs +++ b/Clippit/PowerPoint/PresentationBuilder.cs @@ -5,6 +5,8 @@ using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using System.Xml.Linq; +using DocumentFormat.OpenXml.Drawing; using DocumentFormat.OpenXml.Packaging; namespace Clippit.PowerPoint @@ -128,7 +130,49 @@ private static void ExtractSlide(PresentationDocument srcDoc, int slideNumber, P throw; } } - + + public static void ModifyPresentationWithCustomColors(string presentationFilePath, IEnumerable customColors) + { + using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFilePath, true)) + { + var presentationPart = presentationDocument.PresentationPart; + var slideMasterPart = presentationPart.SlideMasterParts.First(); + var themePart = slideMasterPart.ThemePart; + var theme = themePart.Theme; + ModifyCustomColors(theme, customColors); + + // Save the changes + themePart.Theme.Save(); + } + } + public static void ModifyCustomColors(Theme theme, IEnumerable colors) + { + var customColors = new List(); + XNamespace ns = "http://schemas.openxmlformats.org/drawingml/2006/main"; + foreach (var color in colors) + { + customColors.Add(new CustomColor( + new RgbColorModelHex() + { + Val = color.Element(ns + "srgbClr") + .Attribute("val").Value + } + ) + { + Name = color.Attribute("name").Value + }); + } + theme.CustomColorList = new CustomColorList(customColors); + } + + public static IEnumerable LoadCustomColors(string filePath) + { + var doc = XDocument.Load(filePath); + XNamespace a = "http://schemas.openxmlformats.org/drawingml/2006/main"; + + return doc.Root.Elements(A.custClrLst).Elements(A.custClr); + } + private static void BuildPresentation(List sources, PresentationDocument output) { using var fluentBuilder = new FluentPresentationBuilder(output);