【问题标题】:How to make pdf file password protected?如何使pdf文件密码保护?
【发布时间】:2014-09-05 08:04:51
【问题描述】:

我想让 pdf 文件有密码保护。我只是为了同样的目的而使用它,并在下面找到一个好的解决方案。它工作正常但是在我使用下面给定的代码保护 pdf 之后,它会清除我 pdf 中已经存在的所有数据。

此代码使用的 jar 文件是:

itextpdf-5.2.1.jar

bcmail-jdk16-1.46.jar

bcprov-jdk16-1.46.jar

bctsp-jdk16-1.46.jar

保护 PDF 的代码:

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

public class Secure_file {
    private static String USER_PASSWORD = "password";
    private static String OWNER_PASSWORD = "secured";
    public static void main(String[] args) throws IOException {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:\\sample.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();
        }
    }
}

我需要在这个程序中进行哪些更改?

【问题讨论】:

    标签: java pdf


    【解决方案1】:

    如果您查找iText in Action keywords,您会发现encryption 指向示例part3.chapter12.EncryptionPdf。该示例的方法 createPdf 基本上等同于您的代码,但方法 encryptPdf 是您想要的:

    /** User password. */
    public static byte[] USER = "Hello".getBytes();
    /** Owner password. */
    public static byte[] OWNER = "World".getBytes();
    
    ...
    
    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();
    }
    

    【讨论】:

    • 非常感谢。你让我今天一整天都感觉很好。它就像一个魅力。现在我想对 word、jpg、excel 等其他文件扩展名做同样的事情。那么有没有图书馆可以做同样的事情?
    • Word 和 excel 应该是可能的,可能使用 ms office interop 或一些非 ms 库。我怀疑 jpeg 是可能的。您应该将此作为明确的问题,而不是评论。
    【解决方案2】:

    使用 iText5 的示例。如果使用 iText7 非常相似,但使用另一个类而不是 stampler。

                    PdfReader reader = new PdfReader(dp.getStream());
                    File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
                    tempFile.deleteOnExit();
                    FileOutputStream os = new FileOutputStream(tempFile);
                    PdfStamper stamper = new PdfStamper(reader, os);
    
                    String pdfPassword = "1234"
                    String pdfAdminPassword = "5678"
                    stamper.setEncryption(
                            pdfPassword.getBytes(),
                            pdfAdminPassword.getBytes(),
                            PdfWriter.ALLOW_PRINTING,
                            PdfWriter.ENCRYPTION_AES_128);
    
                    reader.close();
                    stamper.close();
    
                    InputStream encryptedFileIs = new FileInputStream(tempFile);
    
    

    或 apache lib pdfbox

                    PDDocument document = PDDocument.load(dp.getStream());
                    AccessPermission ap = new AccessPermission();
                    StandardProtectionPolicy spp = new StandardProtectionPolicy("1234", "1234", ap);
                    spp.setEncryptionKeyLength(128);
                    spp.setPermissions(ap);
                    document.protect(spp);
    
                    File tempFile = File.createTempFile("someFilename", FILE_EXTENSION_PDF);
                    tempFile.deleteOnExit();
                    FileOutputStream os = new FileOutputStream(tempFile);
                    document.save(os);
                    document.close();
                    InputStream encryptedFileIs = new FileInputStream(tempFile);
    

    祝你好运,编码愉快:)

    【讨论】:

      【解决方案3】:
      stamper.setEncryption(USER, OWNER,PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
      

      我已使用此代码为 pdf 添加密码。打开pdf时会要求输入密码

      【讨论】:

        【解决方案4】:

        我用过 FOP 参考这个document

        FOUserAgent userAgent = fopFactory.newFOUserAgent();
        useragent.getRendererOptions().put("encryption-params", new PDFEncryptionParams(
                null, "password", false, false, true, true));
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent);
        

        【讨论】:

        • 操作员想要对已经存在的 PDF 进行密码保护,而不是他将要使用 FOP 创建的 PDF。
        猜你喜欢
        • 2012-09-05
        • 1970-01-01
        • 2017-11-17
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 2021-03-28
        相关资源
        最近更新 更多