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

Aspose.Slides for Java 提供的接口和方法使您能够使用 PowerPoint 中可用的相同选项来格式化形状。
格式化线条
使用IShape.getLineFormat来控制形状的轮廓。此示例创建了一个未填充的矩形,使用蓝色、虚线轮廓。ILineFormat.setStyle 选择粗细复合线,而ILineFormat.setWidth 将宽度设为 7 磅,ILineFormat.setDashStyle 选择虚线样式。
import com.aspose.slides.*;
import java.awt.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。
数组条目在虚线和间隙长度之间交替,先从虚线开始。每个值都是线宽的倍数:宽度为 3 磅时 { 4, 2 } 产生 12 磅的虚线和 6 磅的间隙。使用成对的有限、非负值来定义完整的虚线‑间隙段。示例使用正值以保证每段虚线和间隙均可见。
此示例创建了五条蓝色线,长度相同且端点平直。前三条在宽度为 3 磅时比较不同的模式。第四条在宽度为 6 磅时重复第一个模式,虚线和间隙长度均加倍。第五条从自定义模式切换为实线。
import com.aspose.slides.*;
import java.awt.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(new Color(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 磅。从上到下,它们分别使用 flat、round 和 square 端点。端点的水平坐标相同;round 和 square 端点在每个端点之外延伸 10 磅。
import com.aspose.slides.*;
import java.awt.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(new Color(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 的 miter、bevel 和 round 值分别设置,以比较拐角的绘制方式。
import com.aspose.slides.*;
import java.awt.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 java.awt.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(new Color(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 中的显示不同,请参阅Choose a Gradient Rendering Style。您可以在不更改演示文稿中存储的渐变填充的情况下选择渲染样式。
设置线性渐变角度
使用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 java.awt.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 java.awt.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 java.awt.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 java.awt.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.LIGHT_GRAY);
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% 重复。将 X、Y 偏移设为 -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 java.awt.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 java.awt.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(new Color(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(new Color(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。
在不同主题上下文之间复制时,例如在具有不同强调调色板的演示文稿之间复制,复制的引用会根据目标的主题进行解析。因此,同一引用和变换在不同目标主题下可能产生不同的可见颜色。更多关于配置主题颜色的信息,请参阅Presentation Theme。
使用幻灯片背景作为形状填充
当形状应与幻灯片背景保持一致时(例如在演示文稿模板中),请使用IAutoShape.setUseBackgroundFill。首先使用IFillFormat.setFillType 将填充类型设为FillType.NotDefined 以移除任何显式填充。这样组合后,形状将使用幻灯片背景而不是其样式填充。仅启用该标志并不会移除已有的显式填充。
这与FillType.NoFill 不同:背景填充会用幻灯片背景的对应区域遮盖形状后面的对象,而 NoFill 则让这些对象保持可见。渐变仍然相对于幻灯片对齐,而不是在每个形状内部重新开始。
下面的示例创建了一个由母版继承的背景,并在三个矩形后放置一条洋红色条带。左侧矩形使用背景填充,中间矩形无填充,右侧矩形从背景填充切换为显式的橙色填充。
import com.aspose.slides.*;
import java.awt.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, new Color(0, 0, 139));
gradient.getGradientStops().add(1, new Color(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(new Color(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 java.awt.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(new Color(255, 255, 0, 128));
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 java.awt.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(new Color(0, 128, 0));
shape.getLineFormat().getFillFormat().setFillType(FillType.Solid);
shape.getLineFormat().getFillFormat().getSolidFillColor().setColor(new Color(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 java.awt.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(new Color(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 模板文件中。创建新演示文稿时,打开模板,克隆所需的样式形状,并在需要的地方重新应用其格式。