Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with variable replacement for Docx4j #9

Closed
rieckpil opened this issue Dec 16, 2019 · 2 comments
Closed

Issue with variable replacement for Docx4j #9

rieckpil opened this issue Dec 16, 2019 · 2 comments

Comments

@rieckpil
Copy link
Owner

@rieckpil rieckpil commented Dec 16, 2019

Hey Rieckpil,
This is my code I don’t know what I want to replace the variable with the collection

public class DocxGenerator {

    private static final String TEMPLATE_NAME = “template.dotx”;

    public byte[] generateDocxFileFromTemplate() throws Exception {

        InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(TEMPLATE_NAME);

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateInputStream);

        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        List colorlist = new ArrayList();
        colorlist.add(“red”);
        colorlist.add(“white”);
        colorlist.add(“green”);

        VariablePrepare.prepare(wordMLPackage);

        java.util.HashMap variables = new java.util.HashMap();
        VariablePrepare.prepare(wordMLPackage);
        variables.put(“firstName”, “sathish”);
        variables.put(“lastName”, “kumar”);
        variables.put(“colorlist”, colorlist);

        documentPart.variableReplace(variables);

    }
}

My document template:

My name is ${firstName} ${lastName}
My favourite colors are ${colorlist}

@rieckpil

This comment has been minimized.

Copy link
Owner Author

@rieckpil rieckpil commented Dec 16, 2019

I assume you can't replace the ${colorlist} variable with a Java collection type as the .variableReplace() method of docx4j has the following signature public void variableReplace(java.util.Map<String, String> mappings) throws JAXBException, Docx4JException and is therefor expecting a Map<String, String>.

If you just want to render the content of your List, you can do the following:

 variables.put("colorlist", Arrays.asList("blue", "red", "white").stream().collect(Collectors.joining(", ")));

and the output on the document should be: blue, red, white.

Otherwise please have a look at the library itself or ask the question again on StackOverflow.

@rieckpil rieckpil closed this Dec 16, 2019
@sathish38

This comment has been minimized.

Copy link

@sathish38 sathish38 commented Dec 16, 2019

I assume you can't replace the ${colorlist} variable with a Java collection type as the .variableReplace() method of docx4j has the following signature public void variableReplace(java.util.Map<String, String> mappings) throws JAXBException, Docx4JException and is therefor expecting a Map<String, String>.

If you just want to render the content of your List, you can do the following:

 variables.put("colorlist", Arrays.asList("blue", "red", "white").stream().collect(Collectors.joining(", ")));

and the output on the document should be: blue, red, white.

Otherwise please have a look at the library itself or ask the question again on StackOverflow.

Hi rieckpil,
I want to iterate it by one by one like this
1.blue
2.red
3.white
what I want to do change in my template so far I used the foreach in the template but it doesnt works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.