Java readAllBytes 从文件中删除由 PDFBox StandardProtectionPolicy 完成的所有者密码保护

问题描述 投票:0回答:1

我已经使用 PDFBox StandardProtectionPolicy 对 pdf 文档设置了所有者密码保护(允许阅读而不是编辑),当我检查保存的最终文档时,所有者密码保护存在,但是当我从保存的密码保护文档中读取字节时,它会失去所有密码保护,不明白为什么...!

我的代码:

PDDocument pdDocument = PDDocument.load(new File(inputPdf));
     AccessPermission accessPermission = new AccessPermission();
     accessPermission.setCanModify(false);
     accessPermission.setCanPrint(false); //Disable printing
     accessPermission.setCanExtractContent(false);
     accessPermission.setCanPrintDegraded(false);
     accessPermission.setReadOnly();
     StandardProtectionPolicy spp = new StandardProtectionPolicy(password, "", accessPermission);
     spp.setEncryptionKeyLength(128);
     pdDocument.protect(spp);
     pdDocument.save(new java.io.File(outputPdf));
     //Close the document
     pdDocument.close();

outputPdf 按预期受所有者密码保护,但 readAllbytes 删除了所有保护!

我也应用了文件级写保护但没有效果:

File file = new File(outputPdf);
     //Setting permissions to the file
     file.setReadable(true); //read
     file.setWritable(false); //write
     file.setExecutable(true);

需要将上述文件的字节返回给调用者:

return Files.readAllBytes(Path.of(outputPdf);
     //removes all owner password protections

感谢帮助 - 如何在不删除保护的情况下返回字节......?!

java pdfbox
1个回答
0
投票

以下是如何使用 PDFBox 打开受密码保护的 PDF 的示例:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;

import java.io.File;
import java.io.IOException;

public class PdfPasswordReader {
    public static void main(String[] args) {
        String inputPdf = "path/to/password-protected.pdf";
        String password = "your_owner_password";

        try (PDDocument pdDocument = PDDocument.load(new File(inputPdf), password)) {
            if (pdDocument.isEncrypted()) {
                pdDocument.setAllSecurityToBeRemoved(false);
                pdDocument.getDocument().setIsXRefStream(false);
                pdDocument.save(new File("path/to/unlocked.pdf"));
            }

            // Now you can read or manipulate the document as needed
            PDDocumentInformation info = pdDocument.getDocumentInformation();
            System.out.println("Title: " + info.getTitle());
            // Add more operations as needed
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

当您使用 Files.readAllBytes(Path.of(outputPdf)) 时,您直接读取文件,而不考虑 PDFBox 加密。为了保持加密,您应该使用 PDFBox 加载文档,然后将其内容写入字节数组。以下是如何实现这一目标的示例:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class PdfByteReader {
    public static byte[] readPdfBytes(String inputPdf, String password) {
        try (PDDocument pdDocument = PDDocument.load(new File(inputPdf), password)) {
            if (pdDocument.isEncrypted()) {
                pdDocument.setAllSecurityToBeRemoved(false);
                pdDocument.getDocument().setIsXRefStream(false);
            }

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            pdDocument.save(byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new byte[0];
    }

    public static void main(String[] args) {
        String inputPdf = "path/to/password-protected.pdf";
        String password = "your_owner_password";

        byte[] pdfBytes = readPdfBytes(inputPdf, password);

        // Now you can return pdfBytes or perform further operations
    }
}

在此示例中,readPdfBytes 方法使用 PDFBox 加载受密码保护的 PDF,确保安全性不会被删除,将其保存到 ByteArrayOutputStream,然后将其转换为字节数组。

您可以调用 readPdfBytes 并将结果字节数组返回给调用者。确保相应地调整输入文件路径(inputPdf)和密码。

这样,您可以在获取 PDF 文档的字节数组表示形式的同时保留 PDFBox 加密。

© www.soinside.com 2019 - 2024. All rights reserved.