Spire.Presentation for Java专业的 PowerPoint API,它允许开发人员在 Java 应用程序中创建、读取、写入、转换和保存 PowerPoint 文档,而无需安装 Microsoft PowerPoint。如有需要,可以进入慧都下载最新版!
当图片出现在文字上时,文字会被图片覆盖而无法显示。此时,您可以设置图片的透明度,以便在图片上方显示文字。本文将演示如何使用Spire.Presentation for Java将图像插入到 PowerPoint 中的特定幻灯片位置并设置其透明度。
在 PowerPoint 中设置图像的透明度
本代码示例中涉及到了大量的类和方法,为了方便大家了解,特提供下表。
姓名 | 描述 |
---|---|
IAutoShape 界面 | 代表一个形状。 |
形状列表类 | 表示形状的集合。 |
图片填充格式类 | 表示图片填充样式。 |
ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double) 方法 | 将新形状添加到列表中。 |
IAutoShape.getLine().setFillType(FillFormatType.value) 方法 | 设置线条的填充格式类型。 |
IAutoShape.getFill().setFillType(FillFormatType.value) 方法 | 设置形状的填充格式类型。 |
IAutoShape.getFill().getPictureFill() 方法 | 获取图片填充格式。 |
PictureFillFormat.setFillType(PictureFillType.value) 方法 | 设置图片填充模式。 |
PictureFillFormat.getPicture().setUrl(java.lang.String value) 方法 | 为图片填充设置图像的 URL。 |
PictureFillFormat.getPicture().setTransparency() 方法 | 设置图片填充的透明度。 |
以下是在 PowerPoint 中设置图像透明度的一些步骤:
- 创建一个Presentation实例并使用Presentation.loadFromFile()方法加载一个 PowerPoint 示例文档。
- 使用Presentation.getSlides().get()方法获取指定的幻灯片,并使用ShapeList.appendShape(ShapeType.shapeType, Rectangle2D.Double)方法将形状插入到幻灯片的指定位置。
- 使用图像填充形状并使用IAutoShape.getFill().setFillType(FillFormatType.value)方法设置填充格式类型。
- 使用IAutoShape.getFill().getPictureFill()方法获取图片填充格式。
- 设定用图像填充模式PictureFillFormat.setFillType(PictureFillType.value)方法中,设置使用该图像的URL PictureFillFormat.getPicture()。setUrl(java.lang.String中值)的方法,并设置使用该图像的透明度PictureFillFormat.getPicture( ).setTransparency()方法。
- 使用Presentation.saveToFile()方法保存文档。
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.geom.Rectangle2D;
public class SetImageTransparency {
public static void main(String[] args) throws Exception {
//Create a Presentation instance
Presentation presentation = new Presentation();
//Load a PowerPoint sample document
presentation.loadFromFile("C:\Users\Test1\Desktop\sample.pptx");
//Insert a shape to the specified position of the first slide
Rectangle2D.Double rect1 = new Rectangle2D.Double(50, 130, 275, 150);
IAutoShape shape = presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rect1);
//Fill the shape with an image
shape.getLine().setFillType(FillFormatType.NONE);//Sets the fill format type
shape.getFill().setFillType(FillFormatType.PICTURE);//Sets the type of filling
shape.getFill().getPictureFill().getPicture().setUrl("C:\Users\Test1\Desktop\Picture.png");//Sets the linked image's URL
shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);//Sets the picture fill mode
//Set transparency of the image
shape.getFill().getPictureFill().getPicture().setTransparency(50);
//Save the document to file
presentation.saveToFile("output/SetImageTransparency_result.pptx", FileFormat.PPTX_2010);
}
}