com.aspose.words

Class FieldFormat

  • java.lang.Object
    • com.aspose.words.FieldFormat
public class FieldFormat 
extends java.lang.Object

Provides typed access to field's numeric, date and time, and general formatting.

Example:

Shows how to formatting fields
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Use a document builder to insert field with no format
Field field = builder.insertField("= 2 + 3");

// We can format our field here instead of in the field code
FieldFormat format = field.getFormat();
format.setNumericFormat("$###.00");
field.update();

// Apply a date/time format
field = builder.insertField("DATE");
format = field.getFormat();
format.setDateTimeFormat("dddd, MMMM dd, yyyy");
field.update();

// Apply 2 general formats at the same time
field = builder.insertField("= 25 + 33");
format = field.getFormat();
format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().add(GeneralFormat.UPPER);
field.update();

int index = 0;
Iterator<Integer> generalFormatEnumerator = format.getGeneralFormats().iterator();
while (generalFormatEnumerator.hasNext()) {
    System.out.println(MessageFormat.format("General format index {0}: {1}", index++, generalFormatEnumerator.toString()));
}

Assert.assertEquals("LVIII", field.getResult());
Assert.assertEquals(2, format.getGeneralFormats().getCount());
Assert.assertEquals(format.getGeneralFormats().get(0), GeneralFormat.LOWERCASE_ROMAN);

// Removing field formats
format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
format.getGeneralFormats().removeAt(0);
Assert.assertEquals(format.getGeneralFormats().getCount(), 0);
field.update();

// Our field has no general formats left and is back to default form
Assert.assertEquals(field.getResult(), "58");

Property Getters/Setters Summary
java.lang.StringgetDateTimeFormat()
void
setDateTimeFormat(java.lang.Stringvalue)
           Gets or sets a formatting that is applied to a date and time field result. Corresponds to the \@ switch.
GeneralFormatCollectiongetGeneralFormats()
Gets a collection of general formats that are applied to a numeric, text or any field result. Corresponds to the \* switches.
java.lang.StringgetNumericFormat()
void
setNumericFormat(java.lang.Stringvalue)
           Gets or sets a formatting that is applied to a numeric field result. Corresponds to the \# switch.
 

    • Property Getters/Setters Detail

      • getDateTimeFormat/setDateTimeFormat

        public java.lang.String getDateTimeFormat() / public void setDateTimeFormat(java.lang.String value)
        
        Gets or sets a formatting that is applied to a date and time field result. Corresponds to the \@ switch.

        Example:

        Shows how to formatting fields
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Use a document builder to insert field with no format
        Field field = builder.insertField("= 2 + 3");
        
        // We can format our field here instead of in the field code
        FieldFormat format = field.getFormat();
        format.setNumericFormat("$###.00");
        field.update();
        
        // Apply a date/time format
        field = builder.insertField("DATE");
        format = field.getFormat();
        format.setDateTimeFormat("dddd, MMMM dd, yyyy");
        field.update();
        
        // Apply 2 general formats at the same time
        field = builder.insertField("= 25 + 33");
        format = field.getFormat();
        format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().add(GeneralFormat.UPPER);
        field.update();
        
        int index = 0;
        Iterator<Integer> generalFormatEnumerator = format.getGeneralFormats().iterator();
        while (generalFormatEnumerator.hasNext()) {
            System.out.println(MessageFormat.format("General format index {0}: {1}", index++, generalFormatEnumerator.toString()));
        }
        
        Assert.assertEquals("LVIII", field.getResult());
        Assert.assertEquals(2, format.getGeneralFormats().getCount());
        Assert.assertEquals(format.getGeneralFormats().get(0), GeneralFormat.LOWERCASE_ROMAN);
        
        // Removing field formats
        format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().removeAt(0);
        Assert.assertEquals(format.getGeneralFormats().getCount(), 0);
        field.update();
        
        // Our field has no general formats left and is back to default form
        Assert.assertEquals(field.getResult(), "58");
      • getGeneralFormats

        public GeneralFormatCollection getGeneralFormats()
        
        Gets a collection of general formats that are applied to a numeric, text or any field result. Corresponds to the \* switches.

        Example:

        Shows how to formatting fields
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Use a document builder to insert field with no format
        Field field = builder.insertField("= 2 + 3");
        
        // We can format our field here instead of in the field code
        FieldFormat format = field.getFormat();
        format.setNumericFormat("$###.00");
        field.update();
        
        // Apply a date/time format
        field = builder.insertField("DATE");
        format = field.getFormat();
        format.setDateTimeFormat("dddd, MMMM dd, yyyy");
        field.update();
        
        // Apply 2 general formats at the same time
        field = builder.insertField("= 25 + 33");
        format = field.getFormat();
        format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().add(GeneralFormat.UPPER);
        field.update();
        
        int index = 0;
        Iterator<Integer> generalFormatEnumerator = format.getGeneralFormats().iterator();
        while (generalFormatEnumerator.hasNext()) {
            System.out.println(MessageFormat.format("General format index {0}: {1}", index++, generalFormatEnumerator.toString()));
        }
        
        Assert.assertEquals("LVIII", field.getResult());
        Assert.assertEquals(2, format.getGeneralFormats().getCount());
        Assert.assertEquals(format.getGeneralFormats().get(0), GeneralFormat.LOWERCASE_ROMAN);
        
        // Removing field formats
        format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().removeAt(0);
        Assert.assertEquals(format.getGeneralFormats().getCount(), 0);
        field.update();
        
        // Our field has no general formats left and is back to default form
        Assert.assertEquals(field.getResult(), "58");
      • getNumericFormat/setNumericFormat

        public java.lang.String getNumericFormat() / public void setNumericFormat(java.lang.String value)
        
        Gets or sets a formatting that is applied to a numeric field result. Corresponds to the \# switch.

        Example:

        Shows how to formatting fields
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        // Use a document builder to insert field with no format
        Field field = builder.insertField("= 2 + 3");
        
        // We can format our field here instead of in the field code
        FieldFormat format = field.getFormat();
        format.setNumericFormat("$###.00");
        field.update();
        
        // Apply a date/time format
        field = builder.insertField("DATE");
        format = field.getFormat();
        format.setDateTimeFormat("dddd, MMMM dd, yyyy");
        field.update();
        
        // Apply 2 general formats at the same time
        field = builder.insertField("= 25 + 33");
        format = field.getFormat();
        format.getGeneralFormats().add(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().add(GeneralFormat.UPPER);
        field.update();
        
        int index = 0;
        Iterator<Integer> generalFormatEnumerator = format.getGeneralFormats().iterator();
        while (generalFormatEnumerator.hasNext()) {
            System.out.println(MessageFormat.format("General format index {0}: {1}", index++, generalFormatEnumerator.toString()));
        }
        
        Assert.assertEquals("LVIII", field.getResult());
        Assert.assertEquals(2, format.getGeneralFormats().getCount());
        Assert.assertEquals(format.getGeneralFormats().get(0), GeneralFormat.LOWERCASE_ROMAN);
        
        // Removing field formats
        format.getGeneralFormats().remove(GeneralFormat.LOWERCASE_ROMAN);
        format.getGeneralFormats().removeAt(0);
        Assert.assertEquals(format.getGeneralFormats().getCount(), 0);
        field.update();
        
        // Our field has no general formats left and is back to default form
        Assert.assertEquals(field.getResult(), "58");