如何使用docx4j Java库将更改持久化到word文档



我是第一次使用docx4j Java库,很难找到一个好的参考。我需要开始的是一个简单的Java类,用于在只读模式下对Word文档实施保护。我已经可以读取保护模式并设置它了。但是当保存Word文档时,更改不会写入Word文档。

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,但目前不知道它在哪里出错。

已解析。而不是将DocumentSettingsPart添加到加载的WordprocessingMLPackage中。需要使用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);

对于docx4j v3.3.0,请参阅http://www.docx4java.org/forums/docx-java-f6/password-set-for-documentprotection-not-accepted-in-msword-t2427.html#p8290

最新更新