2016-06-29 23 views
0

처음으로 docx4j Java 라이브러리를 사용하고 있으며 좋은 참조를 찾는 데 어려움이 있습니다. 필요한 것은 읽기 전용 모드로 Word 문서를 보호하는 간단한 Java 클래스입니다. 나는 co.kr에서 멀리 내가 보호 모드를 읽고 그것을 설정할 수 있습니다. 그러나 Word 문서를 저장할 때 변경 내용은 Word 문서에 쓰여지지 않습니다.docx4j Java 라이브러리로 변경 사항을 워드 문서로

public class Doc4JPOC { 

    public static void main(String[] args) { 

     String docName = "/Users/petervannes/Desktop/Unprotected document.docx"; 
//  String docName = "/Users/petervannes/Desktop/Protected document.docx" ; 

     Doc4JPOC d4j = new Doc4JPOC(); 

     d4j.isProtected(docName); 
     d4j.protect(docName); 
     d4j.isProtected(docName); 

    } 

    private void protect(String filename) { 

     try { 
      WordprocessingMLPackage wordMLPackage = Docx4J.load(new java.io.File(filename)); 

      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart(); 

      Relationship rs = mdp.getRelationshipsPart().getRelationshipByType(Namespaces.SETTINGS); 
      DocumentSettingsPart dsp = (DocumentSettingsPart) mdp.getRelationshipsPart().getPart(rs); 

      // Update settings.xml 
      List<Object> nodes = dsp.getJAXBNodesViaXPath("//w:documentProtection", true); 
      for (Object obj : nodes) { 
       CTDocProtect cdtP = ((CTDocProtect) obj); 
       cdtP.setEnforcement(Boolean.TRUE); 
       cdtP.setEdit(STDocProtect.READ_ONLY); 
      } 

      // Write updated settings.xml to document 
      wordMLPackage.addTargetPart(dsp); 
//   wordMLPackage.save(new java.io.File(filename)); 

      Docx4J.save(wordMLPackage, new java.io.File(filename), 0); 
      System.out.println("Protected document " + filename) ; 

     } catch (Docx4JException ex) { 
      Logger.getLogger(Doc4JPOC.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (JAXBException jex) { 
      Logger.getLogger(Doc4JPOC.class.getName()).log(Level.SEVERE, null, jex); 

     } 

    } 

    private void isProtected(String filename) { 

     Boolean isProtectionEnforced = false; 
     STDocProtect editMode = STDocProtect.NONE; 

     try { 
      WordprocessingMLPackage wordMLPackage = Docx4J.load(new java.io.File(filename)); 

      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart(); 
      Relationship rs = mdp.getRelationshipsPart().getRelationshipByType(Namespaces.SETTINGS); 
      DocumentSettingsPart dsp = (DocumentSettingsPart) mdp.getRelationshipsPart().getPart(rs); 

      System.out.println("Partname : " + dsp.getPartName()); 

      List<Object> nodes = dsp.getJAXBNodesViaXPath("//w:documentProtection", true); 
      for (Object obj : nodes) { 
       CTDocProtect cdtP = ((CTDocProtect) obj); 

       isProtectionEnforced = cdtP.isEnforcement(); 
       editMode = cdtP.getEdit(); 

       System.out.println("Enforced: " + cdtP.isEnforcement()); 
       System.out.println("Edit: " + cdtP.getEdit()); 

      } 

      if (isProtectionEnforced) { 
       System.out.println("Protection is enabled , protection mode is " + editMode.toString()); 
      } else { 
       System.out.println("Protection is disabled"); 
      } 

     } catch (Docx4JException ex) { 
      Logger.getLogger(Doc4JPOC.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (JAXBException jex) { 
      Logger.getLogger(Doc4JPOC.class.getName()).log(Level.SEVERE, null, jex); 

     } 
    } 

} 

이 클래스를 실행하면 다음과 같은 결과가 표시됩니다.

Partname : /word/settings.xml 
Protection is disabled 
Protected document /Users/petervannes/Desktop/Unprotected document.docx 
Partname : /word/settings.xml 
Protection is disabled 

그래서 난 내가 보호 방법에 올바르게 WordprocessingMLPackage 또는 DocumentSettingsPart를 업데이트하고 있지 않다 의심하지만, 현재는 잘못 단서가 없다.

답변

0

해결되었습니다. 로드 된 WordprocessingMLPackage에 DocumentSettingsPart를 추가하는 대신. 콘텐츠에 대한 문서 보호를 설정하려면 CTDocProtect 인스턴스를 사용해야합니다.

CTDocProtect cdtP = new CTDocProtect(); 
cdtP.setEnforcement(Boolean.TRUE); 
cdtP.setEdit(STDocProtect.READ_ONLY); 

dsp.getContents().setDocumentProtection(cdtP); 
Docx4J.save(wordMLPackage, new java.io.File(filename), 0);