解密pdf-3(pdf文件加密)

本文介绍了一种使用Java实现PDF文档加密的方法,通过设置查看密码及权限限制,确保文档只能被授权用户打开并打印。文中提供了具体的代码示例,包括如何创建带有中文内容的基本PDF文档,并设置了两种密码以及文档权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你是否希望你的pdf 只有你本人才可以打开呢?
现在我们就来给pdf 加上查看密码 并进行一些权限限制

package test.pdf;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
* 加密文档,仅允许打印
*
*/
public class Simple3 {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws DocumentException, IOException {
Document doc = new Document();

PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream("E:/HelloWorld.pdf"));
int intPermissions = PdfWriter.ALLOW_PRINTING;
//要是用这个方法需要引入一个jar包(bcprov-jdk15-137.jar)。
//第一个参数:打开时需要的密码;第二个参数:实用其他其他权限时使用的密码;
//第三个参数:可使用的权限;第四个参数:密码类型
pdfWriter.setEncryption("123456".getBytes(), "789456".getBytes(), intPermissions, false);
//pdfWriter.setEncryption(null, null, intPermissions, false);

BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
doc.open();
Paragraph par = new Paragraph("你好!世界!!",fontChinese);

doc.add(par);
doc.close();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值