I'm using the Odtftoolkit library in Java, and I have a process that uses a template document with TextUserFieldDeclElement. My process makes two diferents documents from the same template, and I need to show both pages in the same document. My problem is that TextUserFieldDeclElement of the first page shows the same value that the second page.
My code is like this:
1: Make each document (I call to this metodos first with a object "plantilla", and after, with other diferent object "plantilla").
TextDocument outputOdt = TextDocument.loadDocument(new File(plantilla));
OdfElement contenedor = outputOdt.getVariableContainerElement();
org.w3c.dom.NodeList nodos = contenedor
.getElementsByTagName("text:user-field-decl");
for (int i = 0; i < nodos.getLength(); i++) {
if (nodos.item(i) instanceof TextUserFieldDeclElement) {
TextUserFieldDeclElement txt = (TextUserFieldDeclElement) nodos
.item(i);
if (txt.getTextNameAttribute() != null) {
// Consultamos si el atributo está en el hash
if (poGlobal.containsKey(txt.getTextNameAttribute())) {
outputOdt.getVariableFieldByName(
txt.getTextNameAttribute()).updateField(
poGlobal.get(txt.getTextNameAttribute()),
null);
}
}
}
}
File ficTmp = Fichero.dameFicheroTemporal(".odt");
// Volcamos al fichero temporal
outputOdt.save(ficTmp);
arDocumentos.add(ficTmp);
2: I work with the arDocuments Array and try to copy all documents into the same as:
try {
TextDocument target = TextDocument.loadDocument(this.arDocumentos
.get(0));
// Por cada documento, copiamos y pegamos en el destino
for (int i = 1; i < this.arDocumentos.size(); i++) {
Paragraph parrafo = target.addParagraph(null);
TextDocument src = TextDocument.loadDocument(this.arDocumentos
.get(i));
target.insertContentFromDocumentAfter(src, parrafo, false);
}
target.save(new File(destino));
} catch (Exception ex) {
throw new CtrError("Error al cerrar el documento.", ex);
}