ich versuche über ein Programm Feldbefehle in einen Dokument hinzuzufügen. Das funktioniert bei Feldbefehlen die nur Texte darstellen sollen auch prima. Nur bei Feldbefehlen die Währungswerte darstellen sollen funktioniert es nicht.
Der Feldbefehl wird erzeugt und die Formatierung steht auch auf default Währung. Als Wert an den Feld wird 0.0 übergeben. Leider zeigt der Feldbefehl dann im Dokument auch nur 0.0 an. Und nicht laut Formatierung 0,00 €.
Was muss ich jetzt schon wieder im Quellcode veranstalten, damit das funktioniert ????
Das gleiche Problem habe ich auch, wenn ich zum Druckzeitpunkt die eigtl. Werte in die Feldbefehle schreib. Dort wird dann z.B. 46.0 gesetzt. Und so dann auch gedruckt. Also wieder nicht formatiert !!!
Code: Alles auswählen
private XDependentTextField createTextField(XTextDocument xTextDocument, String fieldName, String fieldValue)
{
XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier) UnoRuntime.queryInterface(XTextFieldsSupplier.class, xTextDocument);
XNameAccess xNamedFieldMasters = xTextFieldsSupplier.getTextFieldMasters();
XDependentTextField userField = null;
XPropertySet masterPropSet = null;
try
{
// Create the field...
userField = (XDependentTextField) UnoRuntime.queryInterface(XDependentTextField.class, xMultiServiceFactory.createInstance("com.sun.star.text.TextField.User"));
if (!xNamedFieldMasters.hasByName("com.sun.star.text.FieldMaster.User." + fieldName))
{
// Create the field master...
masterPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xMultiServiceFactory.createInstance("com.sun.star.text.FieldMaster.User"));
// Set the field name and content...
masterPropSet.setPropertyValue("Name", fieldName);
masterPropSet.setPropertyValue("Content", fieldValue);
if (fieldValue.equals("0.0"))
{
// Query the number formats supplier of the document
com.sun.star.util.XNumberFormatsSupplier xNumberFormatsSupplier = (com.sun.star.util.XNumberFormatsSupplier) UnoRuntime.queryInterface(com.sun.star.util.XNumberFormatsSupplier.class, xTextDocument);
// Get the number formats from the supplier
com.sun.star.util.XNumberFormats xNumberFormats = xNumberFormatsSupplier.getNumberFormats();
// Query the XNumberFormatTypes interface
com.sun.star.util.XNumberFormatTypes xNumberFormatTypes = (com.sun.star.util.XNumberFormatTypes) UnoRuntime.queryInterface(com.sun.star.util.XNumberFormatTypes.class, xNumberFormats);
// Get the number format index key of the default currency
// format,
// note the empty locale for default locale
com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
int nCurrencyKey = xNumberFormatTypes.getStandardFormat(com.sun.star.util.NumberFormat.CURRENCY, aLocale);
XPropertySet fieldProperties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, userField);
fieldProperties.setPropertyValue("NumberFormat", new Integer(nCurrencyKey));
}
}
else
{
masterPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xNamedFieldMasters.getByName("com.sun.star.text.FieldMaster.User." + fieldName));
}
// Attach the fieldmaster to the field...
userField.attachTextFieldMaster(masterPropSet);
}
catch (java.lang.Exception e)
{
e.printStackTrace();
}
return userField;
}