100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java对word Excel PPT PDF文件加密

java对word Excel PPT PDF文件加密

时间:2020-07-15 07:16:07

相关推荐

java对word Excel PPT PDF文件加密

java对word、Excel、PPT、PDF文件加密

所需依赖:Spire.Office.jar

说明:该解决方案使用spire.office(免费版)该版本有使用限制谨慎使用对于小文件来说足够了

package com.zxl.demo.encrypt;import com.spire.doc.Document;import com.spire.pdf.PdfDocument;import com.spire.pdf.security.PdfEncryptionKeySize;import com.spire.pdf.security.PdfPermissionsFlags;import com.spire.presentation.FileFormat;import com.spire.presentation.Presentation;import com.spire.xls.Workbook;import java.util.EnumSet;/*** @Describe: 文件加密工具类* @Author: zml* @Date: -5-6 15:53:01*/public class OfficeEncrypyUtils {public static void main(String[] args) {String filePath = "D:\\liang\\office加密\\xls加密-前.xls";String savePath = "D:\\liang\\office加密\\xls加密-后.xls";String password = "123";// wordEncrypt(filePath,savePath,password);// excelEncrypt(filePath,savePath,password);// pdfEncrypt(filePath,savePath,password);excelEncrypt(filePath,savePath,password);}/*** 对word文件加密*/private static void wordEncrypt(String filePath, String savePath, String password){try{Document document = new Document();document.loadFromFile(filePath);document.encrypt(password);document.saveToFile(savePath);}catch (Exception e){e.printStackTrace();}}/*** 对excel文件加密*/private static void excelEncrypt(String filePath, String savePath, String password){try{Workbook workbook = new Workbook();workbook.loadFromFile(filePath);workbook.protect(password);workbook.saveToFile(savePath);}catch (Exception e){e.printStackTrace();}}/*** 对pdf文件加密*/private static void pdfEncrypt(String filePath, String savePath, String password){try{PdfDocument pdfDocument = new PdfDocument();pdfDocument.loadFromFile(filePath);//对文件进行加密PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;String openPassword = "e-iceblue";EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);pdfDocument.getSecurity().encrypt(openPassword, password, flags, keySize);//保存文件pdfDocument.saveToFile(savePath);pdfDocument.close();}catch (Exception e){e.printStackTrace();}}/*** 对ppt文件加密*/private static void pptEncrypt(String filePath, String savePath, String password){try{Presentation presentation = new Presentation();presentation.loadFromFile(filePath);//设置密码presentation.encrypt(password);//保存文档presentation.saveToFile(savePath, FileFormat.PPT);}catch (Exception e){e.printStackTrace();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。