在 Java 中格式化 PowerPoint 文本

突出显示文本

Method highlightText 已添加到 ITextFrame 接口和 TextFrame 类。

它允许使用文本示例通过背景颜色突出显示文本部分,类似于 PowerPoint 2019 中的“文本突出显示颜色”工具。

以下代码片段演示如何使用此功能:

Presentation pres = new Presentation("Presentation.pptx");
try {
    TextHighlightingOptions textHighlightingOptions = new TextHighlightingOptions();
    textHighlightingOptions.setWholeWordsOnly(true);
    
    ((AutoShape)pres.getSlides().get_Item(0).getShapes().get_Item(0)).getTextFrame().highlightText("title", Color.BLUE); // 突出显示所有单词 'important'
    ((AutoShape)pres.getSlides().get_Item(0).getShapes().get_Item(0)).getTextFrame().highlightText("to", Color.MAGENTA, textHighlightingOptions);// 突出显示所有单独的 'the' 出现
    pres.save("OutputPresentation-highlight.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

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

Method highlightRegex 已添加到 ITextFrame 接口和 TextFrame 类。

它允许使用正则表达式通过背景颜色突出显示文本部分,类似于 PowerPoint 2019 中的“文本突出显示颜色”工具。

以下代码片段演示如何使用此功能:

Presentation pres = new Presentation("Presentation.pptx");
try {
    TextHighlightingOptions options = new TextHighlightingOptions();
    
    ((AutoShape) pres.getSlides().get_Item(0).getShapes().get_Item(0)).getTextFrame().highlightRegex("\\b[^\\s]{4}\\b", java.awt.Color.YELLOW, options); // 高亮所有长度为10个字符或更长的单词
    
    pres.save("OutputPresentation-highlight.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

设置文本背景颜色

Aspose.Slides 允许您为文本的背景指定首选颜色。

此 Java 代码演示如何为整段文本设置背景颜色:

Presentation pres = new Presentation();
try {
    IAutoShape autoShape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
    autoShape.getTextFrame().getParagraphs().clear();

    Paragraph para = new Paragraph();

    Portion portion1 = new Portion("Black");
    portion1.getPortionFormat().setFontBold(NullableBool.True);

    Portion portion2 = new Portion(" Red ");

    Portion portion3 = new Portion("Black");
    portion3.getPortionFormat().setFontBold(NullableBool.True);

    para.getPortions().add(portion1);
    para.getPortions().add(portion2);
    para.getPortions().add(portion3);
    autoShape.getTextFrame().getParagraphs().add(para);

    pres.save("text.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Presentation presentation = new Presentation("text.pptx");
try {
    IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    StreamSupport.stream(autoShape.getTextFrame().getParagraphs().spliterator(), false)
            .map(p -> p.getPortions())
            .forEach(c -> c.forEach(ic -> ic.getPortionFormat().getHighlightColor().setColor(Color.BLUE)));

    presentation.save("text-red.pptx", SaveFormat.Pptx);
} finally {
    if (presentation != null) presentation.dispose();
}

此 Java 代码演示如何仅为文本的一部分设置背景颜色:

Presentation pres = new Presentation();
try {
    IAutoShape autoShape = pres.getSlides().get_Item(0).getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
    autoShape.getTextFrame().getParagraphs().clear();
    
    Paragraph para = new Paragraph();

    Portion portion1 = new Portion("Black");
    portion1.getPortionFormat().setFontBold(NullableBool.True);

    Portion portion2 = new Portion(" Red ");

    Portion portion3 = new Portion("Black");
    portion3.getPortionFormat().setFontBold(NullableBool.True);
    
    para.getPortions().add(portion1);
    para.getPortions().add(portion2);
    para.getPortions().add(portion3);
    autoShape.getTextFrame().getParagraphs().add(para);
    
    pres.save("text.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

Presentation presentation = new Presentation("text.pptx");
try {
    IAutoShape autoShape = (IAutoShape)presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    Optional<IPortion> redPortion = StreamSupport.stream(autoShape.getTextFrame().getParagraphs().get_Item(0).getPortions().spliterator(), false)
            .filter(p -> p.getText().contains("Red"))
            .findFirst();

    if(redPortion.isPresent())
        redPortion.get().getPortionFormat().getHighlightColor().setColor(Color.RED);

    presentation.save("text-red.pptx", SaveFormat.Pptx);
} finally {
    if (presentation != null) presentation.dispose();
}

对齐文本段落

文本格式是创建任何文档或演示文稿的关键要素之一。我们知道 Aspose.Slides for Java 支持在幻灯片中添加文本,但在本主题中,我们将了解如何控制幻灯片中文本段落的对齐方式。请按照以下步骤使用 Aspose.Slides for Java 对齐文本段落:

  1. 创建 Presentation 类的实例。
  2. 使用索引获取幻灯片的引用。
  3. 访问幻灯片中存在的占位符形状,并将其强制转换为 AutoShape
  4. AutoShape 暴露的 TextFrame 获取需要对齐的 Paragraph。
  5. 对 Paragraph 进行对齐。Paragraph 可以对齐到右、左、居中和两端对齐。
  6. 将修改后的演示文稿写入为 PPTX 文件。

以下给出上述步骤的实现。

// 实例化一个代表 PPTX 文件的 Presentation 对象
Presentation pres = new Presentation("ParagraphsAlignment.pptx");
try {
    // 访问第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);

    // 访问幻灯片中的第一个和第二个占位符,并将其强制转换为 AutoShape
    ITextFrame tf1 = ((IAutoShape)slide.getShapes().get_Item(0)).getTextFrame();
    ITextFrame tf2 = ((IAutoShape)slide.getShapes().get_Item(1)).getTextFrame();

    // 更改两个占位符中的文本
    tf1.setText("Center Align by Aspose");
    tf2.setText("Center Align by Aspose");

    // 获取占位符的第一段落
    IParagraph para1 = tf1.getParagraphs().get_Item(0);
    IParagraph para2 = tf2.getParagraphs().get_Item(0);

    // 将文本段落居中对齐
    para1.getParagraphFormat().setAlignment(TextAlignment.Center);
    para2.getParagraphFormat().setAlignment(TextAlignment.Center);

    //将演示文稿保存为 PPTX 文件
    pres.save("Centeralign_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

设置文本透明度

本文演示如何使用 Aspose.Slides for Java 为任何文本形状设置透明度属性。要为文本设置透明度,请按照以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 获取幻灯片的引用。
  3. 设置阴影颜色
  4. 将演示文稿写入为 PPTX 文件。

以下给出上述步骤的实现。

Presentation pres = new Presentation("transparency.pptx");
try {
    IAutoShape shape = (IAutoShape)pres.getSlides().get_Item(0).getShapes().get_Item(0);
    IEffectFormat effects = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat().getEffectFormat();

    IOuterShadow outerShadowEffect = effects.getOuterShadowEffect();

    Color shadowColor = outerShadowEffect.getShadowColor().getColor();
    System.out.println(shadowColor.toString() + " - transparency is: "+ (shadowColor.getAlpha() / 255f) * 100);

    // 将透明度设置为零百分比
    outerShadowEffect.getShadowColor().setColor(new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(), 255));

    pres.save("transparency-2.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

设置文本字符间距

Aspose.Slides 允许您设置文本框中字母之间的间距。这样,您可以通过扩大或压缩字符间距来调整行或文本块的视觉密度。

此 Java 代码演示如何为一行文本扩展间距并为另一行文本压缩间距:

Presentation presentation = new Presentation("in.pptx");

IAutoShape textBox1 = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(0);
IAutoShape textBox2 = (IAutoShape) presentation.getSlides().get_Item(0).getShapes().get_Item(1);

textBox1.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setSpacing(20); // 展开
textBox2.getTextFrame().getParagraphs().get_Item(0).getParagraphFormat().getDefaultPortionFormat().setSpacing(-2); // 压缩

presentation.save("out.pptx", SaveFormat.Pptx);

管理段落的字体属性

演示文稿通常包含文本和图像。文本可以以多种方式格式化,以突出显示特定章节和单词,或符合公司样式。文本格式帮助用户改变演示内容的外观和感觉。本文展示如何使用 Aspose.Slides for Java 配置幻灯片上文本段落的字体属性。使用 Aspose.Slides for Java 管理段落的字体属性的步骤:

  1. 创建 Presentation 类的实例。
  2. 使用索引获取幻灯片的引用。
  3. 访问幻灯片中的占位符形状并将其强制转换为 IAutoShape
  4. IAutoShape 暴露的 ITextFrame 获取 Paragraph
  5. 两端对齐段落。
  6. 访问段落的文本 Portion。
  7. 使用 FontData 定义字体并相应地设置文本 Portion 的 Font。
    1. 将字体设为粗体。
    2. 将字体设为斜体。
  8. 使用 Portion 对象暴露的 getFillFormat 设置字体颜色。
  9. 将修改后的演示文稿写入 PPTX 文件。

以下给出上述步骤的实现。它对一个未修饰的演示文稿进行处理,并格式化其中一张幻灯片的字体。

// 实例化一个代表 PPTX 文件的 Presentation 对象
Presentation pres = new Presentation("FontProperties.pptx");
try {
    // 使用幻灯片位置访问幻灯片
    ISlide slide = pres.getSlides().get_Item(0);

    // 访问幻灯片中的第一个和第二个占位符,并将其强制转换为 AutoShape
    ITextFrame tf1 = ((IAutoShape)slide.getShapes().get_Item(0)).getTextFrame();
    ITextFrame tf2 = ((IAutoShape)slide.getShapes().get_Item(1)).getTextFrame();

    // 访问第一段落
    IParagraph para1 = tf1.getParagraphs().get_Item(0);
    IParagraph para2 = tf2.getParagraphs().get_Item(0);

    // 访问第一个 Portion
    IPortion port1 = para1.getPortions().get_Item(0);
    IPortion port2 = para2.getPortions().get_Item(0);

    // 定义新字体
    FontData fd1 = new FontData("Elephant");
    FontData fd2 = new FontData("Castellar");

    // 将新字体分配给 Portion
    port1.getPortionFormat().setLatinFont(fd1);
    port2.getPortionFormat().setLatinFont(fd2);

    // 将字体设为粗体
    port1.getPortionFormat().setFontBold(NullableBool.True);
    port2.getPortionFormat().setFontBold(NullableBool.True);

    // 将字体设为斜体
    port1.getPortionFormat().setFontItalic(NullableBool.True);
    port2.getPortionFormat().setFontItalic(NullableBool.True);

    // 设置字体颜色
    port1.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    port1.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.MAGENTA);
    port2.getPortionFormat().setFillType(FillType.Solid);
    port2.getPortionFormat().getSolidFillColor().setColor(Color.ORANGE);

    // 将 PPTX 写入磁盘
    pres.save("WelcomeFont_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

管理文本的字体族

Portion 用于在段落中保存具有相似格式的文本。本文展示如何使用 Aspose.Slides for Java 创建一个包含文本的文本框,然后定义特定的字体以及字体族类别的其他属性。创建文本框并设置其中文本的字体属性的步骤:

  1. 创建 Presentation 类的实例。
  2. 使用索引获取幻灯片的引用。
  3. 向幻灯片添加类型为 RectangleIAutoShape
  4. 移除与该 IAutoShape 关联的填充样式。
  5. 访问 AutoShape 的 TextFrame。
  6. 向 TextFrame 添加一些文本。
  7. 访问与 ITextFrame 关联的 Portion 对象。
  8. Portion 定义要使用的字体。
  9. 使用 Portion 对象暴露的相关属性设置粗体、斜体、下划线、颜色和高度等其他字体属性。
  10. 将修改后的演示文稿写入 PPTX 文件。

以下给出上述步骤的实现。

// 实例化 Presentation
Presentation pres = new Presentation();
try {

    // 获取第一张幻灯片
    ISlide sld = pres.getSlides().get_Item(0);

    // 添加一个矩形类型的 AutoShape
    IAutoShape ashp = sld.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 50);

    // 移除与 AutoShape 关联的任何填充样式
    ashp.getFillFormat().setFillType(FillType.NoFill);

    // 访问与 AutoShape 关联的 TextFrame
    ITextFrame tf = ashp.getTextFrame();
    tf.setText("Aspose TextBox");

    // 访问与 TextFrame 关联的 Portion
    IPortion port = tf.getParagraphs().get_Item(0).getPortions().get_Item(0);

    // 为 Portion 设置字体
    port.getPortionFormat().setLatinFont(new FontData("Times New Roman"));

    // 设置字体的粗体属性
    port.getPortionFormat().setFontBold(NullableBool.True);

    // 设置字体的斜体属性
    port.getPortionFormat().setFontItalic(NullableBool.True);

    // 设置字体的下划线属性
    port.getPortionFormat().setFontUnderline(TextUnderlineType.Single);

    // 设置字体的高度
    port.getPortionFormat().setFontHeight(25);

    // 设置字体的颜色
    port.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    port.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);

    // 将 PPTX 写入磁盘 
    pres.save("SetTextFontProperties_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

设置文本字体大小

Aspose.Slides 允许您为段落中现有文本以及以后可能添加的文本选择首选的字体大小。

此 Java 代码演示如何为段落中包含的文本设置字体大小:

Presentation presentation = new Presentation("example.pptx");
try {
    // 获取第一个形状,例如。
    IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);

    if (shape instanceof IAutoShape )
    {
        IAutoShape autoShape = (AutoShape) shape;
        // 获取第一段落,例如。
        IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);

        // 将段落中所有文本部分的默认字体大小设置为 20 磅。 
        paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(20);

        // 将段落中当前文本部分的字体大小设置为 20 磅。 
        for(IPortion portion : paragraph.getPortions())
        {
            portion.getPortionFormat().setFontHeight(20);
        }
    }
} finally {
    if (presentation != null) presentation.dispose();
}

设置文本旋转

Aspose.Slides for Java 允许开发者旋转文本。文本可以设置为以下方向:HorizontalVerticalVertical270WordArtVerticalEastAsianVerticalMongolianVerticalWordArtVerticalRightToLeft。要旋转任何 TextFrame 的文本,请按照以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 访问第一张幻灯片。
  3. 向幻灯片添加任意形状。
  4. 访问 ITextFrame
  5. 旋转文本
  6. 将文件保存到磁盘。
// 创建 Presentation 类的实例
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);
    
    // 添加矩形类型的 AutoShape
    IAutoShape ashp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 350, 350);
    
    // 向矩形添加 TextFrame
    ashp.addTextFrame("");
    ashp.getFillFormat().setFillType(FillType.NoFill);
    
    // 访问文本框
    ITextFrame txtFrame = ashp.getTextFrame();
    txtFrame.getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270);
    
    // 为文本框创建 Paragraph 对象
    IParagraph para = txtFrame.getParagraphs().get_Item(0);
    
    // 为段落创建 Portion 对象
    IPortion portion = para.getPortions().get_Item(0);
    portion.setText("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    
    // 保存演示文稿
    pres.save("RotateText_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

为 TextFrame 设置自定义旋转角度

Aspose.Slides for Java 现在支持为 TextFrame 设置自定义旋转角度。在本主题中,我们将通过示例演示如何在 Aspose.Slides 中设置 RotationAngle 属性。已向 IChartTextBlockFormatITextFrameFormat 接口添加了新方法 setRotationAnglegetRotationAngle,允许为 TextFrame 设置自定义旋转角度。设置 RotationAngle,请按照以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 在幻灯片上添加图表。
  3. 设置 RotationAngle 属性
  4. 将演示文稿写入 PPTX 文件。

下面的示例设置了 RotationAngle 属性。

// 创建 Presentation 类的实例
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);

    // 添加矩形类型的 AutoShape
    IAutoShape ashp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 350, 350);

    // 向矩形添加 TextFrame
    ashp.addTextFrame("");
    ashp.getFillFormat().setFillType(FillType.NoFill);

    // 访问文本框
    ITextFrame txtFrame = ashp.getTextFrame();
    txtFrame.getTextFrameFormat().setRotationAngle(25);

    // 为文本框创建 Paragraph 对象
    IParagraph para = txtFrame.getParagraphs().get_Item(0);

    // 为段落创建 Portion 对象
    IPortion portion = para.getPortions().get_Item(0);
    portion.setText("Text rotation example.");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    // 保存演示文稿
    pres.save(resourcesOutputPath+"RotateText_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

段落行距

Aspose.Slides 在 ParagraphFormat 下提供了 SpaceAfterSpaceBeforeSpaceWithin 属性,可用于管理段落的行距。这三个属性的使用方式如下:

  • 要以百分比指定段落的行距,请使用正值。
  • 要以点数指定段落的行距,请使用负值。

例如,您可以通过将 SpaceBefore 属性设为 -16 来为段落应用 16pt 的行距。

以下是为特定段落指定行距的步骤:

  1. 加载包含带有文本的 AutoShape 的演示文稿。
  2. 通过索引获取幻灯片的引用。
  3. 访问 TextFrame。
  4. 访问 Paragraph。
  5. 设置 Paragraph 属性。
  6. 保存演示文稿。

以下 Java 代码演示如何为段落指定行距:

// 创建 Presentation 类的实例
Presentation pres = new Presentation("Fonts.pptx");
try {
    // 根据索引获取幻灯片的引用
    ISlide sld = pres.getSlides().get_Item(0);
    
    // 访问 TextFrame
    ITextFrame tf1 = ((IAutoShape)sld.getShapes().get_Item(0)).getTextFrame();
    
    // 访问段落
    IParagraph para = tf1.getParagraphs().get_Item(0);
    
    // 设置段落的属性
    para.getParagraphFormat().setSpaceWithin(80);
    para.getParagraphFormat().setSpaceBefore(40);
    para.getParagraphFormat().setSpaceAfter(40);
    
    // 保存演示文稿
    pres.save("LineSpacing_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

为 TextFrame 设置 AutofitType 属性

在本主题中,我们将探讨文本框的不同格式属性。本文覆盖如何设置 TextFrame 的 AutofitType 属性、文本锚点以及在演示文稿中旋转文本。Aspose.Slides for Java 允许开发者为任意文本框设置 AutofitType 属性。AutofitType 可设为 NormalShape。如果设为 Normal,则形状保持不变,文本会自行调整而不改变形状;如果设为 Shape,则形状会被修改,仅容纳所需的文本。设置 TextFrame 的 AutofitType 属性,请按照以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 访问第一张幻灯片。
  3. 向幻灯片添加任意形状。
  4. 访问 ITextFrame
  5. 设置 TextFrame 的 AutofitType
  6. 将文件保存到磁盘。
// 创建 Presentation 类的实例
Presentation pres = new Presentation();
try {
    // 访问第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);

    // 添加矩形类型的 AutoShape
    IAutoShape ashp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 350, 150);

    // 向矩形添加 TextFrame
    ashp.addTextFrame("");
    ashp.getFillFormat().setFillType(FillType.NoFill);

    // 访问文本框
    ITextFrame txtFrame = ashp.getTextFrame();
    txtFrame.getTextFrameFormat().setAutofitType(TextAutofitType.Shape);

    // 为文本框创建 Paragraph 对象
    IParagraph para = txtFrame.getParagraphs().get_Item(0);

    // 为段落创建 Portion 对象
    IPortion portion = para.getPortions().get_Item(0);
    portion.setText("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);

    // 保存演示文稿
    pres.save(resourcesOutputPath + "formatText_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

设置 TextFrame 的锚点

Aspose.Slides for Java 允许开发者为任意 TextFrame 设置锚点。TextAnchorType 指定文本在形状中的放置位置。锚点类型可设为 TopCenterBottomJustifiedDistributed。设置 TextFrame 锚点,请按照以下步骤操作:

  1. 创建 Presentation 类的实例。
  2. 访问第一张幻灯片。
  3. 向幻灯片添加任意形状。
  4. 访问 ITextFrame
  5. 设置 TextAnchorType
  6. 将文件保存到磁盘。
// 创建 Presentation 类的实例
Presentation pres = new Presentation();
try {
    // 获取第一张幻灯片
    ISlide slide = pres.getSlides().get_Item(0);
    
    // 添加矩形类型的 AutoShape
    IAutoShape ashp = slide.getShapes().addAutoShape(ShapeType.Rectangle, 150, 75, 350, 350);
    
    // 向矩形添加 TextFrame
    ashp.addTextFrame("");
    ashp.getFillFormat().setFillType(FillType.NoFill);
    
    // 访问文本框
    ITextFrame txtFrame = ashp.getTextFrame();
    txtFrame.getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom);
    
    // 为文本框创建 Paragraph 对象
    IParagraph para = txtFrame.getParagraphs().get_Item(0);
    
    // 为段落创建 Portion 对象
    IPortion portion = para.getPortions().get_Item(0);
    portion.setText("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog.");
    portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid);
    portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK);
    
    // 保存演示文稿
    pres.save("AnchorText_out.pptx", SaveFormat.Pptx);
} finally {
    if (pres != null) pres.dispose();
}

演示文稿中的制表位和 EffectiveTabs

所有文本制表位均以像素为单位。

todo:image_alt_text
图例:2 个显式制表位和 2 个默认制表位
  • EffectiveTabs.ExplicitTabCount(在本例中为 2)属性等于 Tabs.Count。
  • EffectiveTabs 集合包含所有制表位(来自 Tabs 集合和默认制表位)。
  • EffectiveTabs.ExplicitTabCount(在本例中为 2)属性等于 Tabs.Count。
  • EffectiveTabs.DefaultTabSize(294)属性显示默认制表位之间的距离(本例中的第 3 和第 4 个)。
  • EffectiveTabs.GetTabByIndex(index) 当 index = 0 时返回第一个显式制表位(Position = 731),index = 1 时返回第二个显式制表位(Position = 1241)。若 index = 2 则返回第一个默认制表位(Position = 1470),依此类推。
  • EffectiveTabs.GetTabAfterPosition(pos) 用于获取某段文字后面的下一个制表位。例如有文本:“Hello World!"。要渲染该文本,需要先计算 “Hello” 的像素长度,然后以该值调用 GetTabAfterPosition,即可获得绘制 “world!” 的下一个制表位位置。

设置默认文本样式

如果需要一次性将相同的默认文本格式应用于演示文稿的所有文本元素,可以使用 IPresentation 接口的 getDefaultTextStyle 方法并设置首选格式。下面的代码示例展示如何为新演示文稿中所有幻灯片的文本设置默认粗体字体(14 pt)。

Presentation presentation = new Presentation();
try {
    // 获取顶层段落格式。
    IParagraphFormat paragraphFormat = presentation.getDefaultTextStyle().getLevel(0);

    if (paragraphFormat != null) {
        paragraphFormat.getDefaultPortionFormat().setFontHeight(14);
        paragraphFormat.getDefaultPortionFormat().setFontBold(NullableBool.True);
    }

    presentation.save("DefaultTextStyle.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

提取带全大写效果的文本

在 PowerPoint 中,应用 All Caps 字体效果会使幻灯片上的文本显示为大写,即使最初是小写输入。当您使用 Aspose.Slides 检索此类文本时,库会返回原始输入的文本。为处理此情况,请检查 TextCapType——如果指示 All,只需将返回的字符串转换为大写,以便您的输出与用户在幻灯片上看到的相匹配。

设想我们在 sample2.pptx 文件的第一张幻灯片上有如下文本框。

The All Caps effect

以下代码示例展示如何提取带有 All Caps 效果的文本:

Presentation presentation = new Presentation("sample2.pptx");
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    IAutoShape autoShape = (IAutoShape) slide.getShapes().get_Item(0);
    IParagraph paragraph = autoShape.getTextFrame().getParagraphs().get_Item(0);
    IPortion textPortion = paragraph.getPortions().get_Item(0);

    System.out.println("Original text: " + textPortion.getText());

    IPortionFormatEffectiveData textFormat = textPortion.getPortionFormat().getEffective();
    if (textFormat.getTextCapType() == TextCapType.All) {
        String text = textPortion.getText().toUpperCase();
        System.out.println("All-Caps effect: " + text);
    }
} finally {
    presentation.dispose();
}

输出:

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

常见问题

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

要修改幻灯片中表格的文本,需要使用 ITable 接口。您可以遍历表格中的所有单元格,通过访问每个单元格的 TextFrameParagraphFormat 属性来更改各单元格中的文本。

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

要对文本应用渐变颜色,请使用 BasePortionFormat 中的 getFillFormat 方法。将 FillFormat 设置为 Gradient,并定义渐变的起始和结束颜色以及方向、透明度等其他属性,以在文本上创建渐变效果。