首页 > 编程语言 > Java 中锁定和解锁PPT内容形状

Java 中锁定和解锁PPT内容形状

时间:2023-08-22 11:37:43浏览次数:70  
标签:Java 解锁 形状 shape PPT false true PowerPoint

本文为您提供了在 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 锁定形状
  1. 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

相关文章

  • java Swing 有没有文件夹选择器?
    这两天做一个图形化软件需要选择文件并进行文件读取,因此需要使用到JAVASpring中的文件选择框。参考:https://blog.51cto.com/u_92655/6514099参考2:https://blog.csdn.net/shalimu/article/details/128145834如果要选择文件夹,则需要设置参数:......
  • java笔试手写算法面试题大全含答案
    1.统计一篇英文文章单词个数。publicclassWordCounting{publicstaticvoidmain(String[]args){try(FileReaderfr=newFileReader("a.txt")){intcounter=0;booleanstate=false;intcurrentChar;while((currentChar=fr.read())!=-1){if(currentChar=='......
  • java笔试手写算法面试题大全含答案
    1.统计一篇英文文章单词个数。publicclassWordCounting{publicstaticvoidmain(String[]args){try(FileReaderfr=newFileReader("a.txt")){intcounter=0;booleanstate=false;intcurrentChar;while((currentChar=fr.read())!=-1){if(currentChar==�......
  • Java后端使用HttpServleRequest接收前端JSON传参
    BufferedReaderbf=newBufferedReader(newInputStreamReader((ServletInputStream)request.getInputStream(),"utf-8"));StringBuffersb=newStringBuffer();Stringtemp="";while((temp=bf.readLine())!=null){sb.append(temp);}b......
  • java实现大文件上传源码
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。 首先我们需要了解的是上传文件三要素:1.表单提交方式:post(get方式提交有大小限制,post没有)2.表单的enctype属性:必须设置为multipart/form-data.3.表单......
  • java 读取各种类型的文件 (一)
    Java可以用不同的库来读取各种格式的文档。下面是一些常见的文档格式以及相应的Java库:CSV文件:使用ApacheCommonsCSV库来读取和写入CSV文件。该库提供了一个简单易用的API。Excel文件(xls和xlsx):可以使用ApachePOI库来读取和写入Excel文件。该库支持旧版的xls文件和新版的xl......
  • 【校招VIP】java语言考点之垃圾回收算法
    考点介绍:垃圾回收算法是必考题。GC中的垃圾指的是存在于内存中的、不会再被使用的对象。而垃圾回收就是把那些不再被使用的对象进行清除,收回占用的内存空间......一、考点题目1、java中如何判断对象是否是垃圾?解答:引用计数:在对象中添加一个引用计数器,如果被引用计数器加1,引用......
  • java Swing:对话框(Dialog)怎么做?
    对话框是桌面应用程序中一种很重要的组件,例如在删除一个文件的时候,系统就会用对话框的形式提醒用户是否确定要删除这个文件。Swing体系中有两类对话框:第一类是系统对话框,第二类是自定义对话框。系统对话框就是Swing体系已经定义好的对话框,它们的基本界面是Java基础类库已经设计好......
  • (随笔)关于java自动以unix时间戳格式接收Date数据类型导致的sql查询时间失误问题的相
    前言​ 在进行项目开发的时候,有遇到过mapper执行sql语言查询指定时间范围内或截止时间之前的数据时不查询当天时间内不同时分秒时的数据,接口实现逻辑为前端传入Date类型(精度为yyyy-MM-dd)起止时间,mapper.xml文件中通过concat_ws方法实现时间精度转换(数据库内数据精度为yyyy-MM-dd......
  • 12 JavaScript 关于eval函数
    12eval函数eval本身在js里面正常情况下使用的并不多.但是很多网站会利用eval的特性来完成反爬操作.我们来看看eval是个什么鬼?从功能上讲,eval非常简单.它和python里面的eval是一样的.它可以动态的把字符串当成js代码进行运行.s="console.log('我爱你')";eval(s);也......