有没有人知道如何使用 openpdf java lib 为现有 pdf 设置密码?

Does any one have an idea about how to set password to existing pdf using openpdf java lib?

如何使用 OpenPdf java 库为现有 PDF 设置密码? 我试过下面的代码,但是创建了没有内容的新 pdf

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class PasswordProtectedPDF {
  public static final String ENCRYPTED_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
  // User and owner password
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      Document doc = new Document();
      PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(ENCRYPTED_PDF));
      // set password, user permissions and encryption
      writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 
      doc.open();
 
     
      doc.close();
      writer.close();
    } catch (DocumentException | FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

要为新 PDF 添加密码,我们使用 PdfWriter.setEncryption() 方法。

PdfStamper API 当我们需要保护现有的 PDF 时使用。在实例化 PdfStamper 时,它接受源文件作为 PdfReader,目标文件作为 OutputStream。 PdfStamper 可以在写入目标文件时在 PDF 中添加一些额外的内容。 PdfStamper.setEncryption 在为现有 PDF 设置加密时使用与 PdfWriter.setEncryption 相同的参数。

已更新您的代码以使用 PDFStamper 而不是 PDFWriter。


    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfStamper;
    
    public class PasswordProtectedPDF {
      // User and owner password
      final static String USER_PASSWORD = "user";
      final static String OWNER_PASSWORD = "owner";
      public static void main(String[] args) {
        try {
          File f = new File("F://knpcode//result//OpenPDF//ENCRYPTED_PP.pdf");
          FileOutputStream out = new FileOutputStream(f);
          PdfReader reader = new PdfReader("F://knpcode//result//OpenPDF//PP.pdf");
          PdfStamper stamper = new PdfStamper(reader, out);
          
          // set password, user permissions and encryption
          stamper.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

          // Don't forget to add this line as no bytes are written to that output stream up until you close the PdfStamper instance. 
          stamper.close();

        } catch ( IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }

使用 Google“java openpdf 密码示例”我找到了这个网站:https://knpcode.com/java-programs/password-protected-pdf-using-openpdf-java/,其中包含有关您的问题的一些信息。

这里有两个示例程序(只是 copy/pasted 它们,不是测试)。不要忘记 包括 Bouncy Castle 作为安全提供程序。

加密(“保护”)PDF:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class PasswordProtectedPDF {
  public static final String ENCRYPTED_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
  // User and owner password
  final static String USER_PASSWORD = "user";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    try {
      Document doc = new Document();
      PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(ENCRYPTED_PDF));
      // set password, user permissions and encryption
      writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 
      doc.open();
      Paragraph para = new Paragraph("Password protected PDF where only content printing is permitted content can't be copied.");
      doc.add(para);
      doc.close();
      writer.close();
    } catch (DocumentException | FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

使用 OpenPDF 阅读受密码保护的 PDF:

import java.io.IOException;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.parser.PdfTextExtractor;

public class ReadPDF {
  // PDF to be read
  public static final String READ_PDF = "F://knpcode//result//OpenPDF//PP.pdf";
  final static String OWNER_PASSWORD = "owner";
  public static void main(String[] args) {
    PdfReader pdfreader = null;
    try {
      pdfreader = new PdfReader(READ_PDF, OWNER_PASSWORD.getBytes());
      // get pages in PDF
      int pages = pdfreader.getNumberOfPages();
      PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfreader);
      // Iterate through pages to read content
      for(int i = 1; i <= pages; i++) {
        // Extract content of each page
        String contentOfPage = pdfTextExtractor.getTextFromPage(i, true);
        System.out.println(contentOfPage );
      }         
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally {
      if(pdfreader != null) {
        pdfreader.close();
      }
    }   
  }
}