iText 加密 pdf 权限

3 篇文章 0 订阅
3 篇文章 0 订阅

翻了下官方api 发现权限中没有注释,而 itext in Action 第二版中有记录,在此记录下,以备查询
itext in Action P338

PdfWriter中的权限说明
ALLOW_PRINTING“The user is permitted to print the document.文档允许打印”
ALLOW_DEGRADED_PRINTING“The user is permitted to print the document, but not with the quality offered by ALLOW_PRINTING (for 128-bit encryption only).允许用户打印文档,但不提供allow_printing质量(128位加密)。”
ALLOW_MODIFY_CONTENTS“The user is permitted to modify the contents—for example, tochange the content of a page, or insert or remove a page.允许用户修改内容,例如 更改页面内容,或插入或删除页。”
ALLOW_ASSEMBLY“The user is permitted to insert, remove, and rotate pages andadd bookmarks. The content of a page can’t be changed unlessthe permission ALLOW_MODIFY_CONTENTS is granted too (for128-bit encryption only).允许用户插入、删除和旋转页面和添加书签。页面的内容不能更改,除非也授予allow_modify_contents权限”
ALLOW_COPY“The user is permitted to copy or otherwise extract text and graphics from the document, including using assistive technologiessuch as screen readers or other accessibility devices.允许用户复制或以其他方式从文档中提取文本和图形,包括使用辅助技术。例如屏幕阅读器或其他可访问设备。”
ALLOW_SCREENREADERS“The user is permitted to extract text and graphics for use by accessibility devices (for 128-bit encryption only).允许用户提取文本和图形以供易访问性设备使用”
ALLOW_MODIFY_ANNOTATIONS“The user is permitted to add or modify text annotations and interactive form fields.允许用户添加或修改文本注释和交互式表单字段。”
ALLOW_FILL_IN“The user is permitted to fill form fields (for 128-bit encryptiononly)允许用户填写表单字段”
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中接收MultipartFile图片并将其转换为PDF,并且使用iTextPDFPDF进行加密,你可以按照以下步骤进行操作: 1. 首先,确保你的项目中已经添加了iTextPDF的相关依赖。你可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.2</version> </dependency> ``` 2. 在你的控制器或服务类中,接收MultipartFile图片并将其转换为PDF,然后使用iTextPDFPDF进行加密,如下所示: ```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; @RestController public class FileController { @PostMapping("/convertAndEncrypt") public void convertAndEncrypt(@RequestParam("file") MultipartFile file) { try { // 将MultipartFile转换为InputStream InputStream inputStream = file.getInputStream(); // 创建PDF文档 Document document = new Document(); OutputStream outputStream = new FileOutputStream("converted.pdf"); PdfWriter writer = PdfWriter.getInstance(document, outputStream); // 打开文档 document.open(); // 将图片写入PDF文档 document.add(com.itextpdf.text.Image.getInstance(inputStream)); // 关闭文档 document.close(); // 使用iTextPDFPDF进行加密 PDFEncryptionUtil.encryptPdf("converted.pdf", "encrypted.pdf", "userPassword", "ownerPassword"); // 处理加密后的PDF文件,例如保存到本地或者返回给前端 } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 创建一个名为`PDFEncryptionUtil`的工具类,并在其中实现对PDF文件的加密,如下所示: ```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; import java.io.OutputStream; public class PDFEncryptionUtil { public static void encryptPdf(String inputFilePath, String outputFilePath, String userPassword, String ownerPassword) throws Exception { PdfReader reader = new PdfReader(inputFilePath); OutputStream outputStream = new FileOutputStream(outputFilePath); PdfStamper stamper = new PdfStamper(reader, outputStream); stamper.setEncryption(userPassword.getBytes(), ownerPassword.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); stamper.close(); reader.close(); outputStream.close(); } } ``` 在上述代码中,`convertAndEncrypt`方法接收一个MultipartFile参数,将其转换为PDF并保存到本地,然后调用`PDFEncryptionUtil`工具类对生成的PDF文件进行加密。 请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。 希望对你有所帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值