Disable Printing Issue with PDF Box

旧巷老猫 提交于 2019-12-11 08:05:26

问题


I am using this sample PDFBox code to encrypt and disable printing of a pdf file. Encryption happens successfully, but printing is not disabled.

What could be the issue?

Here's the dependencies section of my pom.xml

<dependencies>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15</artifactId>
        <version>1.46</version>
    </dependency>
</dependencies>

and below is the source code

import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;

public class Test {
    public static void main(String[] args) throws Exception {
        PDDocument doc = PDDocument.load(new File("/tmp/Test.pdf"));
        int keyLength = 128;
        AccessPermission ap = new AccessPermission();
        ap.setCanPrint(false);
        StandardProtectionPolicy spp = new StandardProtectionPolicy("Admin", "Password", ap);
        spp.setEncryptionKeyLength(keyLength);
        spp.setPermissions(ap);
        doc.protect(spp);
        doc.save("/tmp/Test-Encrypted.pdf");
        doc.close();
    }

}

来源:https://stackoverflow.com/questions/45240186/disable-printing-issue-with-pdf-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!