在Java中使用iText进行密码保护的PDF

时间:2020-01-09 10:35:35  来源:igfitidea点击:

在本文中,我们将介绍如何使用iText库在Java中创建受密码保护的PDF,以及如何使用密码保护已经存在的PDF。

使用iText的受密码保护的PDF

为了创建加密的PDF,需要执行以下步骤。

  • 使用WriterProperties类的setStandardEncryption()方法,可以设置文档的加密选项。
  • EncryptionConstants类中提供了加密常数。 EncryptionConstants类中提供的权限和加密算法。 ALLOW_ASSEMBLY
  • ALLOW_COPY
  • ALLOW_DEGRADED_PRINTING
  • ALLOW_FILL_IN
  • ALLOW_MODIFY_ANNOTATIONS
  • ALLOW_MODIFY_CONTENTS
  • ALLOW_PRINTING
  • ALLOW_SCREENREADERS
  • ENCRYPTION_AES_128
  • ENCRYPTION_AES_256
  • STANDARD_ENCRYPTION_128
  • STANDARD_ENCRYPTION_40
  • 为了加密PDF,我们还需要Bouncy Castle Provider。 Bouncy Castle Crypto软件包是密码算法的Java实现。 Maven依赖对于相同的是
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15on</artifactId>
  <version>1.61</version>
</dependency>

使用iText Java程序进行密码保护的PDF

import java.io.IOException;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

public class ReadPDF {
  public static final String CREATED_PDF = "F://theitroad//result//PP.pdf";
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      PdfWriter writer = new PdfWriter(CREATED_PDF, new WriterProperties()
                    .setStandardEncryption(USER_PASSWORD.getBytes(), 
                    OWNER_PASSWORD.getBytes(), 
                    EncryptionConstants.ALLOW_PRINTING, 
                    EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
      PdfDocument pdf = new PdfDocument(writer);	
      Document document = new Document(pdf); 
      document.add(new Paragraph("Password protected PDF where only content printing is permitted content can't be copied."));
      document.close();		
    }catch (IOException e) {
      System.out.println("Exception occurred " + e.getMessage());
    }
  }
}

如我们所见,打开PDF需要密码。打开PDF后,我们可以检查是否无法复制PDF的内容。

密码保护已经存在的PDF

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;

public class ReadPDF {
  public static final String SOURCE_PDF = "F://theitroad//result//List.pdf";
  public static final String RESULT_PDF = "F://theitroad//result//ListEncrypted.pdf";
  static final String USER_PASSWORD = "user";
  static final String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      // Read existing PDF
      PdfReader reader = new PdfReader(SOURCE_PDF);
      WriterProperties props = new WriterProperties().setStandardEncryption(USER_PASSWORD.getBytes(), 
                          OWNER_PASSWORD.getBytes(), 
                          EncryptionConstants.ALLOW_PRINTING,
                          EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA);
      // Write to PDF along with encryption properties
      PdfWriter writer = new PdfWriter(new FileOutputStream(RESULT_PDF), props);
      PdfDocument pdfDoc = new PdfDocument(reader, writer);
      pdfDoc.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}