Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docx4J get header/footer elements from docx file and modify them

I am using Docx4J to modify docx templates and put values in place of placeholders in the template that are predefined.

so far, i had been successful in finding and replacing paragraphs and texts, tables, images etc. But i am not yet successful in finding Header and/or Footer elements of the document.

I am using

WordprocessingMLPackage wordMLPackage =
WordprocessingMLPackage.load(new java.io.File(inputfilepath));
wordMLPackage.getMainDocumentPart(); 

to search for elements in the template.

like image 800
Waqas Memon Avatar asked Feb 17 '23 01:02

Waqas Memon


2 Answers

For your application, you can mimic the code in https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/datastorage/BindingHandler.java at line 145

A similar approach is taken in https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/model/fields/merge/MailMerger.java at line 124

like image 112
JasonPlutext Avatar answered Feb 18 '23 14:02

JasonPlutext


This is how I managed to replace keyholders on the footer(In Java, no XML). The piece that I was missing is the :

JaxbXmlPart part = (JaxbXmlPart) relationshipPart.getPart(r);

from the source-code MailMerger.java that JasonPlutext mentioned, stated in the line 410. So thank you JasonPlutext!

public static void replaceElementFromFooter(WordprocessingMLPackage template, String nameplace, String placeholder, String newValue) throws Docx4JException {
    List<Object> result = new ArrayList<Object>();

    RelationshipsPart relationshipPart = template.getMainDocumentPart().getRelationshipsPart();

    List<Relationship> relationships = relationshipPart.getRelationships().getRelationship()

    for (Relationship r : relationships) {

        if (r.getType().equals(nameplace)) {

            JaxbXmlPart part = (JaxbXmlPart) relationshipPart.getPart(r);

            List<Object> texts = getAllElementsFromObject(part.getContents(), Text.class);

            replaceTextElement(texts, placeholder, newValue);
        }

    }

    return result;
}

private static List<Object> getAllElementsFromObject(Object obj, Class<?> toSearch) {
    List<Object> result = new ArrayList<Object>();

    if (obj instanceof JAXBElement) {
        obj = ((JAXBElement<?>) obj).getValue();
    }

    if (obj.getClass().equals(toSearch)) {
        result.add(obj);
    } else if (obj instanceof ContentAccessor) {
        List<?> children = ((ContentAccessor) obj).getContent();
        for (Object child : children) {
            result.addAll(getAllElementFromObject(child, toSearch));
        }
    }
    return result;
}

private static void replaceTextElement(List<Object> texts, String placeholder, String newValue) {
    for (Object element : texts) {
        Text textElement = (Text) element;

        if (textElement.getValue().contains(placeholder)) {
            String replacedValue = textElement.getValue().replaceAll(placeholder, newValue);
            textElement.setValue(replacedValue);
        }

    }
}
like image 37
aks Avatar answered Feb 18 '23 14:02

aks