使用PDFBox保护PDF

tar*_*rka 6 java pdf pdfbox

我真的在为PDFBox的文档挣扎.对于这样一个受欢迎的图书馆信息似乎有点薄(对我来说!).

无论如何,我有关于保护PDF的问题.目前我想要的是控制用户的访问权限.特别是我想阻止用户修改PDF.

如果我省略访问权限代码,一切都很完美.我正在从外部资源中读取PDF.然后我在阅读并填充字段,在保存新PDF之前添加一些图像.这一切都很完美.

当我添加以下代码来管理访问时出现问题:

/* Secure the PDF so that it cannot be edited */
try {
    String ownerPassword = "DSTE$gewRges43";
    String userPassword = "";

    AccessPermission ap = new AccessPermission();
    ap.setCanModify(false);

    StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPassword, userPassword, ap);
    pdf.protect(spp);
} catch (BadSecurityHandlerException ex) {
    Logger.getLogger(PDFManager.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)

当我添加此代码时,所有文本和图像都是从传出的pdf中条带化的.这些字段仍然存在于文档中,但它们都是空的,并且原始PDF的一部分以及在代码中动态添加的所有文本和图像都消失了.

更新:好的,尽我所知,问题来自与表单字段相关的错误.我将尝试一种没有表单字段的不同方法,看看它给出了什么.

Nig*_*olf 9

我找到了解决这个问题的方法.看来,如果PDF来自外部源,有时PDF受到保护或加密.

如果从外部源加载PDF文档并获得保护时出现空白输出,则可能使用加密文档.我有一个处理PDF文档的流处理系统.所以下面的代码对我有用.如果您只是使用PDF输入,那么您可以将以下代码与您的流程集成.

public InputStream convertDocument(InputStream dataStream) throws Exception {
    // just acts as a pass through since already in pdf format
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);

    System.setProperty("org.apache.pdfbox.baseParser.pushBackSize", "2024768"); //for large files

    PDDocument doc = PDDocument.load(dataStream, true);

    if (doc.isEncrypted()) { //remove the security before adding protections
        doc.decrypt("");
        doc.setAllSecurityToBeRemoved(true);
    }
    doc.save(os);
    doc.close();
    dataStream.close();
    os.close();
    return is;
}
Run Code Online (Sandbox Code Playgroud)

现在拿回返回的InputStream并将其用于您的安全应用程序;

   PipedOutputStream os = new PipedOutputStream();
   PipedInputStream is = new PipedInputStream(os);

   System.setProperty("org.apache.pdfbox.baseParser.pushBackSize", "2024768");
   InputStream dataStream = secureData.data();

   PDDocument doc = PDDocument.load(dataStream, true);
   AccessPermission ap = new AccessPermission();
   //add what ever perms you need blah blah...
   ap.setCanModify(false);
   ap.setCanExtractContent(false);
   ap.setCanPrint(false);
   ap.setCanPrintDegraded(false);
   ap.setReadOnly();

   StandardProtectionPolicy spp = new StandardProtectionPolicy(UUID.randomUUID().toString(), "", ap);

   doc.protect(spp);

   doc.save(os);
   doc.close();
   dataStream.close();
   os.close();
Run Code Online (Sandbox Code Playgroud)

现在这应该返回一个没有空白输出的正确文档!

诀窍是先删除加密!