简体   繁体   English

如何使用iText仅将用户密码而不将所有者密码添加到PDF

[英]How to add only user password without owner password to a PDF using iText

How to add only user password to the pdf file without adding the owner password using iText? 如何使用iText仅将用户密码添加到pdf文件而不添加所有者密码? As far as I can see, seems only can do this by adding an user password along with an owner password. 据我所知,似乎只能通过添加用户密码和所有者密码来做到这一点。

Encrypting an existing PDF is done like this: 加密现有的PDF是这样完成的:

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();
    reader.close();
}

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

If OWNER is null , then a random owner password will be generated (one that nobody knows). 如果OWNER为null ,那么将生成一个随机的所有者密码(没人知道的一个)。 Maybe that's what your customer means. 也许那就是您的客户的意思。 As mkl already explained, it doesn't really make sense to have a PDF with a USER password, but without an OWNER password. 正如mkl已经解释的那样,拥有一个带有USER密码但没有OWNER密码的PDF并没有什么意义。

The following code is what you need to add the owner password. 以下代码是添加所有者密码所需的内容。

  JRPdfExporter exporter = new JRPdfExporter();       
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperprintobj);
  exporter.setParameter(JRExporterParameter.OUTPUT_FILE,new File(fileName));
  exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE);
  exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
  //exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, "jasper");
  exporter.setParameter(JRPdfExporterParameter.OWNER_PASSWORD, "reports");
  exporter.setParameter(
    JRPdfExporterParameter.PERMISSIONS, 
    new Integer(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING)
    );
  exporter.exportReport(); // finally export as pdf

Use this code in java where you generate the pdf. 在生成pdf的java中使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用用户密码的itext pdf解密 - itext pdf decryption using user password 在iText中使用setEncryption方法时是否可以仅设置所有者密码? - Is it possible to set only owner password while using setEncryption method in iText? 如何使用itext打开受密码保护的pdf - How to open password protected pdf using itext 如何使用 iText 删除受密码保护的 pdf 中的密码 7 - How to remove password in password-protected pdf using iText 7 通过itext用用户密码解密受保护的pdf - decrypting protected pdf with user password by itext 如何使用 java8 和 iText 密码保护现有的 pdf 文件? - How to password protect a existing pdf file using java8 & iText? Itext Pdf 可以提取文本,但无法使用所有者密码从 pdf 复制页面? - Itext Pdf can extract text but can not copy page from pdf with owner password? 如果用用户密码和所有者密码加密,如何用用户密码解密Java中的128bit RC4 pdf文件 - How to decrypt 128bit RC4 pdf file in java with user password if it is encrypted with user as well as owner password 如何仅在JSP中添加使用iText生成的PDF中的页眉和页脚 - how to add header and footer in PDF to be generated using iText in JSP only 使用iText将用户输入添加到PDF表单 - Using iText to add user input to PDF form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM