Theme’s font does not work when custom font style is set as “linked” using .NET

Hi,

Is there a way to specify a Font Style as “linked” to the Theme’s Font, so that if I later change the Theme (e.g. manually in MS Word), the style’s font is updated.

In the following image, the Heading 1 font is “Metropolis (Headings)”, not just “Metropolis”. It is linked to the Theme.

This code only copies the font, not keeps it linked.

var themeHeadingsFont =_builder.Document.Theme.MajorFonts.Latin;
var style = styles.Add(StyleType.Paragraph, "Custom Style");
style.Font.Name = themeHeadingsFont;

Similar question for the Colors. I’d like to be able to keep the link to the Theme’s Accent1, Accent2 colors so if that changes, than the whole document also gets updated.

Is that feasible?

Thanks.

@dstj

Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "Theme colors.docx");

// This object gives us access to the document theme, which is a source of default fonts and colors
Theme theme = doc.Theme;

// These fonts will be inherited by some styles like "Heading 1" and "Subtitle"
theme.MajorFonts.Latin = "Courier New";
theme.MinorFonts.Latin = "Agency FB";

Assert.AreEqual(string.Empty, theme.MajorFonts.ComplexScript);
Assert.AreEqual(string.Empty, theme.MajorFonts.EastAsian);
Assert.AreEqual(string.Empty, theme.MinorFonts.ComplexScript);
Assert.AreEqual(string.Empty, theme.MinorFonts.EastAsian);

// This collection of colors corresponds to the color palette from Microsoft Word which appears when changing shading or font color 
ThemeColors colors = theme.Colors;

// We will set the color of each color palette column going from left to right like this
colors.Dark1 = Color.MidnightBlue;
colors.Light1 = Color.PaleGreen;
colors.Dark2 = Color.Indigo;
colors.Light2 = Color.Khaki;

colors.Accent1 = Color.OrangeRed;
colors.Accent2 = Color.LightSalmon;
colors.Accent3 = Color.Yellow;
colors.Accent4 = Color.Gold;
colors.Accent5 = Color.BlueViolet;
colors.Accent6 = Color.DarkViolet;

// We can also set colors for hyperlinks like this
colors.Hyperlink = Color.Black;
colors.FollowedHyperlink = Color.Gray;

doc.Save(ArtifactsDir + "Themes.CustomColorsAndFonts.docx");

If you still face problem, please ZIP and attach your input and expected output documents here for our reference. We will then provide you more information on it.

@tahir.manzoor

You copy-pasted the code from here: Theme Class | Aspose.Words for .NET

But this is not what I’m asking.

As the sample code you pasted mentions // These fonts will be inherited by some styles like "Heading 1" and "Subtitle". What I want is to create a new style that will inherit from the theme fonts.

Here’s a sample program and video showing you what I mean
AsposeThemeFontStyle.zip (1.4 MB)

1 Like

@dstj

We are working over your query and will get back to you soon.

@dstj

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-21183 . You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

A post was split to a new topic: Set custom font style as “linked” to the Theme’s font does not work

The issues you have found earlier (filed as WORDSNET-21183) have been fixed in this Aspose.Words for .NET 21.2 update and this Aspose.Words for Java 21.2 update.