如何使用docx4j Java库将更改保存到word文档

时间:2016-06-29 19:53:58

标签: java ms-word docx4j wordprocessingml

我第一次使用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

所以我怀疑我没有在protect方法中正确更新WordprocessingMLPackage或DocumentSettingsPart,但目前还不知道哪里出错了。

2 个答案:

答案 0 :(得分:0)

解决了它。而不是将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);

答案 1 :(得分:0)