Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed custom colour palettes #76

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion Clippit.Tests/PowerPoint/PtUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
}
}
}

Expand Down
46 changes: 45 additions & 1 deletion Clippit/PowerPoint/PresentationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -128,7 +130,49 @@ private static void ExtractSlide(PresentationDocument srcDoc, int slideNumber, P
throw;
}
}


public static void ModifyPresentationWithCustomColors(string presentationFilePath, IEnumerable<XElement> 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<XElement> colors)
{
var customColors = new List<CustomColor>();
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<XElement> 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<SlideSource> sources, PresentationDocument output)
{
using var fluentBuilder = new FluentPresentationBuilder(output);
Expand Down
Loading