Java で Odtftoolkit ライブラリを使用しており、テンプレート ドキュメントを使用するプロセスがありますTextUserFieldDeclElement。私のプロセスでは、同じテンプレートから 2 つの異なるドキュメントが作成され、同じドキュメントで両方のページを表示する必要があります。私の問題はTextUserFieldDeclElement、最初のページが 2 番目のページと同じ値を示していることです。
私のコードは次のようなものです:
1: 各ドキュメントを作成します (最初にオブジェクト "plantilla" を使用してこのメソッドを呼び出し、その後、別のオブジェクト "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: arDocuments Array を使用して、すべてのドキュメントを次のようにコピーしようとしました。
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);
}