在 Android 上格式化 PowerPoint 形状
介绍
通过调整轮廓、填充和效果来格式化 PowerPoint 中的形状。Aspose.Slides for Android via Java 让您可以以编程方式控制这些设置,包括透明度、旋转和 3D 格式化。

Aspose.Slides for Android via Java 提供了接口和方法,允许您使用 PowerPoint 中的相同选项来格式化形状。
格式线条
使用IShape.getLineFormat来控制形状的轮廓。此示例创建一个未填充的矩形,具有蓝色、虚线轮廓。ILineFormat.setStyle选择粗细复合线,而ILineFormat.setWidth将宽度设置为 7 磅,并且ILineFormat.setDashStyle选择虚线模式。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.getFillFormat().setFillType(FillType.NoFill);
shape.getLineFormat().setStyle(LineStyle.ThickThin);
shape.getLineFormat().setWidth(7);
shape.getLineFormat().setDashStyle(LineDashStyle.Dash);
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
presentation.save("formatted_lines.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

设置自定义线段破折号模式
当预定义的线条样式无法提供所需的破折号长度和间隙时,请使用ILineFormat.setCustomDashPattern。例如用于品牌轮廓或技术图表。为自定义模式分配后,线段破折号样式会自动更改为LineDashStyle.Custom。
数组条目在破折号和间隙长度之间交替,首先是破折号。每个值都是线宽的倍数:{ 4, 2 } 在宽度为 3 磅时会产生 12 磅的破折号后跟 6 磅的间隙。使用成对的有限、非负值来定义完整的破折号‑间隙段。示例使用正值以确保每个破折号和间隙均可见。
此示例创建五条相同长度、平直端帽的蓝色线条。前三条在宽度为 3 磅时比较不同的模式。第四条以两倍宽度重复第一种模式,从而将破折号和间隙长度均加倍。第五条从自定义模式切换为实线。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(600, 300, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
float[][] patterns = {
{ 4, 2 },
{ 1, 2 },
{ 6, 2, 1, 2 },
{ 4, 2 },
{ 4, 2 }
};
for (int i = 0; i < patterns.length; i++) {
IAutoShape line = slide.getShapes().addAutoShape(ShapeType.Line, 50, 50 + i * 50, 500, 0);
line.getLineFormat().setWidth(i == 3 ? 6 : 3);
line.getLineFormat().getFillFormat().setFillType(FillType.Solid);
line.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
line.getLineFormat().setCapStyle(LineCapStyle.Flat);
line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None);
line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None);
line.getLineFormat().setCustomDashPattern(patterns[i]);
}
// 将最后一条线恢复为实线样式。
slide.getShapes().get_Item(4).getLineFormat().setDashStyle(LineDashStyle.Solid);
presentation.save("custom_dash_patterns.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
若要返回预定义的虚线样式,请将LineDashStyle.Dash传递给ILineFormat.setDashStyle。所选样式控制线条的外观。
PowerPoint 中从上到下的结果:{4, 2}, 3 pt;{1, 2}, 3 pt;{6, 2, 1, 2}, 3 pt;{4, 2}, 6 pt;以及 Solid, 3 pt。

设置线端帽
使用ILineFormat.setCapStyle来设置开放线条的端点。LineCapStyle 类提供三种显式端帽样式:
Flat:笔画在端点处结束。Round:半圆形端帽延伸至端点之外,距离为线宽的一半。Square:方形端帽延伸至端点之外,距离为线宽的一半。
该类还包括 NotDefined,表示未显式指定端帽样式。
端帽不同于箭头(向线端添加符号)和接合(控制线段之间的拐角)。此示例通过ILineFormat.setBeginArrowheadStyle和ILineFormat.setEndArrowheadStyle禁用箭头,以便仅比较端帽形状。
下面的示例创建三条除端帽外完全相同的水平线,宽度为 20 磅。从上到下,它们分别使用平直、圆形和方形端帽。它们的端点具有相同的水平坐标;圆形和方形端帽在每个端点之外延伸 10 磅。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(500, 240, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
byte[] capStyles = new byte[] { LineCapStyle.Flat, LineCapStyle.Round, LineCapStyle.Square };
for (int i = 0; i < capStyles.length; i++) {
IAutoShape line = slide.getShapes().addAutoShape(ShapeType.Line, 100, 50 + i * 70, 300, 0);
line.getLineFormat().setWidth(20);
line.getLineFormat().getFillFormat().setFillType(FillType.Solid);
line.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
line.getLineFormat().setDashStyle(LineDashStyle.Solid);
line.getLineFormat().setCapStyle(capStyles[i]);
line.getLineFormat().setBeginArrowheadStyle(LineArrowheadStyle.None);
line.getLineFormat().setEndArrowheadStyle(LineArrowheadStyle.None);
}
presentation.save("line_caps.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
PowerPoint 中从上到下的结果:Flat、Round 和 Square。

对形状线条应用草图效果
草图效果使形状线条看起来像手绘。使用IShape.getLineFormat访问线条设置,使用ILineFormat.getSketchFormat访问草图设置,使用ISketchFormat.setSketchType从LineSketchType 类中选择值。
下面的 Java 代码展示了如何应用LineSketchType.Curved效果,读取显式分配的值,以及使用LineSketchType.None移除该效果:
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
ISketchFormat sketchFormat = shape.getLineFormat().getSketchFormat();
sketchFormat.setSketchType(LineSketchType.Curved);
int explicitSketchType = sketchFormat.getSketchType();
System.out.println("Explicit sketch type: " + explicitSketchType);
sketchFormat.setSketchType(LineSketchType.None);
} finally {
presentation.dispose();
}
ISketchFormat.getSketchType 返回的值表示直接分配给形状的设置。如果线条格式可以从主题、母版幻灯片或布局幻灯片继承,则使用ILineFormat.getEffective,访问ILineFormatEffectiveData.getSketchFormat,并读取ISketchFormatEffectiveData.getSketchType。有效值反映了解决继承后实际应用的格式。此示例需要一个已有的演示文稿,其中第一张幻灯片至少包含一个形状,并打印两个值:
import com.aspose.slides.*;
Presentation presentation = new Presentation("presentation.pptx");
try {
IShape shape = presentation.getSlides().get_Item(0).getShapes().get_Item(0);
ILineFormat lineFormat = shape.getLineFormat();
int explicitSketchType = lineFormat.getSketchFormat().getSketchType();
ILineFormatEffectiveData effectiveLineFormat = lineFormat.getEffective();
int effectiveSketchType = effectiveLineFormat.getSketchFormat().getSketchType();
System.out.println("Explicit sketch type: " + explicitSketchType);
System.out.println("Effective sketch type: " + effectiveSketchType);
} finally {
presentation.dispose();
}
格式连接样式
以下是三种连接类型选项:
- Round
- Miter
- Bevel
连接样式控制两个轮廓段在拐角处的连接方式。使用 Round 可获得圆角,使用 Miter 可获得尖角,使用 Bevel 可获得平坦的拐角。

此示例创建三个蓝色轮廓、宽度为 15 磅的黑色矩形。它使用ILineFormat.setJoinStyle分别应用LineJoinStyle 的斜接、平切和圆角值,以比较拐角的绘制效果。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape miterShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 75);
IAutoShape bevelShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 210, 20, 150, 75);
IAutoShape roundShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 135, 150, 75);
miterShape.getFillFormat().setFillType(FillType.Solid);
miterShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
bevelShape.getFillFormat().setFillType(FillType.Solid);
bevelShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
roundShape.getFillFormat().setFillType(FillType.Solid);
roundShape.getFillFormat().getSolidFillColor().setColor(Color.BLACK);
miterShape.getLineFormat().setWidth(15);
bevelShape.getLineFormat().setWidth(15);
roundShape.getLineFormat().setWidth(15);
miterShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
miterShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
bevelShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
bevelShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
roundShape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
roundShape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.BLUE);
miterShape.getLineFormat().setJoinStyle(LineJoinStyle.Miter);
bevelShape.getLineFormat().setJoinStyle(LineJoinStyle.Bevel);
roundShape.getLineFormat().setJoinStyle(LineJoinStyle.Round);
miterShape.getTextFrame().setText("Miter Join Style");
bevelShape.getTextFrame().setText("Bevel Join Style");
roundShape.getTextFrame().setText("Round Join Style");
presentation.save("join_styles.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
在尖角处限制斜接连接
使用ILineFormat.setMiterLimit并将ILineFormat.setJoinStyle设为LineJoinStyle.Miter来限制尖角的延伸距离。
该限制是内部和外部拐角之间距离与线宽的比率。例如,值 8 允许斜接长度最高为线宽的八倍;它并不是以磅为单位的度量。如果拐角超过该限制,PowerPoint 会在该拐角使用平切连接。更大的限制会产生更尖锐的点。此设置仅影响斜接连接,当拐角仍在限制范围内时更改它不会产生可见效果。
此示例创建两个相同的未填充三角形,轮廓宽度为 20 磅。左侧三角形的限制为 2,右侧为 8。每个三角形宽 80 磅、高 160 磅,其顶部拐角的斜接长度与线宽比约为 4.12。因此,PowerPoint 会对左侧的顶点使用平切,对右侧保留尖角。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(500, 320, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
float[] miterLimits = new float[] { 2f, 8f };
for (int i = 0; i < miterLimits.length; i++) {
IAutoShape triangle = slide.getShapes().addAutoShape(ShapeType.Triangle, 90 + i * 240, 110, 80, 160);
triangle.getFillFormat().setFillType(FillType.NoFill);
triangle.getLineFormat().setWidth(20);
triangle.getLineFormat().getFillFormat().setFillType(FillType.Solid);
triangle.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 0, 139));
triangle.getLineFormat().setJoinStyle(LineJoinStyle.Miter);
triangle.getLineFormat().setMiterLimit(miterLimits[i]);
}
presentation.save("miter_limits.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
PowerPoint 中的结果:左侧 Miter limit 2,右侧 miter limit 8。

渐变填充
渐变填充在形状内部混合颜色。此示例使用IFillFormat.getGradientFormat将椭圆填充为 45 度角的线性渐变,从红色过渡到紫色。使用IGradientStopCollection.addPresetColor方法在渐变的起点 (0) 和终点 (1) 放置颜色停止。清除现有的停止点可确保仅使用这两种颜色。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 150, 75);
shape.getFillFormat().setFillType(FillType.Gradient);
shape.getFillFormat().getGradientFormat().setGradientShape(GradientShape.Linear);
shape.getFillFormat().getGradientFormat().setLinearGradientAngle(45);
shape.getFillFormat().getGradientFormat().getGradientStops().clear();
shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(1.0f, PresetColor.Purple);
shape.getFillFormat().getGradientFormat().getGradientStops().addPresetColor(0.0f, PresetColor.Red);
presentation.save("gradient_fill.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

如果导出的图像中渐变显示与 PowerPoint 中不同,请参阅选择渐变渲染样式。您可以在不更改演示文稿中存储的渐变填充的前提下选择渲染样式。
设置线性渐变角度
使用IGradientFormat.setLinearGradientAngle来控制GradientShape.Linear 填充的方向。角度以度数表示,顺时针递增:0 表示从左到右,90 表示从上到下,180 表示从右到左,270 表示从下到上。支持小数角度。超出 [0, 360) 的有限角度会归一化为一次完整旋转,例如 -45 变为 315,360 变为 0。
此示例创建六个矩形,红、绿、蓝三种颜色停止分别位于位置 0、0.5 和 1。禁用缩放,以便在不受矩形比例影响的情况下比较角度。左到右的角度分别为第一行的 0、45、90,第二行的 135、180、270。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
float[] angles = new float[] { 0, 45, 90, 135, 180, 270 };
for (int i = 0; i < angles.length; i++) {
int x = 30 + (i % 3) * 230;
int y = 30 + (i / 3) * 190;
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 200, 150);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getFillFormat().setFillType(FillType.Gradient);
IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
gradient.setGradientShape(GradientShape.Linear);
gradient.getGradientStops().clear();
gradient.getGradientStops().add(0f, Color.RED);
gradient.getGradientStops().add(0.5f, Color.GREEN);
gradient.getGradientStops().add(1f, Color.BLUE);
gradient.setLinearGradientScaled(NullableBool.False);
gradient.setLinearGradientAngle(angles[i]);
}
presentation.save("linear_gradient_angles.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
PowerPoint 中的结果:

随形状比例缩放线性渐变
IGradientFormat.setLinearGradientScaled接受一个NullableBool 值。显式设为 False 可在形状比例变化时保持几何渐变角度不变,设为 True 则随形状的宽高进行缩放。启用缩放时,在非正方形形状上可见的角度可能与分配的角度不同。
下面的示例使用 45 度角和与角度比较相同的颜色停止。左列使用 False,右列使用 True。每列包含一个宽矩形上方和一个高矩形下方。两者起始宽度相同;改变第二个矩形的宽度和高度可演示在不更改渐变设置的情况下填充如何响应尺寸变化。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(720, 460, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
byte[] scalingOptions = new byte[] { NullableBool.False, NullableBool.True };
for (int column = 0; column < scalingOptions.length; column++) {
for (int row = 0; row < 2; row++) {
int x = 30 + column * 360;
int y = row == 0 ? 30 : 150;
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, y, 300, 90);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getFillFormat().setFillType(FillType.Gradient);
IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
gradient.setGradientShape(GradientShape.Linear);
gradient.getGradientStops().clear();
gradient.getGradientStops().add(0f, Color.RED);
gradient.getGradientStops().add(0.5f, Color.GREEN);
gradient.getGradientStops().add(1f, Color.BLUE);
gradient.setLinearGradientAngle(45);
gradient.setLinearGradientScaled(scalingOptions[column]);
if (row == 1) {
shape.setWidth(90);
shape.setHeight(280);
}
}
}
presentation.save("linear_gradient_scaling.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
PowerPoint 中的结果,左侧关闭缩放,右侧启用缩放:

在关闭缩放时,对角线色带在两个矩形中保持相同方向。启用缩放时,色带随形状比例伸展。这有助于在将形状适配到不同尺寸时复现模板的渐变。在正方形上,启用缩放不会改变此 45 度渐变的外观。
角度和缩放设置均适用于线性渐变。它们不控制GradientShape.Radial 或 GradientShape.Rectangle 填充。
将角度与渐变方向结合使用
IGradientFormat.setGradientDirection从GradientDirection 类中选择渐变样式;它并不是数值角度的替代品。对于线性渐变,角落样式可以在不更改通过setLinearGradientAngle分配的角度的情况下改变颜色分布的区域。比较角度或缩放时请保持方向不变。
下面的示例以相反的顺序应用相同的设置。两个矩形的外观相同:先设置方向后设置角度或相反均不改变结果。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
for (int i = 0; i < 2; i++) {
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30 + i * 140, 320, 100);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
shape.getFillFormat().setFillType(FillType.Gradient);
IGradientFormat gradient = shape.getFillFormat().getGradientFormat();
gradient.setGradientShape(GradientShape.Linear);
gradient.getGradientStops().clear();
gradient.getGradientStops().add(0f, Color.RED);
gradient.getGradientStops().add(0.5f, Color.GREEN);
gradient.getGradientStops().add(1f, Color.BLUE);
gradient.setLinearGradientScaled(NullableBool.True);
if (i == 0) {
gradient.setGradientDirection(GradientDirection.FromCorner1);
gradient.setLinearGradientAngle(30);
} else {
gradient.setLinearGradientAngle(30);
gradient.setGradientDirection(GradientDirection.FromCorner1);
}
}
presentation.save("linear_gradient_direction.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
图案填充
图案填充使用两种颜色的设计,例如点、条纹或格子。此示例使用IPatternFormat.setPatternStyle选择格子图案。IPatternFormat.getBackColor提供浅灰色背景,IPatternFormat.getForeColor提供黄色图案颜色。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.getFillFormat().setFillType(FillType.Pattern);
shape.getFillFormat().getPatternFormat().setPatternStyle(PatternStyle.Trellis);
shape.getFillFormat().getPatternFormat().getBackColor().setColor(Color.rgb(192, 192, 192));
shape.getFillFormat().getPatternFormat().getForeColor().setColor(Color.YELLOW);
presentation.save("pattern_fill.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

图片填充
图片填充在形状内部使用图像。此示例需要本地图片,并使用Images.fromFile将其加载到演示文稿的图像集合中。将IPictureFillFormat.setPictureFillMode设置为平铺模式,可在矩形内部重复图像。下面显示的源图像即为使用的图片:

import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 255, 130);
shape.getFillFormat().setFillType(FillType.Picture);
shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.Tile);
IImage image = Images.fromFile("lotus.png");
IPPImage picture;
try {
picture = presentation.getImages().addImage(image);
} finally {
image.dispose();
}
shape.getFillFormat().getPictureFillFormat().getPicture().setImage(picture);
presentation.save("picture_fill.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

将图片平铺为纹理
如果要将平铺图片设为纹理并自定义平铺行为,可使用IPictureFillFormat 接口和PictureFillFormat 类的以下方法:
- setPictureFillMode: 设置图片填充模式——
Tile或Stretch。 - setTileAlignment: 指定平铺在形状内的对齐方式。
- setTileFlip: 控制平铺是水平翻转、垂直翻转还是同时翻转。
- setTileOffsetX: 设置平铺相对于形状原点的水平偏移(单位为磅)。
- setTileOffsetY: 设置平铺相对于形状原点的垂直偏移(单位为磅)。
- setTileScaleX: 定义平铺的水平比例(百分比)。
- setTileScaleY: 定义平铺的垂直比例(百分比)。
此示例需要本地图片,并将IPictureFillFormat 配置为以原始大小的 50% 重复。将水平和垂直偏移设为 -32 磅;右下对齐并在两个方向翻转,以确定重复纹理的排列方式。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide firstSlide = presentation.getSlides().get_Item(0);
IAutoShape shape = firstSlide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 190, 95);
shape.getFillFormat().setFillType(FillType.Picture);
IImage sourceImage = Images.fromFile("lotus.png");
IPPImage presentationImage;
try {
presentationImage = presentation.getImages().addImage(sourceImage);
} finally {
sourceImage.dispose();
}
IPictureFillFormat pictureFillFormat = shape.getFillFormat().getPictureFillFormat();
pictureFillFormat.getPicture().setImage(presentationImage);
pictureFillFormat.setPictureFillMode(PictureFillMode.Tile);
pictureFillFormat.setTileOffsetX(-32);
pictureFillFormat.setTileOffsetY(-32);
pictureFillFormat.setTileScaleX(50);
pictureFillFormat.setTileScaleY(50);
pictureFillFormat.setTileAlignment(RectangleAlignment.BottomRight);
pictureFillFormat.setTileFlip(TileFlip.FlipBoth);
presentation.save("tile.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

纯色填充
纯色填充为形状提供单一、统一的颜色。此示例使用IFillFormat.setFillType选择纯色填充模式,并通过IFillFormat.getSolidFillColor 将颜色设为黄色,然后将矩形保存到演示文稿中。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.getFillFormat().setFillType(FillType.Solid);
shape.getFillFormat().getSolidFillColor().setColor(Color.YELLOW);
presentation.save("solid_color_fill.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

在保留主题引用的同时复制颜色
使用IColorFormat.copyFrom可以复制颜色定义,包括其颜色类型、主题引用和变换。这在将填充颜色在应继续遵循演示文稿主题的形状之间转移时很有用。该方法仅复制颜色本身;不会复制填充类型、轮廓或其他形状格式。请使用IFillFormat.setFillType显式设置目标的填充类型。
此示例创建三个矩形,左到右依次为:源、主题感知的复制以及固定 RGB 复制。源使用IColorFormat.setSchemeColor引用第一强调色,并通过IColorOperationCollection.add 添加色调。RGB 复制使用源的有效填充颜色,因此最初三个矩形外观相同。调用IColorFormat.setColor 设置固定的 RGB 值并清除任何现有的颜色变换。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(660, 220, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
presentation.getMasterTheme().getColorScheme().getAccent1().setColor(Color.rgb(65, 105, 225));
IAutoShape source = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 40, 180, 140);
IAutoShape themeCopy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 240, 40, 180, 140);
IAutoShape rgbCopy = slide.getShapes().addAutoShape(ShapeType.Rectangle, 450, 40, 180, 140);
for (IAutoShape shape : new IAutoShape[] { source, themeCopy, rgbCopy }) {
shape.getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
}
IColorFormat sourceColor = source.getFillFormat().getSolidFillColor();
sourceColor.setSchemeColor(SchemeColor.Accent1);
sourceColor.getColorTransform().add(ColorTransformOperation.Tint, 0.75f);
themeCopy.getFillFormat().getSolidFillColor().copyFrom(sourceColor);
rgbCopy.getFillFormat().getSolidFillColor().setColor(source.getFillFormat().getEffective().getSolidFillColor());
IImage before = slide.getImage(1f, 1f);
try {
before.save("color-copy-before.png", ImageFormat.Png);
} finally {
before.dispose();
}
// 主题引用会遵循新的强调色;固定的 RGB 值则不会。
presentation.getMasterTheme().getColorScheme().getAccent1().setColor(Color.rgb(255, 69, 0));
IImage after = slide.getImage(1f, 1f);
try {
after.save("color-copy-after.png", ImageFormat.Png);
} finally {
after.dispose();
}
presentation.save("copied_colors.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
主题更改前,三个填充均匹配:

主题更改后,源和主题感知的复制遵循新强调色并保留其色调。固定 RGB 复制仍保持蓝色:

复制的定义与源独立。随后更改源的主题引用、分配其他颜色或添加/移除其变换都不会影响目标。主题更改仍会影响两个形状,因为它们各自独立地引用相同的主题颜色。
保存并重新打开 PPTX 时,复制的颜色类型(通过IColorFormat.getColorType 返回)、主题引用以及变换集合(通过IColorFormat.getColorTransform 返回)均保持不变;不会将定义平铺为 RGB。
在不同主题上下文之间复制(例如具有不同强调色板的演示文稿),复制的引用会相对于目标的主题进行解析。因此,同一引用和变换在不同目标主题下可能产生不同的可见颜色。有关配置主题颜色的更多信息,请参阅演示文稿主题。
使用幻灯片背景作为形状填充
当形状应匹配幻灯片背景时(例如在演示文稿模板中),请使用IAutoShape.setUseBackgroundFill。首先使用IFillFormat.setFillType将填充类型设为FillType.NotDefined 以移除任何显式填充。通过此组合,形状将使用幻灯片背景而非其样式填充。仅启用该标志并不会移除已有的显式填充。
这与FillType.NoFill不同:背景填充会用幻灯片背景的相应区域覆盖形状后面的对象,而无填充则保留这些对象可见。渐变仍然与幻灯片对齐,而不是在每个形状内部重新启动。
以下示例创建一个母版背景供幻灯片继承,并在三个矩形后放置一条品红色横幅。左侧矩形使用背景填充,中间矩形无填充,右侧矩形从背景填充切换为显式的橙色填充。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
presentation.getSlideSize().setSize(720, 400, SlideSizeScaleType.DoNotScale);
ISlide slide = presentation.getSlides().get_Item(0);
// 从母版继承渐变。
slide.getBackground().setType(BackgroundType.NotDefined);
slide.getLayoutSlide().getBackground().setType(BackgroundType.NotDefined);
IBackground background = slide.getLayoutSlide().getMasterSlide().getBackground();
background.setType(BackgroundType.OwnBackground);
background.getFillFormat().setFillType(FillType.Gradient);
IGradientFormat gradient = background.getFillFormat().getGradientFormat();
gradient.setGradientShape(GradientShape.Linear);
gradient.setLinearGradientAngle(0);
gradient.getGradientStops().clear();
gradient.getGradientStops().add(0, Color.rgb(0, 0, 139));
gradient.getGradientStops().add(1, Color.rgb(135, 206, 250));
// 先添加此对象,使其位于矩形后面。
IAutoShape band = slide.getShapes().addAutoShape(ShapeType.Rectangle, 25, 150, 670, 100);
band.getFillFormat().setFillType(FillType.Solid);
band.getFillFormat().getSolidFillColor().setColor(Color.MAGENTA);
band.getLineFormat().getFillFormat().setFillType(FillType.NoFill);
IAutoShape backgroundShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 100, 160, 200);
backgroundShape.getFillFormat().setFillType(FillType.NotDefined);
backgroundShape.setUseBackgroundFill(true);
IAutoShape noFillShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 280, 100, 160, 200);
noFillShape.getFillFormat().setFillType(FillType.NoFill);
IAutoShape explicitShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 510, 100, 160, 200);
explicitShape.getFillFormat().setFillType(FillType.NotDefined);
explicitShape.setUseBackgroundFill(true);
// 在应用显式填充之前禁用背景填充。
explicitShape.setUseBackgroundFill(false);
explicitShape.getFillFormat().setFillType(FillType.Solid);
explicitShape.getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
presentation.save("background_fill.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果从左到右:背景填充、无填充和显式填充。品红色横幅被左侧和右侧矩形遮挡,但通过中间矩形可见。

相同的行为适用于直接在幻灯片上定义的背景或从布局继承的背景。形状使用幻灯片解析后的背景;示例中幻灯片和布局背景均未定义,以便由母版提供。
设置透明度
对于纯色填充,颜色的 alpha 分量控制不透明度:0 完全透明,255 完全不透明。此示例为黄色矩形设置 alpha 值 128,使其约 50% 透明,从而下方的矩形仍可见。颜色通过IFillFormat.getSolidFillColor 分配。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape solidShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
IAutoShape transparentShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 80, 80, 150, 75);
transparentShape.getFillFormat().setFillType(FillType.Solid);
transparentShape.getFillFormat().getSolidFillColor().setColor(Color.argb(128, 255, 255, 0));
presentation.save("shape_transparency.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

旋转形状
使用IShape.setRotation可在幻灯片平面上旋转形状。角度以度数表示;此示例将矩形顺时针旋转 5 度并保存演示文稿。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
shape.setRotation(5);
presentation.save("shape_rotation.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

添加 3D 倒角效果
使用IShape.getThreeDFormat 为形状添加深度和倒角。此示例创建一个绿色椭圆并使用橙色轮廓,调用IThreeDFormat.setDepth将深度设为 4 磅,并通过IThreeDFormat.getBevelTop 将顶部倒角设置为宽度和高度均为 6 磅的圆形。前置正交摄像机和三点光源使倒角可见。
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Ellipse, 50, 50, 100, 100);
shape.getFillFormat().setFillType(FillType.Solid);
shape.getFillFormat().getSolidFillColor().setColor(Color.rgb(0, 128, 0));
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
shape.getLineFormat().setWidth(2.0);
shape.getThreeDFormat().setDepth(4);
shape.getThreeDFormat().getBevelTop().setBevelType(BevelPresetType.Circle);
shape.getThreeDFormat().getBevelTop().setHeight(6);
shape.getThreeDFormat().getBevelTop().setWidth(6);
shape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.OrthographicFront);
shape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.ThreePt);
shape.getThreeDFormat().getLightRig().setDirection(LightingDirection.Top);
presentation.save("3D_bevel_effect.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

添加 3D 旋转效果
形状的 3D 视图由摄像机设置控制。此示例使用ICamera.setRotation将纬度、经度和旋转角度分别设为 40、35、20 度。通过ICamera.setCameraType 选择等轴摄像机,并通过ILightRig.setLightType 设定平衡光照,然后保存格式化后的矩形。
import com.aspose.slides.*;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape autoShape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 150, 75);
autoShape.getTextFrame().setText("Hello, Aspose!");
autoShape.getThreeDFormat().getCamera().setCameraType(CameraPresetType.IsometricLeftUp);
autoShape.getThreeDFormat().getCamera().setRotation(40, 35, 20);
autoShape.getThreeDFormat().getLightRig().setLightType(LightRigPresetType.Balanced);
presentation.save("3D_rotation_effect.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
结果如下:

控制形状的黑白渲染
IShape.setBlackWhiteMode 方法指定在黑白模式下查看或处理演示文稿时,单个形状的渲染方式。该方法本身并不启用黑白显示,也不会在普通彩色模式下改变形状的填充、线条或其他格式。
使用BlackWhiteMode 类中的值来选择所需行为。例如,Automatic 让渲染应用自行决定转换方式,Gray 和 LightGray 使用灰度,BlackWhite 仅使用黑白,Black 和 White 强制单色,Color 保持普通彩色,Hidden 在黑白模式下省略该形状。NotDefined 表示未分配形状级别的模式。
此示例创建一个橙色矩形,并使用IShape.setBlackWhiteMode 选择灰度着色。保存的形状在普通彩色显示下仍保持橙色填充,而在遵循黑白显示设置的应用程序中请求灰度着色:
import com.aspose.slides.*;
import android.graphics.Color;
Presentation presentation = new Presentation();
try {
ISlide slide = presentation.getSlides().get_Item(0);
IAutoShape shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 200, 100);
shape.getFillFormat().setFillType(FillType.Solid);
shape.getFillFormat().getSolidFillColor().setColor(Color.rgb(255, 165, 0));
shape.setBlackWhiteMode(BlackWhiteMode.Gray);
presentation.save("shape_black_white_mode.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
在普通彩色模式下,矩形保持橙色填充。在黑白显示工作流中,它使用灰度因为其模式已设为 Gray。此功能可在保留全彩幻灯片的同时,为打印、预览或其他遵循黑白显示设置的工作流定义不同的外观。
重置格式
使用ISlide.reset 可从对应的布局占位符恢复幻灯片占位符的位置、大小和格式。此示例需要一个已有的演示文稿,其中占位符已关联布局,并在保存前重置每张幻灯片。没有对应布局占位符的形状不会被重置。
import com.aspose.slides.*;
Presentation presentation = new Presentation("sample.pptx");
try {
for (ISlide slide : presentation.getSlides()) {
slide.reset();
}
presentation.save("reset_formatting.pptx", SaveFormat.Pptx);
} finally {
presentation.dispose();
}
常见问题
形状格式化会影响最终演示文稿的文件大小吗?
颜色、线条样式和效果设置相对于嵌入的图像和媒体通常只会增加很少的数据。图片填充可能会增加文件大小,因为其图像会存储在演示文稿中。
如何检测幻灯片上具有相同格式的形状,以便将它们分组?
比较每个形状的关键格式属性——填充、线条和效果设置。如果所有对应值匹配,则视为相同样式,可在逻辑上将这些形状分组,从而简化后续的样式管理。
是否可以将一组自定义形状样式保存到单独的文件,以便在其他演示文稿中重用?
可以。将带有所需样式的示例形状存储在模板幻灯片或 .POTX 模板文件中。创建新演示文稿时,打开模板,克隆所需的样式形状,并在需要的地方重新应用其格式。