当前位置:   article > 正文

java加密保护text_java使用itext为pdf文件设置密码保护

writer.setencryption

我们可以使用itext的PdfWriter类的setEncryption方法来为pdf文件设置密码

package com.open.pdf;

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStream;

import java.util.Date;

import com.itextpdf.text.Document;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {

private static String USER_PASS = "Hello123";

private static String OWNER_PASS = "Owner123";

public static void main(String[] args) {

try {

OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, file);

writer.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(),

PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

document.open();

document.add(new Paragraph("Hello World, iText"));

document.add(new Paragraph(new Date().toString()));

document.close();

file.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/202774
推荐阅读
相关标签
  

闽ICP备14008679号