2015-09-04 79 views
1

我一直在試圖把我的PDF文件的權限。 我有這將設置訪問權限上稱爲訪問訪問權限不起作用

private AccessPermission access = new AccessPermission(); 
    public void setPdfPermissions(boolean allowPrint, boolean degradePrint, 
       boolean editPage, boolean allowAssembly, boolean allowCopy, 
       boolean allowReaders, boolean editAnnotation, boolean allowFillIn) { 
      if (allowPrint) { // allow printing 
       access.setCanPrint(allowPrint); 
      } 
      if (degradePrint) { // degrade printing 
       access.setCanPrintDegraded(allowAssembly); 
      } 
      if (editPage) { // edit page contents 
       access.setCanModify(editPage); 
      } 
      if (allowAssembly) { // insert, remote or rotate page 
       access.setCanAssembleDocument(allowAssembly); 
      } 
      if (allowCopy) { // copy page contents or graphics 
       access.setCanExtractForAccessibility(allowCopy); 
      } 
      if (allowReaders) { // screen readers can copy contents or graphics 
       access.setReadOnly(); 
      } 
      if (editAnnotation) { // edit annotations 
       access.setCanModifyAnnotations(editAnnotation); 
      } 
      if (allowFillIn) { // fill form fields 
       access.setCanFillInForm(allowFillIn); 
      } 

     } 

,然後我米保存在安全處理程序中訪問的實例變量的方法

StandardSecurityHandler secHandler = new StandardSecurityHandler(); 
      if((userPass != null) || (ownerPass != null)) { 
       System.out.println("userPass:"+userPass+"owner pass:"+userPass); 
       // TODO 
       StandardProtectionPolicy policy = new StandardProtectionPolicy(ownerPass.toString(), userPass.toString(), 
         access); 
       secHandler = new StandardSecurityHandler(policy); 
    document.setSecHandler(secHandler); 

時,即時通訊傳遞錯誤的價值像setPrint爲假,它允許我打印。任何幫助,高度讚賞。

回答

1

你的第二個代碼塊是錯誤的。 here描述了加密文件的正確方法(至少對於1.8版本)。所以對你來說,正確的代碼是:

// owner password to open the file with all permissions 
// user password to open the file but with restricted permissions, can be empty 
StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPass, userPass, access); 
spp.setEncryptionKeyLength(128); 
doc.protect(spp); 

編輯:另見MKL的答案,爲什麼你的第一個代碼段是錯的太:-)

1

除了@提爾曼的回答,第一個代碼塊也是錯誤的:

對於每個boolean參數,setPdfPermissions只有做一些事情如果值是true,如:

if (allowPrint) { // allow printing 
    access.setCanPrint(allowPrint); 
} 

這將工作,如果默認權限未授予。但是,看看AccessPermission默認構造函數的定義,人們會發現情況正好相反。在從1.8.10的代碼:

/** 
* Create a new access permission object. 
* By default, all permissions are granted. 
*/ 
public AccessPermission() 
{ 
    bytes = DEFAULT_PERMISSIONS; 
} 

因此,setPdfPermissions essentialis是一個很大的NOP(無操作)的代碼塊。