itext Protecting your PDF

  1 /*
  2  * This class is part of the book "iText in Action - 2nd Edition"
  3  * written by Bruno Lowagie (ISBN: 9781935182610)
  4  * For more info, go to: http://itextpdf.com/examples/
  5  * This example only works with the AGPL version of iText.
  6  */
  7  
  8 package part3.chapter12;
  9  
 10 import java.io.FileOutputStream;
 11 import java.io.IOException;
 12  
 13 import com.itextpdf.text.Document;
 14 import com.itextpdf.text.DocumentException;
 15 import com.itextpdf.text.Paragraph;
 16 import com.itextpdf.text.pdf.PdfWriter;
 17 import com.itextpdf.text.pdf.PdfReader;
 18 import com.itextpdf.text.pdf.PdfStamper;
 19  
 20  
 21 public class EncryptionPdf {
 22     /** User password. */
 23     public static byte[] USER = "Hello".getBytes();
 24     /** Owner password. */
 25     public static byte[] OWNER = "World".getBytes();
 26  
 27     /** The resulting PDF file. */
 28     public static final String RESULT1
 29         = "results/part3/chapter12/encryption.pdf";
 30     /** The resulting PDF file. */
 31     public static final String RESULT2
 32         = "results/part3/chapter12/encryption_decrypted.pdf";
 33     /** The resulting PDF file. */
 34     public static final String RESULT3
 35         = "results/part3/chapter12/encryption_encrypted.pdf";
 36  
 37     /**
 38      * Creates a PDF document.
 39      * @param filename the path to the new PDF document
 40      * @throws DocumentException 
 41      * @throws IOException 
 42      */
 43     public void createPdf(String filename) throws IOException, DocumentException {
 44         // step 1
 45         Document document = new Document();
 46         // step 2
 47         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
 48         writer.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
 49         writer.createXmpMetadata();
 50         // step 3
 51         document.open();
 52         // step 4
 53         document.add(new Paragraph("Hello World"));
 54         // step 5
 55         document.close();
 56     }
 57  
 58     /**
 59      * Manipulates a PDF file src with the file dest as result
 60      * @param src the original PDF
 61      * @param dest the resulting PDF
 62      * @throws IOException
 63      * @throws DocumentException
 64      */
 65     public void decryptPdf(String src, String dest) throws IOException, DocumentException {
 66         PdfReader reader = new PdfReader(src, OWNER);
 67         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
 68         stamper.close();
 69         reader.close();
 70     }
 71  
 72     /**
 73      * Manipulates a PDF file src with the file dest as result
 74      * @param src the original PDF
 75      * @param dest the resulting PDF
 76      * @throws IOException
 77      * @throws DocumentException
 78      */
 79     public void encryptPdf(String src, String dest) throws IOException, DocumentException {
 80         PdfReader reader = new PdfReader(src);
 81         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
 82         stamper.setEncryption(USER, OWNER,
 83             PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
 84         stamper.close();
 85         reader.close();
 86     }
 87  
 88     /**
 89      * Main method.
 90      *
 91      * @param    args    no arguments needed
 92      * @throws DocumentException 
 93      * @throws IOException
 94      */
 95     public static void main(String[] args) throws IOException, DocumentException {
 96         EncryptionPdf metadata = new EncryptionPdf();
 97         metadata.createPdf(RESULT1);
 98         metadata.decryptPdf(RESULT1, RESULT2);
 99         metadata.encryptPdf(RESULT2, RESULT3);
100     }
101 }

http://itextpdf.com/examples/iia.php?id=219

原文地址:https://www.cnblogs.com/sunxucool/p/3821621.html