2

我们需要使用密码保护 PDF 文件。在这方面是否有任何基于 Java 的开源工具可以帮助我们?

4

4 回答 4

4

您可以轻松地在 java 中制作受密码保护的 pdf 文件......为此,您需要两个额外的 jar/lib bctsp-jdk16-1.46.jar 和 bcprov-jdk16-1.46.jar 以及 itextpdf-5.2。 1.罐子。
从这里下载所有 jars下载 Jars

下面也是代码片段

private static String USER_PASSWORD = "password";
private static String OWNER_PASSWORD = "naveen";
public static void main(String[] args) throws IOException {

    Document document = new Document();
      try
      {

         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\HelloWorld.pdf"));
         writer.setEncryption(USER_PASSWORD.getBytes(),OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);
         document.open();
         document.add(new Paragraph("This is Password Protected PDF document."));
         document.close();
         writer.close();
      } catch (DocumentException e)
      {
         e.printStackTrace();
      } catch (FileNotFoundException e)
      {
         e.printStackTrace();
      }
}
于 2014-08-05T18:58:16.817 回答
3

我建议使用iText java PDF 库。

在 iText 中,有一个名为PdfEncrypter的类,它可以让您使用密码保护 PDF 文件。

于 2010-01-09T05:01:08.270 回答
0

FOP library also allows encryption:

http://xmlgraphics.apache.org/fop/0.94/pdfencryption.html

于 2010-01-09T08:17:50.807 回答
0

你可以用iText PDF for java 来做到这一点:

一些例子:

http://1t3xt.info/examples/browse/?page=example&id=42

于 2010-01-09T04:57:39.770 回答