如何保护pdf文件密码?

时间:2014-09-05 08:04:51

标签: java pdf

我想要保护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();
        }
    }
}

我需要在此计划中做出哪些更改?

4 个答案:

答案 0 :(得分:3)

如果您查找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();
}

答案 1 :(得分:1)

使用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);

祝你好运,编码愉快:)

答案 2 :(得分:0)

stamper.setEncryption(USER, OWNER,PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

我已经使用此代码为pdf添加密码。打开pdf时会询问密码

答案 3 :(得分:0)

我使用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);