应用保护到演示文稿

幻灯片的组成

PPTX 幻灯片由多个组件组成,如自动图形、表格、OLE 对象、分组形状、图框、视频框、连接器以及可用于构建演示文稿的各种其他元素。在 Aspose.Slides for Java 中,幻灯片上的每个元素都被转化为 Shape 对象。换句话说,幻灯片上的每个元素要么是 Shape 对象,要么是从 Shape 对象派生的对象。PPTX 的结构复杂,所以与 PPT 不同,后者可以对所有类型的形状使用通用锁,PPTX 对于不同类型的形状有不同的锁。BaseShapeLock 类是通用的 PPTX 锁定类。Aspose.Slides for Java 支持以下类型的锁:

  • AutoShapeLock 锁定自动图形。
  • ConnectorLock 锁定连接器形状。
  • GraphicalObjectLock 锁定图形对象。
  • GroupshapeLock 锁定组合形状。
  • PictureFrameLock 锁定图框。 在一个演示文稿对象中对所有 Shape 对象执行的任何操作都适用于整个演示文稿。

应用和移除保护

应用保护确保演示文稿无法被编辑。这是保护演示文稿内容的一个有用技术。

将保护应用于 PPTX 形状

Aspose.Slides for Java 提供了 Shape 类来处理幻灯片上的形状。

如前所述,每个形状类都有一个相关的形状锁类用于保护。本文重点介绍 NoSelect、NoMove 和 NoResize 锁。这些锁确保形状无法被选择(通过鼠标点击或其他选择方法),并且无法移动或调整大小。

以下代码示例将保护应用于演示文稿中的所有形状类型。

try {
//Instatiate Presentation class that represents a PPTX file
Presentation pTemplate = new Presentation("RectPicFrame.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;
//Traversing through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++)
{
slide = pTemplate.getSlides().get_Item(slideCount);
//Travesing 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 autoshape
if (shape instanceof IAutoShape)
{
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape)shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Applying shapes 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();
//Applying shapes 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();
//Applying shapes 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();
//Applying shapes locks
PicLock.setPositionLocked(true);
PicLock.setSelectLocked(true);
PicLock.setSizeLocked(true);
}
}
}
//Saving the presentation file
pTemplate.save("ProtectedSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}

移除保护

使用 Aspose.Slides for .NET/Java 应用的保护只能通过 Aspose.Slides for .NET/Java 移除。要解锁形状,请将分配的锁的值设置为 false。以下代码示例演示了如何在被锁定的演示文稿中解锁形状。

try {
//Instatiate Presentation class that represents a PPTX file
Presentation pTemplate = new Presentation("ProtectedSample.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;
//Traversing through all the slides in the presentation
for (int slideCount = 0; slideCount < pTemplate.getSlides().size(); slideCount++)
{
slide = pTemplate.getSlides().get_Item(slideCount);
//Travesing 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 autoshape
if (shape instanceof IAutoShape)
{
//Type casting to Auto shape and getting auto shape lock
IAutoShape Ashp = (IAutoShape)shape;
IAutoShapeLock AutoShapeLock = (IAutoShapeLock) Ashp.getShapeLock();
//Applying shapes locks
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();
//Applying shapes locks
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();
//Applying shapes locks
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();
//Applying shapes locks
PicLock.setPositionLocked(false);
PicLock.setSelectLocked(false);
PicLock.setSizeLocked(false);
}
}
}
//Saving the presentation file
pTemplate.save("RemoveProtectionSample.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}

总结