I have a template with certain variables that I need to replace with info from my database, basically.
when I just want to replace with a word it's easy, my problem is when I need to break line like this:
This two persons with their CPF's should come from my variable, but that's what I'm getting:
That's my code now, I've tried putting "\n", "
" and "<w:\br>", and none of them worked, now I'm putting "\n" just to be clear where I want to break line:
private String criaIdentificacaoAssistenciaSocial(Lote lote) throws IOException {
StringBuilder identificacaoAS = new StringBuilder();
for (Pessoa pessoa : lote.getJsonBeneficiarios()) {
PessoaFisica beneficiario = (PessoaFisica) pessoa;
identificacaoAS.append(beneficiario.getNome()).append(", CPF ").append(beneficiario.getCpf());
identificacaoAS.append("\n");
if (beneficiario.getConjuge() != null) {
identificacaoAS.append(beneficiario.getConjuge().getNome()).append(", CPF ").append(beneficiario.getConjuge().getCpf());
identificacaoAS.append("\n");
}
}
return identificacaoAS.toString();
}
My return value goes inside my mappingDocsVariable, then I generate the docx file with this code:
private File geraDocumento(HashMap<String, String> mappingDocVariables, String nomeDocx) throws Exception {
String inputfilepath = System.getProperty("user.dir") + File.separator
+ "utils" + File.separator + nomeDocx + ".docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
VariablePrepare.prepare(wordMLPackage);
documentPart.variableReplace(mappingDocVariables);
String tDir = System.getProperty("java.io.tmpdir");
File temp = new File(tDir + nomeDocx + ".docx");
Docx4J.save(wordMLPackage, temp);
temp.deleteOnExit();
return temp;
}
See newlineToBreakHack at https://github.com/plutext/docx4j/blob/master/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/VariableReplace.java#L122
Previously answered at Insert line breaks in VariableReplace in docx4j