有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我第一次使用docx4j Java库,在找到一个好的参考资料时遇到了一些困难。我需要启动一个简单的Java类,以在只读模式下对Word文档实施保护。我有足够的com,可以读取保护模式并进行设置。但在保存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或DocumentSettingsPat,但目前不知道哪里出了问题


共 (2) 个答案

  1. # 1 楼答案

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