在 .NET 中管理演示文稿主题

介绍

演示文稿主题定义了一组协调的颜色、字体、背景样式、填充、线条和效果。支持主题的对象引用这些共享定义,而不是将每个视觉属性存储为固定值,因此更改主题可以一次性更新许多对象。

在 Aspose.Slides 中,演示文稿级别的主题可通过 Presentation.MasterTheme 属性访问。演示文稿还可以在更低层级包含主题覆盖。母版可以通过 MasterThemeManager.OverrideTheme 覆盖演示文稿主题,布局可以通过 BaseOverrideThemeManager.OverrideTheme 覆盖其继承的主题,单个幻灯片也可以如此操作。实际上,幻灯片的有效主题通过以下继承链解析:演示文稿主题 → 母版覆盖 → 布局覆盖 → 幻灯片覆盖。

主题组成:颜色、字体、背景样式和效果

下面的章节展示了最常见的主题工作流:检查主题、修改颜色和字体、复制或应用主题、更新背景和效果样式,以及在继承和覆盖解析后读取有效值。

检查主题

MasterTheme 对象公开主题的 ColorSchemeFontSchemeFormatScheme 。在修改这些集合之前先检查它们尤为重要,因为当演示文稿来自外部来源时,样式条目的数量和内容可能会有所不同。

以下示例读取主要主题属性并报告主题中存储了多少背景、填充、线条和效果样式:

using System;
using Aspose.Slides;

using var presentation = new Presentation("input.pptx");
var theme = presentation.MasterTheme;

Console.WriteLine($"Theme name: {theme.Name}");
Console.WriteLine($"Accent 1: {theme.ColorScheme.Accent1.Color}");
Console.WriteLine($"Major Latin font: {theme.FontScheme.Major.LatinFont.FontName}");
Console.WriteLine($"Minor Latin font: {theme.FontScheme.Minor.LatinFont.FontName}");
Console.WriteLine($"Background fill styles: {theme.FormatScheme.BackgroundFillStyles.Count}");
Console.WriteLine($"Fill styles: {theme.FormatScheme.FillStyles.Count}");
Console.WriteLine($"Line styles: {theme.FormatScheme.LineStyles.Count}");
Console.WriteLine($"Effect styles: {theme.FormatScheme.EffectStyles.Count}");

如果文件使用了多个母版,请不要假设每个幻灯片具有相同的有效主题。检查与幻灯片关联的母版,并在可能存在布局或幻灯片覆盖的情况下使用本文后面展示的有效主题工作流。

更改主题颜色

支持主题的填充、线条和文本可以引用 SchemeColor 枚举中的逻辑颜色。当您在主题的 IColorScheme 中更改相应条目时,所有仍然引用该主题颜色的对象都会使用新值进行解析。直接使用 RGB 颜色的对象不会受到主题颜色更新的影响。

下面的端到端示例创建一个使用 Accent4 的形状,将主题的 Accent4 颜色改为红色,保存演示文稿,重新打开并输出有效填充颜色:

using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 100, 100);
shape.FillFormat.FillType = FillType.Solid;
shape.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
presentation.MasterTheme.ColorScheme.Accent4.Color = Color.Red;
presentation.Save("theme-color.pptx", SaveFormat.Pptx);

using var savedPresentation = new Presentation("theme-color.pptx");
var savedSlide = savedPresentation.Slides[0];
var savedShape = savedSlide.Shapes[0];
var effectiveFill = savedShape.FillFormat.GetEffective();
Console.WriteLine($"Effective fill color: {effectiveFill.SolidFillColor}");

因为矩形仍然链接到 Accent4,主题更改后其可见颜色变为红色。如果您在形状上用直接颜色替换了方案颜色,随后对 Accent4 的更改将不再影响该填充。

使用附加调色板中的颜色

PowerPoint 通过对主题颜色应用颜色变换来生成更亮和更暗的变体。Aspose.Slides 通过 ColorTransformOperation 暴露这些变换。

主主题颜色以及从附加调色板生成的更亮和更暗颜色

1 - 主主题颜色。

2 - 由主主题颜色生成的更亮和更暗变体。

下面的示例基于 Accent4 创建六个矩形,对其中五个应用亮度变换并保存结果:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var shape1 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 10, 50, 50);
shape1.FillFormat.FillType = FillType.Solid;
shape1.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;

var shape2 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 70, 50, 50);
shape2.FillFormat.FillType = FillType.Solid;
shape2.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
shape2.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.MultiplyLuminance, 0.2f);
shape2.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.AddLuminance, 0.8f);

var shape3 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 130, 50, 50);
shape3.FillFormat.FillType = FillType.Solid;
shape3.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
shape3.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.MultiplyLuminance, 0.4f);
shape3.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.AddLuminance, 0.6f);

var shape4 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 190, 50, 50);
shape4.FillFormat.FillType = FillType.Solid;
shape4.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
shape4.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.MultiplyLuminance, 0.6f);
shape4.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.AddLuminance, 0.4f);

var shape5 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 250, 50, 50);
shape5.FillFormat.FillType = FillType.Solid;
shape5.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
shape5.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.MultiplyLuminance, 0.75f);

var shape6 = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 10, 310, 50, 50);
shape6.FillFormat.FillType = FillType.Solid;
shape6.FillFormat.SolidFillColor.SchemeColor = SchemeColor.Accent4;
shape6.FillFormat.SolidFillColor.ColorTransform.Add(ColorTransformOperation.MultiplyLuminance, 0.5f);

presentation.Save("theme-color-palette.pptx", SaveFormat.Pptx);

这些变体仍然基于主题颜色。如果后续 Accent4 更改,变换后的颜色会根据新的 Accent4 值重新计算。

SchemeColor 值映射到 IColorScheme 槽位

SchemeColor 枚举使用 Text1Background1Text2Background2,而 IColorScheme 将相同的主题槽位公开为 Dark1Light1Dark2Light2。映射是固定的:

  • Text1 = Dark1
  • Background1 = Light1
  • Text2 = Dark2
  • Background2 = Light2

这些是同一主题槽位的别名;它们不是会在两种形式之间动态转换的值。

更改主题字体

主题字体方案包含标题的主要字体集和正文的次要字体集。[FontScheme.Major](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/fontscheme/major/)[FontScheme.Minor](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/fontscheme/minor/) 属性公开这些集合。

PowerPoint 兼容的主题字体标识符可用于文本格式化:

  • +mn-lt - 正文字体 Latin(次要 Latin 字体)
  • +mj-lt - 标题字体 Latin(主要 Latin 字体)
  • +mn-ea - 正文字体 East Asian(次要东亚字体)
  • +mj-ea - 标题字体 East Asian(主要东亚字体)

下面的示例创建一个使用主要 Latin 主题字体的标题和一个使用次要 Latin 主题字体的正文行,然后更改主题字体并保存结果:

using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var heading = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 40, 500, 60);
heading.TextFrame.Text = "Theme heading";
heading.TextFrame.Paragraphs[0].Portions[0].PortionFormat.LatinFont = new FontData("+mj-lt");

var body = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 40, 120, 500, 60);
body.TextFrame.Text = "Theme body text";
body.TextFrame.Paragraphs[0].Portions[0].PortionFormat.LatinFont = new FontData("+mn-lt");

presentation.MasterTheme.FontScheme.Major.LatinFont = new FontData("Aptos Display");
presentation.MasterTheme.FontScheme.Minor.LatinFont = new FontData("Arial");

presentation.Save("theme-fonts.pptx", SaveFormat.Pptx);

标题遵循主要字体,正文遵循次要字体。使用显式字体名称而非主题标识符的文本在主题字体方案更改时不会自动切换。

主要和次要字体集合还可以包含针对特定书写系统的字体映射,例如西里尔文、阿拉伯文、日文、格鲁吉亚文和塔纳文。要检查、添加、替换或删除这些映射,请参阅 脚本特定主题字体

复制或应用主题

以下工作流解决不同的主题相关问题。

将外部主题应用于依赖于某个母版的幻灯片

当您拥有 PowerPoint 主题文件(.thmx)并希望重新设置所有依赖于特定母版的幻灯片时,使用 IMasterSlide.ApplyExternalThemeToDependingSlides 。从 Presentation.Masters 集合中选择母版,该集合实现了 IMasterSlideCollection,并将主题文件路径传递给该方法。

该方法执行以下操作:

  1. 基于选定的母版创建一个新母版幻灯片。
  2. 将外部主题应用于新母版。
  3. 将新母版分配给先前依赖于选定母版的所有幻灯片。
  4. 返回新创建的 IMasterSlide

以下示例将外部主题应用于依赖于第一个母版的幻灯片,保存演示文稿并重新打开结果:

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("presentation.pptx");
var selectedMaster = presentation.Masters[0];
var themedMaster = selectedMaster.ApplyExternalThemeToDependingSlides("corporate-theme.thmx");

Console.WriteLine($"Created master: {themedMaster.Name}");
presentation.Save("presentation-with-external-theme.pptx", SaveFormat.Pptx);

无效、损坏或不受支持的主题可能导致 PptxException 或其格式相关子类。请验证用户提供的路径,处理文件系统访问失败,并仅在主题成功应用后保存演示文稿。

仅重新分配依赖于选定母版的幻灯片。关联其他母版的幻灯片保留其现有母版和主题。支持主题的颜色、字体、填充、线条、背景和效果会根据外部主题解析。直接分配的颜色、字体、填充和其他显式格式可能保持不变。布局级和幻灯片级的覆盖也可能优先于从新母版继承的值。

主题可能引用在运行时环境中不存在的字体。为确保一致的渲染和导出,请安装所需字体、通过 自定义字体源 提供,或配置 字体替换

这是一种直接的母版级工作流:方法接受 .thmx 文件的路径,不需要手动创建幻灯片级或布局级主题覆盖。

在多母版演示文稿中为不同母版应用不同外部主题

当事先不知道相关母版时,可通过 ISlide.LayoutSlideILayoutSlide.MasterSlide 从代表性幻灯片获取母版。应用任何主题之前,请先保存原始母版引用,因为每次调用都会在演示文稿中创建另一个母版。

以下示例使用来自两个章节的幻灯片定位它们的母版,并为每组应用不同的外部主题:

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("multi-master-presentation.pptx");

if (presentation.Slides.Count < 5)
{
    Console.WriteLine("The presentation does not contain the expected representative slides.");
}
else
{
    var firstGroupMaster = presentation.Slides[0].LayoutSlide.MasterSlide;
    var secondGroupMaster = presentation.Slides[4].LayoutSlide.MasterSlide;

    if (ReferenceEquals(firstGroupMaster, secondGroupMaster))
    {
        Console.WriteLine("The representative slides use the same master.");
    }
    else
    {
        var firstThemedMaster = firstGroupMaster.ApplyExternalThemeToDependingSlides("blue-theme.thmx");
        var secondThemedMaster = secondGroupMaster.ApplyExternalThemeToDependingSlides("green-theme.thmx");

        Console.WriteLine($"First themed master: {firstThemedMaster.Name}");
        Console.WriteLine($"Second themed master: {secondThemedMaster.Name}");
        presentation.Save("multi-master-with-external-themes.pptx", SaveFormat.Pptx);
    }
}

第一次调用仅影响依赖于 firstGroupMaster 的幻灯片,第二次调用仅影响依赖于 secondGroupMaster 的幻灯片。属于其他母版的幻灯片不会被重新设样式。

移动幻灯片时保留源主题

如果您想将幻灯片移动到另一演示文稿并保留其原始设计,可使用 IMasterSlideCollection.AddClone 将源母版克隆到目标演示文稿,然后使用 ISlideCollection.AddClone 连同克隆的母版一起克隆幻灯片。这会将母版、其布局以及关联的主题一起携带。

using Aspose.Slides;
using Aspose.Slides.Export;

using var source = new Presentation("source-theme.pptx");
using var target = new Presentation("target.pptx");

var sourceSlide = source.Slides[0];
var sourceMaster = sourceSlide.LayoutSlide.MasterSlide;
var clonedMaster = target.Masters.AddClone(sourceMaster);
target.Slides.AddClone(sourceSlide, clonedMaster, true);

target.Save("theme-preserved.pptx", SaveFormat.Pptx);

当源幻灯片必须在目标中保持相同外观时,这是首选工作流。仅将内容克隆到不相关的目标母版上可能会改变受主题驱动的颜色、字体、背景和效果。

为现有幻灯片应用主题值

如果目标幻灯片必须保持其当前母版和布局,可从源主题初始化幻灯片级覆盖。[OverrideTheme.InitColorSchemeFrom](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/overridetheme/initcolorschemefrom/)[OverrideTheme.InitFontSchemeFrom](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/overridetheme/initfontschemefrom/)[OverrideTheme.InitFormatSchemeFrom](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/overridetheme/initformatschemefrom/) 方法会将三个主要主题组件复制到覆盖中。

using Aspose.Slides;
using Aspose.Slides.Export;

using var source = new Presentation("source-theme.pptx");
using var target = new Presentation("target.pptx");

var targetSlide = target.Slides[0];
var overrideTheme = targetSlide.ThemeManager.OverrideTheme;
overrideTheme.InitColorSchemeFrom(source.MasterTheme.ColorScheme);
overrideTheme.InitFontSchemeFrom(source.MasterTheme.FontScheme);
overrideTheme.InitFormatSchemeFrom(source.MasterTheme.FormatScheme);

target.Save("theme-applied-to-slide.pptx", SaveFormat.Pptx);

这会更改该幻灯片使用的主题,而不影响其他幻灯片继承的主题。要移除本地覆盖并恢复继承值,请调用 [OverrideTheme.Clear](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/overridetheme/clear/)

为布局应用主题覆盖

布局级覆盖适用于使用该布局的所有幻灯片,除非特定幻灯片有自己的覆盖。相同的初始化方法可通过布局的 [LayoutSlideThemeManager](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/layoutslidethememanager/) 使用:

using Aspose.Slides;
using Aspose.Slides.Export;

using var source = new Presentation("source-theme.pptx");
using var target = new Presentation("target.pptx");

var targetLayout = target.Slides[0].LayoutSlide;
var overrideTheme = targetLayout.ThemeManager.OverrideTheme;
overrideTheme.InitColorSchemeFrom(source.MasterTheme.ColorScheme);
overrideTheme.InitFontSchemeFrom(source.MasterTheme.FontScheme);
overrideTheme.InitFormatSchemeFrom(source.MasterTheme.FormatScheme);

target.Save("theme-applied-to-layout.pptx", SaveFormat.Pptx);

当许多布局和幻灯片需要共享相同的基础设计时,请使用母版或演示文稿级主题;当某个布局系列需要不同样式时使用布局覆盖;仅在真正例外时使用幻灯片覆盖。过多的幻灯片级覆盖会使后续全局主题更改难以预测。

更新主题背景样式

主题的背景填充存储在 [FormatScheme.BackgroundFillStyles](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/formatscheme/backgroundfillstyles/) 中。PowerPoint 的 UI 能够呈现比该集合实际存储的填充定义更多的背景选项,因为 UI 可以将主题填充与主题颜色及其他样式引用组合使用。

PowerPoint 演示文稿主题的背景样式库

在使用背景样式之前,请检查存储的集合以及当前的 [Background.StyleIndex](https://reference.aspose.com/slides/zh/net/aspose.slides/background/styleindex/)StyleIndex0 表示无主题填充;正数表示主题背景样式引用。这不同于直接对 .NET 集合进行索引时 [0] 代表第一项。不要假设每个演示文稿都包含相同数量的背景填充样式。

以下示例报告可用的背景填充计数,将主题背景引用分配给第一个母版,并保存演示文稿:

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("input.pptx");
var backgroundStyles = presentation.MasterTheme.FormatScheme.BackgroundFillStyles;
Console.WriteLine($"Background fill styles: {backgroundStyles.Count}");

if (backgroundStyles.Count == 0)
{
    throw new InvalidOperationException("The presentation theme does not contain background fill styles.");
}

presentation.Masters[0].Background.Type = BackgroundType.Themed;
presentation.Masters[0].Background.StyleIndex = 1;

presentation.Save("theme-background.pptx", SaveFormat.Pptx);

可见结果取决于母版引用的主题条目以及布局或幻灯片级的任何背景覆盖。如果幻灯片使用了自己的背景,仅更改母版背景可能不会影响该幻灯片。需要了解继承后最终背景时,请使用 [Background.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/background/geteffective/)

更新主题效果

主题格式方案包含独立的 [FillStyles](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/formatscheme/fillstyles/)[LineStyles](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/formatscheme/linestyles/)[EffectStyles](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/formatscheme/effectstyles/) 集合。典型的 Office 主题通常包含三个主要样式条目,分别对应细腻、适中和强烈的视觉效果,但代码应检查每个集合,而不是假设固定数量。

对同一形状应用细腻、适中和强烈主题效果

在 C# 中访问这些集合时,集合索引是零基的:[0] 为第一项,[2] 为第三项。形状的样式引用索引是另一概念,通过 [IShapeStyle](https://reference.aspose.com/slides/zh/net/aspose.slides/ishapestyle/) 暴露。修改主题样式会影响引用该主题样式的形状;直接格式化的形状可能保持不变。

以下示例检查所需的样式条目是否存在,修改第一条线条样式,修改第三条填充样式,在第三条效果样式中启用外阴影,并保存结果:

using System;
using System.Drawing;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("Subtle_Moderate_Intense.pptx");
var formatScheme = presentation.MasterTheme.FormatScheme;

if (formatScheme.LineStyles.Count < 1 || formatScheme.FillStyles.Count < 3 || formatScheme.EffectStyles.Count < 3)
{
    throw new InvalidOperationException("The theme does not contain the style entries required by this example.");
}

formatScheme.LineStyles[0].FillFormat.FillType = FillType.Solid;
formatScheme.LineStyles[0].FillFormat.SolidFillColor.Color = Color.Red;
formatScheme.FillStyles[2].FillType = FillType.Solid;
formatScheme.FillStyles[2].SolidFillColor.Color = Color.ForestGreen;
formatScheme.EffectStyles[2].EffectFormat.EnableOuterShadowEffect();
formatScheme.EffectStyles[2].EffectFormat.OuterShadowEffect.Distance = 10f;

presentation.Save("theme-effects.pptx", SaveFormat.Pptx);

对于引用这些槽位的形状,第一条主题线条样式变为红色,第三条主题填充样式变为实心森林绿,第三条效果样式获得距离为 10 点的外阴影。具体视觉结果仍取决于每个形状引用的样式槽位以及是否存在直接格式化覆盖。

更改线条、填充和阴影设置后主题效果样式

确定有效实心填充是否使用主题颜色

填充可以直接存储在对象上,也可以从段落、布局、母版、主题样式或其他格式层级继承。调用 [IFillFormat.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/ifillformat/geteffective/) 可将该层级解析为不可变的 [IFillFormatEffectiveData](https://reference.aspose.com/slides/zh/net/aspose.slides/ifillformateffectivedata/)。首先检查 [IFillFormatEffectiveData.FillType](https://reference.aspose.com/slides/zh/net/aspose.slides/ifillformateffectivedata/filltype/)。仅当其为 FillType.Solid 时才读取实心填充属性。

对于实心填充,[IFillFormatEffectiveData.SolidFillColor](https://reference.aspose.com/slides/zh/net/aspose.slides/ifillformateffectivedata/solidfillcolor/) 返回在继承、主题查找和颜色变换后渲染的最终 RGB 值。[IFillFormatEffectiveData.SolidFillSchemeColor](https://reference.aspose.com/slides/zh/net/aspose.slides/ifillformateffectivedata/solidfillschemecolor/) 返回对应的逻辑 [SchemeColor](https://reference.aspose.com/slides/zh/net/aspose.slides/schemecolor/) 槽位,例如 Text1Accent6。如果返回 SchemeColor.NotDefined,则表示有效实心填充不基于方案颜色。在仅有主题颜色或直接 RGB 颜色的工作流中,该值标识为直接 RGB 填充。

不要仅使用本地 [IColorFormat.SchemeColor](https://reference.aspose.com/slides/zh/net/aspose.slides/icolorformat/schemecolor/) 值来分类填充。例如,文本段落可能本地未定义方案颜色,其本地值为 NotDefined,但其有效填充继承自主题颜色并解析为 Text1Accent6。相反,SolidFillSchemeColor 告诉您是哪一个逻辑主题槽产生了有效颜色,但并不说明该槽来自对象、段落、布局、母版或其他层级。

以下示例加载演示文稿,审计形状填充和文本段落填充,打印每个最终 RGB 值及其关联的方案颜色,并标记不会随主题颜色变化的实心填充:

using System;
using Aspose.Slides;

using var presentation = new Presentation("input.pptx");

var slideCount = presentation.Slides.Count;
for (var slideIndex = 0; slideIndex < slideCount; slideIndex++)
{
    var slide = presentation.Slides[slideIndex];

    var shapeCount = slide.Shapes.Count;
    for (var shapeIndex = 0; shapeIndex < shapeCount; shapeIndex++)
    {
        var shape = slide.Shapes[shapeIndex];
        var shapeName = $"Slide {slideIndex + 1}, shape {shapeIndex + 1}";
        AuditFill(shapeName, shape.FillFormat);

        if (shape is IAutoShape autoShape)
        {
            var paragraphCount = autoShape.TextFrame.Paragraphs.Count;
            for (var paragraphIndex = 0; paragraphIndex < paragraphCount; paragraphIndex++)
            {
                var paragraph = autoShape.TextFrame.Paragraphs[paragraphIndex];

                var portionCount = paragraph.Portions.Count;
                for (var portionIndex = 0; portionIndex < portionCount; portionIndex++)
                {
                    var portion = paragraph.Portions[portionIndex];
                    var portionName = $"{shapeName}, paragraph {paragraphIndex + 1}, portion {portionIndex + 1}";
                    AuditFill(portionName, portion.PortionFormat.FillFormat);
                }
            }
        }
    }
}

static void AuditFill(string objectName, IFillFormat localFill)
{
    var effectiveFill = localFill.GetEffective();

    if (effectiveFill.FillType != FillType.Solid)
    {
        Console.WriteLine($"{objectName}: fill type = {effectiveFill.FillType}; not a solid fill.");
        return;
    }

    var rgb = effectiveFill.SolidFillColor;
    var effectiveSchemeColor = effectiveFill.SolidFillSchemeColor;
    var localSchemeColor = localFill.SolidFillColor.SchemeColor;

    Console.WriteLine($"{objectName}: RGB = #{rgb.R:X2}{rgb.G:X2}{rgb.B:X2}");
    Console.WriteLine($"{objectName}: local scheme = {localSchemeColor}, effective scheme = {effectiveSchemeColor}");

    if (effectiveSchemeColor == SchemeColor.NotDefined)
    {
        Console.WriteLine($"{objectName}: direct RGB or another non-scheme fill; audit as theme-independent.");
    }
    else
    {
        Console.WriteLine($"{objectName}: theme-dependent through {effectiveSchemeColor}.");
    }
}

NotDefined 分支提供了一份审计列表,列出不会响应主题颜色槽位更改的实心填充。在演示文稿必须遵循新品牌调色板时,请检查这些对象。报告的 RGB 值仍显示当前外观,而方案值说明该外观是否与主题关联。

有效格式对象是快照。更改演示文稿主题、主题覆盖或任何继承的格式后,再次调用 GetEffective 并读取新的 IFillFormatEffectiveData 对象后再进行比较或报告颜色。

读取有效主题值

原始主题对象告诉您在特定层级定义了什么。有效值告诉您幻灯片或形状在继承和本地覆盖解析后实际使用的内容。对于幻灯片,调用 [BaseOverrideThemeManager.CreateThemeEffective](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/baseoverridethememanager/createthemeeffective/)。对于背景,使用 [Background.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/background/geteffective/);对于填充,使用 [FillFormat.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/fillformat/geteffective/)

以下示例读取幻灯片的有效主题、背景以及第一形状的填充:

using System;
using Aspose.Slides;

using var presentation = new Presentation("input.pptx");
var slide = presentation.Slides[0];
var effectiveTheme = slide.ThemeManager.CreateThemeEffective();
var effectiveBackground = slide.Background.GetEffective();

Console.WriteLine($"Effective major Latin font: {effectiveTheme.FontScheme.Major.LatinFont.FontName}");
Console.WriteLine($"Effective minor Latin font: {effectiveTheme.FontScheme.Minor.LatinFont.FontName}");
Console.WriteLine($"Effective background fill type: {effectiveBackground.FillFormat.FillType}");

if (slide.Shapes.Count > 0)
{
    var effectiveFill = slide.Shapes[0].FillFormat.GetEffective();
    Console.WriteLine($"First shape effective fill type: {effectiveFill.FillType}");
    if (effectiveFill.FillType == FillType.Solid)
    {
        Console.WriteLine($"First shape effective fill color: {effectiveFill.SolidFillColor}");
    }
}

使用有效数据进行渲染诊断、验证和比较。如果仅检查 [Presentation.MasterTheme](https://reference.aspose.com/slides/zh/net/aspose.slides/presentation/mastertheme/),可能会遗漏改变最终外观的母版、布局、幻灯片或形状覆盖。

常见问题

将外部主题应用于演示文稿会影响所有幻灯片吗?

不会。[IMasterSlide.ApplyExternalThemeToDependingSlides](https://reference.aspose.com/slides/zh/net/aspose.slides/imasterslide/applyexternalthemetodependingslides/) 只重新分配依赖于所选母版的幻灯片。使用其他母版的幻灯片保留其现有主题。

我可以在不更改母版的情况下仅对单个幻灯片应用主题吗?

可以。使用幻灯片的 [SlideThemeManager](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/slidethememanager/) 并初始化其覆盖主题。更改仅局限于该幻灯片,其他幻灯片继续继承其现有主题。

将主题从一个演示文稿迁移到另一个演示文稿的最安全方式是什么?

在移动幻灯片并保留其源外观时,使用 [IMasterSlideCollection.AddClone](https://reference.aspose.com/slides/zh/net/aspose.slides/imasterslidecollection/addclone/) 将源母版克隆到目标演示文稿,并使用 [ISlideCollection.AddClone](https://reference.aspose.com/slides/zh/net/aspose.slides/islidecollection/addclone/) 将幻灯片连同该母版一起克隆。这会将母版、布局和主题一起保留。

如何查看继承和覆盖后的有效值?

对于幻灯片或布局主题,使用 [BaseOverrideThemeManager.CreateThemeEffective](https://reference.aspose.com/slides/zh/net/aspose.slides.theme/baseoverridethememanager/createthemeeffective/);对于格式对象,如背景和填充,分别使用 [Background.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/background/geteffective/)[FillFormat.GetEffective](https://reference.aspose.com/slides/zh/net/aspose.slides/fillformat/geteffective/)。这些 API 返回在继承和覆盖应用后的解析值。