Rachit Garg
Rachit Garg

Reputation: 1

Updates to wordMLPackage not reflecting in docx file after Docx4J.save() method

Updates to wordMLPackage after Docx4j.bind are not reflecting in final output document file. Using Docx4j 8.2.29

Here is my code :

private R executeTask(Map<String, Object> context,InputStream inputTemplateDoc, DocumentDataBean documentDataBean)  {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            WordprocessingMLPackage wordMLPackage = Docx4J.load(inputTemplateDoc);
            BindingHandler.getHyperlinkResolver().setHyperlinkStyle("Hyperlink");
            Docx4J.bind(wordMLPackage, marshallToString(documentDataBean), Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_BIND_XML);
            manipulateDocumentTags(context,wordMLPackage);
            Docx4J.save(wordMLPackage, byteArrayOutputStream, Docx4J.FLAG_NONE);
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return processManualTask(context,byteArrayOutputStream); }

public <T> String marshallToString(T object)  {

    if (object==null) return "Marshalling Disabled";

    JAXBContext jaxbContext ;
    Marshaller jaxbMarshaller ;
    String xmlString = null;
    try {
        jaxbContext = JAXBContext.newInstance(object.getClass());
        StringWriter sw = new StringWriter();
        jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


        jaxbMarshaller.marshal(object, sw);
        xmlString = sw.toString();

        log.info(xmlString);
        xmlString=manipulateXMLString(xmlString);

        log.info("manipulateXMLString : {}", xmlString);
    } catch (JAXBException e) {
        log.error("JAXBException durng marshalling " , e);
    }
    return xmlString;
}

My issue is I have a field in documentDataBean which is having line break("\n"). In returned xmlString also I can see the line break. But after Docx.bind it got removed in wordMLPackage. Also tried inserting line break in wordMLPackage after bind but final document is not picking any changes of updated wordMLPackage.

Upvotes: 0

Views: 42

Answers (0)

Related Questions