在 .NET 中格式化演示文稿文本

概述

本文展示了如何使用 Aspose.Slides for .NET 对 PowerPoint 和 OpenDocument 演示文稿中的文本进行格式化。它涵盖了突出显示、背景颜色、透明度、字符间距、字体属性、旋转、段落间距、自动适应行为、文本锚定、制表位和语言设置。

在下面的示例中,我们将使用名为 “sample.pptx” 的文件,该文件在第一页包含一个仅有的文本框,文本内容如下:

示例文本

突出显示文本

当需要突出显示文本框中符合特定样本的文本时,请使用ITextFrame.HighlightText 方法。该方法为匹配的文本片段应用突出显示颜色,并且可以结合TextSearchOptions 控制搜索方式,例如仅匹配完整单词。

下面的代码示例突出显示所有字符 “try” 的出现,然后仅突出显示完整单词 “to”

using (var presentation = new Presentation("sample.pptx"))
{
    // 获取第一张幻灯片中的第一个形状。
    var shape = (IAutoShape)presentation.Slides[0].Shapes[0];

    // 在形状中突出显示单词 "try"。
    shape.TextFrame.HighlightText("try", Color.LightBlue);

    var searchOptions = new TextSearchOptions()
    {
        WholeWordsOnly = true
    };

    // 在形状中突出显示单词 "to"。
    shape.TextFrame.HighlightText("to", Color.Violet, searchOptions, null);

    presentation.Save("highlighted_text.pptx", SaveFormat.Pptx);
}

结果:

突出显示的文本

使用正则表达式突出显示文本

ITextFrame.HighlightRegex 方法突出显示正则表达式找到的文本匹配项。在 .NET 中,此 API 通过ITextFrame 暴露。

下面的代码示例突出显示所有包含 七个或更多字符 的单词:

using (var presentation = new Presentation(folderPath + "sample.pptx"))
{
    var shape = (IAutoShape)presentation.Slides[0].Shapes[0];

    var regex = new Regex(@"\b[^\s]{7,}\b");

    // 突出显示所有包含七个或更多字符的单词。
    shape.TextFrame.HighlightRegex(regex, Color.Yellow, null);

    presentation.Save(folderPath + "highlighted_text_using_regex.pptx", SaveFormat.Pptx);
}

结果:

使用正则表达式突出显示的文本

设置文本背景颜色

使用IParagraphFormat.DefaultPortionFormat 为段落设置默认的突出显示颜色,或使用IPortionFormat.HighlightColor 为单独的文本片段设置颜色。

以下代码示例演示如何为 整个段落 设置背景颜色:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // 为整个段落设置突出显示颜色。
    paragraph.ParagraphFormat.DefaultPortionFormat.HighlightColor.Color = Color.LightGray;

    presentation.Save("gray_paragraph.pptx", SaveFormat.Pptx);
}

结果:

灰色段落

下面的代码示例演示如何为 加粗字体的文本片段 设置背景颜色:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // 为文本片段设置突出显示颜色。
            portion.PortionFormat.HighlightColor.Color = Color.LightGray;
        }
    }

    presentation.Save("gray_text_portions.pptx", SaveFormat.Pptx);
}

结果:

灰色文本片段

对齐文本段落

使用IParagraphFormat.Alignment 设置文本框内段落的对齐方式。可选值包括居中、左对齐、右对齐、两端对齐等。

以下代码示例展示如何将段落对齐到 居中

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // 将段落的对齐方式设置为居中。
    paragraph.ParagraphFormat.Alignment = TextAlignment.Center;

    presentation.Save("aligned_paragraph.pptx", SaveFormat.Pptx);
}

结果:

对齐的段落

设置文本透明度

文本透明度通过分配给IPortionFormat.FillFormat 的颜色的 alpha 分量来控制。在以下示例中,alpha = 50 是 0–255 规模的 ARGB 透明度通道值,而非透明度百分比。

下面的代码示例演示如何为 整个段落 应用透明度:

int alpha = 50;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // 将文本的填充颜色设置为透明颜色。
    paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.FillType = FillType.Solid;
    paragraph.ParagraphFormat.DefaultPortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);

    presentation.Save("transparent_paragraph.pptx", SaveFormat.Pptx);
}

结果:

透明段落

以下代码示例演示如何为 加粗字体的文本片段 应用透明度:

int alpha = 50;

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // 设置文本片段的透明度。
            portion.PortionFormat.FillFormat.FillType = FillType.Solid;
            portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.FromArgb(alpha, Color.Black);
        }
    }

    presentation.Save("transparent_text_portions.pptx", SaveFormat.Pptx);
}

结果:

透明文本片段

设置文本字符间距

使用IBasePortionFormat.Spacing 可在文本框中扩展或收缩字符之间的间距。

以下 C# 代码展示如何为 整个段落 扩展字符间距:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // 注意:使用负值压缩字符间距。
    paragraph.ParagraphFormat.DefaultPortionFormat.Spacing = 3;  // 展开字符间距。

    presentation.Save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx);
}

结果:

段落中的字符间距

下面的代码示例展示如何为 加粗字体的文本片段 扩展字符间距:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // 注意:使用负值压缩字符间距。
            portion.PortionFormat.Spacing = 3;  // 展开字符间距。
        }
    }

    presentation.Save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx);
}

结果:

文本片段中的字符间距

禁用特定字体的字距调整

在某些情况下,Aspose.Slides 渲染的文本看起来比 PowerPoint 中的相同文本略紧。这可能是因为 PowerPoint 会忽略某些字体的字距调整数据,即使该字体包含有效的字距信息且在 PowerPoint 设置中已启用字距调整。

为使渲染输出更接近 PowerPoint,您可以为使用受影响字体的文本片段禁用字距调整。将IPortionFormat.KerningMinimalSize 设置为明显大于实际字体大小的值:

using (var presentation = new Presentation("presentation.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var targetFont = "Roboto";

    foreach (var paragraph in autoShape.TextFrame.Paragraphs)
    {
        foreach (var portion in paragraph.Portions)
        {
            if ((portion.PortionFormat.LatinFont != null &&
                 portion.PortionFormat.LatinFont.FontName == targetFont) ||
                (portion.PortionFormat.EastAsianFont != null &&
                 portion.PortionFormat.EastAsianFont.FontName == targetFont) ||
                (portion.PortionFormat.ComplexScriptFont != null &&
                 portion.PortionFormat.ComplexScriptFont.FontName == targetFont))
            {
                portion.PortionFormat.KerningMinimalSize = 100;
            }
        }
    }

    presentation.Save("output.pptx", SaveFormat.Pptx);
}

此设置可阻止对匹配的文本片段应用字距调整,从而帮助 Aspose.Slides 的渲染与 PowerPoint 对受影响字体的视觉输出保持一致。

管理文本字体属性

可以通过IParagraphFormat.DefaultPortionFormat 在段落层面设置字体属性,或通过IPortionFormat 在单独的文本片段上设置。

以下代码为整个段落设置字体和文本样式:它为段落中的所有片段应用字体大小、粗体、斜体、点状下划线以及 Times New Roman 字体。

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    // 为段落设置字体属性。
    paragraph.ParagraphFormat.DefaultPortionFormat.FontHeight = 12;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontItalic = NullableBool.True;
    paragraph.ParagraphFormat.DefaultPortionFormat.FontUnderline = TextUnderlineType.Dotted;
    paragraph.ParagraphFormat.DefaultPortionFormat.LatinFont = new FontData("Times New Roman");

    presentation.Save("font_properties_for_paragraph.pptx", SaveFormat.Pptx);
}

结果:

段落的字体属性

下面的代码示例将类似属性应用于 加粗字体的文本片段

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    foreach (var portion in paragraph.Portions)
    {
        if (portion.PortionFormat.GetEffective().FontBold)
        {
            // 为文本片段设置字体属性。
            portion.PortionFormat.FontHeight = 13;
            portion.PortionFormat.FontItalic = NullableBool.True;
            portion.PortionFormat.FontUnderline = TextUnderlineType.Dotted;
            portion.PortionFormat.LatinFont = new FontData("Times New Roman");
        }
    }

    presentation.Save("font_properties_for_text_portions.pptx", SaveFormat.Pptx);
}

结果:

文本片段的字体属性

设置文本旋转

使用ITextFrameFormat.TextVerticalType 可在形状内设置预定义的文本方向。

以下代码示例将形状中的文本方向设置为 Vertical270,即将文本 逆时针旋转 90 度

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.TextVerticalType = TextVerticalType.Vertical270;

    presentation.Save("text_rotation.pptx", SaveFormat.Pptx);
}

结果:

文本旋转

为文本框设置自定义旋转

使用ITextFrameFormat.RotationAngle 可为ITextFrame 设置自定义旋转角度。

下面的代码示例将在形状内将文本框顺时针旋转 3 度:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.RotationAngle = 3;

    presentation.Save("custom_text_rotation.pptx", SaveFormat.Pptx);
}

结果:

自定义文本旋转

设置段落行距

Aspose.Slides 提供IParagraphFormat.SpaceAfterIParagraphFormat.SpaceBeforeIParagraphFormat.SpaceWithin 来控制段落间距。这些属性的使用方式如下:

  • 使用正值指定行距为行高的百分比。
  • 使用负值以磅为单位指定行距。

以下代码示例展示如何在段落内部指定行距:

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    paragraph.ParagraphFormat.SpaceWithin = 200;

    presentation.Save("line_spacing.pptx", SaveFormat.Pptx);
}

结果:

段落内的行距

设置文本框的自动适应类型

ITextFrameFormat.AutofitType 决定当文本超出容器边界时的行为。使用它可以控制文本是收缩、溢出还是自动调整形状大小。

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.AutofitType = TextAutofitType.Shape;

    presentation.Save("autofit_type.pptx", SaveFormat.Pptx);
}

设置文本框锚点

ITextFrameFormat.AnchoringType 定义文本在形状内部的垂直位置,例如位于顶部、中部或底部。

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    autoShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Bottom;

    presentation.Save("text_anchor.pptx", SaveFormat.Pptx);
}

设置文本制表位

使用IParagraphFormat.DefaultTabSizeIParagraphFormat.Tabs 可在段落中配置制表位。

using (var presentation = new Presentation("sample.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var paragraph = autoShape.TextFrame.Paragraphs[0];

    paragraph.ParagraphFormat.DefaultTabSize = 100;
    paragraph.ParagraphFormat.Tabs.Add(30, TabAlignment.Left);

    presentation.Save("paragraph_tabs.pptx", SaveFormat.Pptx);
}

结果:

段落制表位

设置校对语言

Aspose.Slides 提供IPortionFormat.LanguageId,允许为文本片段设置校对语言。校对语言决定 PowerPoint 中拼写和语法检查使用的语言。

以下代码示例展示如何为文本片段设置校对语言:

using (var presentation = new Presentation("presentation.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];

    var paragraph = autoShape.TextFrame.Paragraphs[0];
    paragraph.Portions.Clear();

    var font = new FontData("SimSun");

    var textPortion = new Portion();
    textPortion.PortionFormat.ComplexScriptFont = font;
    textPortion.PortionFormat.EastAsianFont = font;
    textPortion.PortionFormat.LatinFont = font;

    // 设置校对语言的 Id。
    textPortion.PortionFormat.LanguageId = "zh-CN";

    textPortion.Text = "1。";
    paragraph.Portions.Add(textPortion);

    presentation.Save("proofing_language.pptx", SaveFormat.Pptx);
}

设置默认语言

使用LoadOptions.DefaultTextLanguage 可定义在加载或创建演示文稿时创建的文本的默认语言。

var loadOptions = new LoadOptions();
loadOptions.DefaultTextLanguage = "en-US";

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

    // 添加一个带文本的新矩形形状。
    var shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 20, 20, 150, 50);
    shape.TextFrame.Text = "Sample text";

    // 检查第一个文本片段的语言。
    var portion = shape.TextFrame.Paragraphs[0].Portions[0];
    Console.WriteLine(portion.PortionFormat.LanguageId);
}

设置默认文本样式

要在演示文稿级别应用默认文本格式,请使用IPresentation.DefaultTextStyle

以下代码示例展示如何在新演示文稿中为所有幻灯片的文本设置 14 磅大小的粗体默认字体。

using (var presentation = new Presentation())
{
    // 获取顶级段落格式。
    var paragraphFormat = presentation.DefaultTextStyle.GetLevel(0);

    if (paragraphFormat != null)
    {
        paragraphFormat.DefaultPortionFormat.FontHeight = 14;
        paragraphFormat.DefaultPortionFormat.FontBold = NullableBool.True;
    }

    presentation.Save("default_text_style.pptx", SaveFormat.Pptx);
}

提取带全大写效果的文本

在 PowerPoint 中,应用 全大写 字体效果会使文本在幻灯片上显示为大写,即使原始输入为小写。当使用 Aspose.Slides 检索此类文本片段时,库会返回原始输入的文本。若要匹配显示的文本,请检查TextCapType 并在值为 All 时将返回的字符串转换为大写。

假设我们在 sample2.pptx 文件的第一页有如下文本框。

全大写效果

下面的代码示例展示如何提取带 全大写 效果的文本:

using (var presentation = new Presentation("sample2.pptx"))
{
    var autoShape = (IAutoShape)presentation.Slides[0].Shapes[0];
    var textPortion = autoShape.TextFrame.Paragraphs[0].Portions[0];

    Console.WriteLine($"Original text: {textPortion.Text}");

    var textFormat = textPortion.PortionFormat.GetEffective();
    if (textFormat.TextCapType == TextCapType.All)
    {
        var text = textPortion.Text.ToUpper();
        Console.WriteLine($"All-Caps effect: {text}");
    }
}

输出:

Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!

常见问题

如何修改幻灯片表格中的文本?

要修改幻灯片表格中的文本,请使用ITable。遍历单元格并通过ICell.TextFrame 更新每个单元格的文本框,及通过IParagraph.ParagraphFormat 调整段落格式。

如何在 PowerPoint 幻灯片的文本上应用渐变颜色?

要为文本应用渐变颜色,请使用IPortionFormat.FillFormat。将IFillFormat.FillType 设置为FillType.Gradient,并配置渐变停靠点、方向和透明度。