pcvnes
pcvnes

Reputation: 967

How to persist changes with docx4j Java library to word document

I am using the docx4j Java library for the first time and have some difficulties finding a good reference. What i need to start is a simple Java class to enforce the protection on a Word document in read-only mode. I have com that far that i can read the protection mode and set it. But when saving the Word document the changes are not written to the Word document.

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);

        }
    }

}

When executing this class i do get the following output;

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

So i suspect that i am not updating the WordprocessingMLPackage or DocumentSettingsPart correctly in the protect method, but have currently no clue where it goes wrong.

Upvotes: 0

Views: 1582

Answers (2)

pcvnes
pcvnes

Reputation: 967

Resolved it. Instead of adding the DocumentSettingsPart to the loaded WordprocessingMLPackage. The CTDocProtect instance needs to be used to set the document protection on the content.

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);

Upvotes: 0

Related Questions