Hi,
I'm developing a library that changes values of user fields within an ODT
file, converts the file to pdf and then saves it to disk. I'm saving the
intermediate odt file (with the filled user fields) to a stream and then
passing it to a converter of xDocReport which does not use OpenOffice for
the conversion.
As I could understand the fields are not updated unless the file is opened
by a LibreOffice or OpenOffice process, leaving me with a pdf containing
the user field references with their old default values. This is confirmed
when I switch to JODconverter 2.2.1 and convert the file through a headless
OpenOffice process.
Is there any way of updating all the fields in the document through the
simpleAPI or ODFDOM? I hacked away a solution for the User Field case but
Date and other types of fields are still a problem.
Here's a code snippet with my solution for the user fields:
NodeList declarations;
NodeList references;
Map<String, VariableField> alteredFields = new HashMap<String,
VariableField>();
try {
declarations =
document.getContentRoot().getElementsByTagName("text:user-field-decl");
references =
document.getContentRoot().getElementsByTagName("text:user-field-get");
} catch (Exception e) {
logger.error("Failed to create the file DOM while filling the
user fields.");
return;
}
for (int i = 0; i < declarations.getLength(); i++) {
String userFieldName =
declarations.item(i).getAttributes().getNamedItem("text:name").getNodeValue();
VariableField var =
document.getVariableFieldByName(userFieldName);
Object fieldValue = getNewFieldValue(userFieldName);
var.updateField(translate(fieldValue, locale), null);
alteredFields.put(userFieldName, var);
}
//check for all references to altered fields and insert the value
automatically.
for (int i = 0; i < references.getLength(); i++) {
String userFieldName =
references.item(i).getAttributes().getNamedItem("text:name").getNodeValue();
VariableField var = alteredFields.get(userFieldName);
if (var != null) {
String fieldValue =
var.getOdfElement().getAttribute("office:string-value");
references.item(i).setTextContent(fieldValue);
}
}
I'd be very thankful if you could help me with this since I do not want to
rely on JODConverter for this library (As it's old and its development is
probably dead) nor dispense the resources needed to open the file with
OpenOffice before converting.
P.S. Also something weird happens when I use
document.getVariableFieldByName(userFieldName) twice, on the second time
the fields value is replaced by an empty string... That's one reason why I
store the altered fields instead of just getting them again.
Best Regards,
Gil Lacerda