本文为您提供了在 Java 中锁定 PowerPoint PPT 形状的综合指南。因此,您可以保护 PowerPoint 演示文稿的内容。出于多种原因,锁定形状可能很有用,包括防止意外更改、保护品牌标识、保持布局完整性等。那么,让我们继续看看如何在 Java 演示文稿中锁定或解锁形状。
Aspose.Slides 是一款 PowerPoint管理API,用于读取,编写,操作和转换PowerPoint幻灯片的独立API,可将PowerPoint转换为PDF,PDF/A,XPS,TIFF,HTML,ODP和其他PowerPoint格式。
Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
一、用于锁定 PowerPoint PPT 中形状的 Java 库
要锁定和解锁 PowerPoint 演示文稿,我们将使用Aspose.Slides for Java。它是一个功能丰富的 Java 库,用于创建和操作演示文稿文档。您可以下载该库或使用pom.xml中的以下依赖项进行安装。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>23.7</version>
<classifier>jdk16</classifier>
</dependency>
二、Java 中的 PowerPoint PPT 锁定形状
- PowerPoint 演示文稿由多种元素组成,例如文本、图像、音频等。Aspose.Slides for Java 将每个元素视为 Shape 或从 Shape 派生的 abject。因此,如果锁定演示文稿中的所有形状,就可以防止 PPT 被修改。
Aspose.Slides for Java 将 PowerPoint 形状分为以下类型:
- 自动形状
- 团体形态
- 连接器
- 镜框
- 图形对象
现在让我们看看如何用 Java 锁定 PowerPoint PPT 中的形状。
- 首先,使用Presentation类加载PPT/PPTX 文件。
- 然后,使用Presentation.getSlides()方法获取演示文稿中的幻灯片。
- 对于每张幻灯片,使用ISlide.getShapes()方法访问其形状。
- 对于集合中的每个形状,执行以下步骤:
- 检查形状的类型。
- 根据形状的类型使用合适的锁。
- 最后,使用Presentation.save(String, SaveFormat)方法保存演示文稿。
以下代码示例演示如何使用 Java 锁定 PowerPoint PPT 中的形状。
try {
//Load presentation file
Presentation pTemplate = new Presentation("presentation.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.getSlides().get_Item(0);
//IShape object for holding temporary shapes
IShape shape;
//Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++) {
slide = pTemplate.getSlides().get_Item(slideCount);
//Traverse through all the shapes in the slides
for (int count = 0; count < slide.getShapes().size(); count++) {
shape = slide.getShapes().get_Item(count);
//if shape is auto shape
if (shape instanceof IAutoShape) {
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape) shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Apply shape locks
AutoShapeLock.setPositionLocked(true);
AutoShapeLock.setSelectLocked(true);
AutoShapeLock.setSizeLocked(true);
}
//if shape is group shape
else if (shape instanceof IGroupShape) {
//Type casting to group shape and getting group shape lock
IGroupShape Group = (IGroupShape) shape;
IGroupShapeLock groupShapeLock = (IGroupShapeLock) Group.getShapeLock();
//Apply shape locks
groupShapeLock.setGroupingLocked(true);
groupShapeLock.setPositionLocked(true);
groupShapeLock.setSelectLocked(true);
groupShapeLock.setSizeLocked(true);
}
//if shape is a connector
else if (shape instanceof IConnector) {
//Type casting to connector shape and getting connector shape lock
IConnector Conn = (IConnector) shape;
IConnectorLock ConnLock = Conn.getShapeLock();
//Apply shape locks
ConnLock.setPositionMove(true);
ConnLock.setSelectLocked(true);
ConnLock.setSizeLocked(true);
}
//if shape is picture frame
else if (shape instanceof IPictureFrame) {
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = (IPictureFrame) shape;
IPictureFrameLock PicLock = (IPictureFrameLock) Pic.getShapeLock();
//Apply shape locks
PicLock.setPositionLocked(true);
PicLock.setSelectLocked(true);
PicLock.setSizeLocked(true);
}
}
}
//Save the presentation file
pTemplate.save("ProtectedSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}
三、在 Java 中解锁 PowerPoint 形状
要解锁 PowerPoint PPT 中锁定的形状,只需将其值设置为false来关闭锁定。值得注意的是,使用 Aspose.Slides for Java 锁定的形状无法使用任何其他库解锁。
以下代码示例演示如何使用 Java 解锁 PPTX 文件中的形状。
try {
//Load presentation file
Presentation pTemplate = new Presentation("presentation.pptx");
//ISlide object for accessing the slides in the presentation
ISlide slide = pTemplate.getSlides().get_Item(0);
//IShape object for holding temporary shapes
IShape shape;
//Traverse through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++) {
slide = pTemplate.getSlides().get_Item(slideCount);
//Traverse through all the shapes in the slides
for (int count = 0; count < slide.getShapes().size(); count++) {
shape = slide.getShapes().get_Item(count);
//if shape is auto shape
if (shape instanceof IAutoShape) {
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape) shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Unlock shape
AutoShapeLock.setPositionLocked(false);
AutoShapeLock.setSelectLocked(false);
AutoShapeLock.setSizeLocked(false);
}
//if shape is group shape
else if (shape instanceof IGroupShape) {
//Type casting to group shape and getting group shape lock
IGroupShape Group = (IGroupShape) shape;
IGroupShapeLock groupShapeLock = (IGroupShapeLock) Group.getShapeLock();
//Unlock shape
groupShapeLock.setGroupingLocked(false);
groupShapeLock.setPositionLocked(false);
groupShapeLock.setSelectLocked(false);
groupShapeLock.setSizeLocked(false);
}
//if shape is a connector
else if (shape instanceof IConnector) {
//Type casting to connector shape and getting connector shape lock
IConnector Conn = (IConnector) shape;
IConnectorLock ConnLock = Conn.getShapeLock();
//Unlock shape
ConnLock.setPositionMove(false);
ConnLock.setSelectLocked(false);
ConnLock.setSizeLocked(false);
}
//if shape is picture frame
else if (shape instanceof IPictureFrame) {
//Type casting to pitcture frame shape and getting picture frame shape lock
IPictureFrame Pic = (IPictureFrame) shape;
IPictureFrameLock PicLock = (IPictureFrameLock) Pic.getShapeLock();
//Unlock shape
PicLock.setPositionLocked(false);
PicLock.setSelectLocked(false);
PicLock.setSizeLocked(false);
}
}
}
//Save the presentation file
pTemplate.save("ProtectedSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}
以上便是如何在java中锁定PPT形状 ,如您还有关于产品相关方面的疑问,可以继续浏览本系列其他内容~
标签:Java,解锁,形状,shape,PPT,false,true,PowerPoint From: https://blog.51cto.com/u_15606885/7187522