当图片覆盖在文字的上方,文本会被图片遮盖从而无法阅读。这时,您可以通过设置图片的透明度来显示文字。本文将介绍怎样使用 spire.presentation for java 将图片插入到幻灯片的特定位置,并通过 java 设置 powerpoint 中图片的透明度。
安装 spire.presentation for java
首先,您需要在 java 程序中添加 spire.presentation.jar 文件作为依赖项。jar 文件可以从此链接下载。如果您使用 ,则可以将以下代码添加到项目的 pom.xml 文件中,从而轻松地在应用程序中导入 jar 文件。
com.e-iceblue
e-iceblue
https://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.presentation
10.2.2
设置 powerpoint 中图像的透明度
在此代码示例中涉及到多个类和方法,因此我们提供了下表,方便您更好地了解它们。
名字 | 释义 |
iautoshape interface | 表示形状 |
shapelist class | 表示形状的集合 |
picturefillformat class | 表示图片填充的格式 |
shapelist.appendshape(shapetype.shapetype, rectangle2d.double) method | 添加新图片到列表 |
iautoshape.getline().setfilltype(fillformattype.value) method | 设置线条的填充格式 |
iautoshape.getfill().setfilltype(fillformattype.value) method | 设置形状的填充格式 |
iautoshape.getfill().getpicturefill() method | 获取图片填充格式 |
picturefillformat.setfilltype(picturefilltype.value) method | 设置图片填充模式 |
picturefillformat.getpicture().set method | 设置填充图像的图片的 url |
picturefillformat.getpicture().settransparency() method | 设置图片填充的透明度 |
下面是设置 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) 方法设置图片的填充格式,使用 picturefillformat.getpicture().set 方法设置图片的 url,通过 picturefillformat.getpicture().settransparency() 方法设置图像的透明度。
- 利用 presentation.savetofile() 方法保存修改后的文档。
- java
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 {
// 创建一个 presentation 类的实例
presentation presentation = new presentation();
// 加载 powerpoint 示例文档
presentation.loadfromfile("/空白幻灯片.pptx");
// 在第一页幻灯片的指定位置插入一个形状
rectangle2d.double rect1 = new rectangle2d.double(50, 130, 275, 150);
iautoshape shape = presentation.getslides().get(0).getshapes().appendshape(shapetype.rectangle, rect1);
// 用图片填充这个形状
shape.getline().setfilltype(fillformattype.none);// 设置填充格式
shape.getfill().setfilltype(fillformattype.picture);// 设置填充的类型
shape.getfill().getpicturefill().getpicture().set;// 设置链接图片的 url
shape.getfill().getpicturefill().setfilltype(picturefilltype.stretch);// 设置图片填充模式
// 设置图片的透明度
shape.getfill().getpicturefill().getpicture().settransparency(50);
// 保存修改后的 powerpoint 文档
presentation.savetofile("/设置图片透明度.pptx", fileformat.pptx_2010);
}
}
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。