代码之家  ›  专栏  ›  技术社区  ›  Sugunalakshmi Pagemajik

删除CustomXml文件-docx4j

  •  0
  • Sugunalakshmi Pagemajik  · 技术社区  · 4 年前

    wordMLPackage.getCustomXmlDataStorageParts().clear();
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   JasonPlutext    4 年前

    https://github.com/plutext/docx4j/blob/master/docx4j-core/src/main/java/org/docx4j/Docx4J.java#L643 告诉你怎么做。要全部删除它们,应该是:

    protected static void removeDefinedCustomXmlParts(WordprocessingMLPackage wmlPackage) {
    List<PartName> partsToRemove = new ArrayList<PartName>();
    RelationshipsPart relationshipsPart = wmlPackage.getMainDocumentPart().getRelationshipsPart();
    List<Relationship> relationshipsList = ((relationshipsPart != null) && 
                                            (relationshipsPart.getRelationships() != null) ?
                                            relationshipsPart.getRelationships().getRelationship() : null);
    Part part = null;
        if (relationshipsList != null) {
            for (Relationship relationship : relationshipsList) {
                if (Namespaces.CUSTOM_XML_DATA_STORAGE.equals(relationship.getType())) {
                    part = relationshipsPart.getPart(relationship);
                    partsToRemove.add(part.getPartName());
                }
            }
        }
        if (!partsToRemove.isEmpty()) {
            for (int i=0; i<partsToRemove.size(); i++) {
                relationshipsPart.removePart(partsToRemove.get(i));
            }
        }
    }