Java 怎么加密PDF

2019年7月29日20:00:21Java 怎么加密PDF已关闭评论 937 1750字阅读5分50秒

1.简介

Java 怎么加密PDF?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class EncrypPDF {

    public static byte[] USER = "Hello".getBytes();

    public static byte[] OWNER = "World".getBytes();

    public static final String RESULT1 = "E:/Movies/encryption.pdf";

    public static final String RESULT2 = "E:/Movies/encryption_decrypted.pdf";

    public static final String RESULT3 = "E:/Movies/encryption_encrypted.pdf";

    public void createPdf(String filename) throws IOException,
            DocumentException {

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(filename));
        writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING,
                PdfWriter.STANDARD_ENCRYPTION_128);
        writer.createXmpMetadata();

        document.open();

        document.add(new Paragraph("Encryption Needed"));

        document.close();
    }

    public void decryptPdf(String src, String dest) throws IOException,
            DocumentException {
        PdfReader reader = new PdfReader(src, OWNER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                dest));
        stamper.close();
    }

    public void encryptPdf(String src, String dest) throws IOException,
            DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                dest));
        stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING,
                PdfWriter.ENCRYPTION_AES_128
                        | PdfWriter.DO_NOT_ENCRYPT_METADATA);
        stamper.close();
    }

    public static void main(String[] args) throws IOException,
            DocumentException {
        EncrypPDF metadata = new EncrypPDF();
        metadata.createPdf(RESULT1);
        metadata.decryptPdf(RESULT1, RESULT2);
        metadata.encryptPdf(RESULT2, RESULT3);
    }
}

 
继续阅读
编程十万个怎么办