com.aspose.words

Interface IFieldResultFormatter

  • public interface IFieldResultFormatter 

Implement this interface if you want to control how the field result is formatted.

Example:

Shows how to control how the field result is formatted.
public void insertCustomFormattingField() throws Exception {
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    doc.getFieldOptions().setResultFormatter(new FieldResultFormatter("${0}", "Date: {0}", "Item # {0}:"));

    // Insert a field with a numeric format
    builder.insertField(" = 2 + 3 \\# $###", null);

    // Insert a field with a date/time format
    builder.insertField("DATE \\@ \"d MMMM yyyy\"", null);

    // Insert a field with a general format
    builder.insertField("QUOTE \"2\" \\* Ordinal", null);

    // Formats will be applied and recorded by the formatter during the field update
    doc.updateFields();
    ((FieldResultFormatter) doc.getFieldOptions().getResultFormatter()).printInvocations();

    // Our formatter has also overridden the formats that were originally applied in the fields
    Assert.assertEquals(doc.getRange().getFields().get(0).getResult(), "$5");
    Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
    Assert.assertEquals(doc.getRange().getFields().get(2).getResult(), "Item # 2:");
}

/// <summary>
/// Custom IFieldResult implementation that applies formats and tracks format invocations
/// </summary>
private static class FieldResultFormatter implements IFieldResultFormatter {
    public FieldResultFormatter(final String numberFormat, final String dateFormat, final String generalFormat) {
        mNumberFormat = numberFormat;
        mDateFormat = dateFormat;
        mGeneralFormat = generalFormat;
    }

    public String formatNumeric(final double value, final String format) {
        mNumberFormatInvocations.add(new Object[]{value, format});

        return (mNumberFormat == null || "".equals(mNumberFormat)) ? null : MessageFormat.format(mNumberFormat, value);
    }

    public String formatDateTime(final Date value, final String format, final int calendarType) {
        mDateFormatInvocations.add(new Object[]{value, format, calendarType});

        return (mDateFormat == null || "".equals(mDateFormat)) ? null : MessageFormat.format(mDateFormat, value);
    }

    public String format(final String value, final int format) {
        return format((Object) value, format);
    }

    public String format(final double value, final int format) {
        return format((Object) value, format);
    }

    private String format(final Object value, final int format) {
        mGeneralFormatInvocations.add(new Object[]{value, format});

        return (mGeneralFormat == null || "".equals(mGeneralFormat)) ? null : MessageFormat.format(mGeneralFormat, value);
    }

    public void printInvocations() {
        System.out.println(MessageFormat.format("Number format invocations ({0}):", mNumberFormatInvocations.size()));
        for (Object[] s : (Iterable<Object[]>) mNumberFormatInvocations) {
            System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
        }

        System.out.println(MessageFormat.format("Date format invocations ({0}):", mDateFormatInvocations.size()));
        for (Object[] s : (Iterable<Object[]>) mDateFormatInvocations) {
            System.out.println("\tValue: " + s[0] + ", original format: " + s[1] + ", calendar type: " + s[2]);
        }

        System.out.println(MessageFormat.format("General format invocations ({0}):", mGeneralFormatInvocations.size()));
        for (Object[] s : (Iterable<Object[]>) mGeneralFormatInvocations) {
            System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
        }
    }

    private String mNumberFormat;
    private String mDateFormat;
    private String mGeneralFormat;

    private ArrayList mNumberFormatInvocations = new ArrayList();
    private ArrayList mDateFormatInvocations = new ArrayList();
    private ArrayList mGeneralFormatInvocations = new ArrayList();

}

Method Summary
abstract java.lang.Stringformat(double value, int format)
Called when Aspose.Words applies a number format switch, i.e. \* Ordinal.
abstract java.lang.Stringformat(java.lang.String value, int format)
Called when Aspose.Words applies a capitalization format switch, i.e. \* Upper.
abstract java.lang.StringformatDateTime(java.util.Date value, java.lang.String format, int calendarType)
Called when Aspose.Words applies a date/time format switch, i.e. \@ "dd.MM.yyyy".
abstract java.lang.StringformatNumeric(double value, java.lang.String format)
Called when Aspose.Words applies a numeric format switch, i.e. \# "#.##".
 

    • Method Detail

      • format

        public abstract java.lang.String format(double value, int format)
        Called when Aspose.Words applies a number format switch, i.e. \* Ordinal. The implementation should return null to indicate that the default formatting should be applied.
        Parameters:
        format - A GeneralFormat value.

        Example:

        Shows how to control how the field result is formatted.
        public void insertCustomFormattingField() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            doc.getFieldOptions().setResultFormatter(new FieldResultFormatter("${0}", "Date: {0}", "Item # {0}:"));
        
            // Insert a field with a numeric format
            builder.insertField(" = 2 + 3 \\# $###", null);
        
            // Insert a field with a date/time format
            builder.insertField("DATE \\@ \"d MMMM yyyy\"", null);
        
            // Insert a field with a general format
            builder.insertField("QUOTE \"2\" \\* Ordinal", null);
        
            // Formats will be applied and recorded by the formatter during the field update
            doc.updateFields();
            ((FieldResultFormatter) doc.getFieldOptions().getResultFormatter()).printInvocations();
        
            // Our formatter has also overridden the formats that were originally applied in the fields
            Assert.assertEquals(doc.getRange().getFields().get(0).getResult(), "$5");
            Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
            Assert.assertEquals(doc.getRange().getFields().get(2).getResult(), "Item # 2:");
        }
        
        /// <summary>
        /// Custom IFieldResult implementation that applies formats and tracks format invocations
        /// </summary>
        private static class FieldResultFormatter implements IFieldResultFormatter {
            public FieldResultFormatter(final String numberFormat, final String dateFormat, final String generalFormat) {
                mNumberFormat = numberFormat;
                mDateFormat = dateFormat;
                mGeneralFormat = generalFormat;
            }
        
            public String formatNumeric(final double value, final String format) {
                mNumberFormatInvocations.add(new Object[]{value, format});
        
                return (mNumberFormat == null || "".equals(mNumberFormat)) ? null : MessageFormat.format(mNumberFormat, value);
            }
        
            public String formatDateTime(final Date value, final String format, final int calendarType) {
                mDateFormatInvocations.add(new Object[]{value, format, calendarType});
        
                return (mDateFormat == null || "".equals(mDateFormat)) ? null : MessageFormat.format(mDateFormat, value);
            }
        
            public String format(final String value, final int format) {
                return format((Object) value, format);
            }
        
            public String format(final double value, final int format) {
                return format((Object) value, format);
            }
        
            private String format(final Object value, final int format) {
                mGeneralFormatInvocations.add(new Object[]{value, format});
        
                return (mGeneralFormat == null || "".equals(mGeneralFormat)) ? null : MessageFormat.format(mGeneralFormat, value);
            }
        
            public void printInvocations() {
                System.out.println(MessageFormat.format("Number format invocations ({0}):", mNumberFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mNumberFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
        
                System.out.println(MessageFormat.format("Date format invocations ({0}):", mDateFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mDateFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1] + ", calendar type: " + s[2]);
                }
        
                System.out.println(MessageFormat.format("General format invocations ({0}):", mGeneralFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mGeneralFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
            }
        
            private String mNumberFormat;
            private String mDateFormat;
            private String mGeneralFormat;
        
            private ArrayList mNumberFormatInvocations = new ArrayList();
            private ArrayList mDateFormatInvocations = new ArrayList();
            private ArrayList mGeneralFormatInvocations = new ArrayList();
        
        }
      • format

        public abstract java.lang.String format(java.lang.String value, int format)
        Called when Aspose.Words applies a capitalization format switch, i.e. \* Upper. The implementation should return null to indicate that the default formatting should be applied.
        Parameters:
        format - A GeneralFormat value.

        Example:

        Shows how to control how the field result is formatted.
        public void insertCustomFormattingField() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            doc.getFieldOptions().setResultFormatter(new FieldResultFormatter("${0}", "Date: {0}", "Item # {0}:"));
        
            // Insert a field with a numeric format
            builder.insertField(" = 2 + 3 \\# $###", null);
        
            // Insert a field with a date/time format
            builder.insertField("DATE \\@ \"d MMMM yyyy\"", null);
        
            // Insert a field with a general format
            builder.insertField("QUOTE \"2\" \\* Ordinal", null);
        
            // Formats will be applied and recorded by the formatter during the field update
            doc.updateFields();
            ((FieldResultFormatter) doc.getFieldOptions().getResultFormatter()).printInvocations();
        
            // Our formatter has also overridden the formats that were originally applied in the fields
            Assert.assertEquals(doc.getRange().getFields().get(0).getResult(), "$5");
            Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
            Assert.assertEquals(doc.getRange().getFields().get(2).getResult(), "Item # 2:");
        }
        
        /// <summary>
        /// Custom IFieldResult implementation that applies formats and tracks format invocations
        /// </summary>
        private static class FieldResultFormatter implements IFieldResultFormatter {
            public FieldResultFormatter(final String numberFormat, final String dateFormat, final String generalFormat) {
                mNumberFormat = numberFormat;
                mDateFormat = dateFormat;
                mGeneralFormat = generalFormat;
            }
        
            public String formatNumeric(final double value, final String format) {
                mNumberFormatInvocations.add(new Object[]{value, format});
        
                return (mNumberFormat == null || "".equals(mNumberFormat)) ? null : MessageFormat.format(mNumberFormat, value);
            }
        
            public String formatDateTime(final Date value, final String format, final int calendarType) {
                mDateFormatInvocations.add(new Object[]{value, format, calendarType});
        
                return (mDateFormat == null || "".equals(mDateFormat)) ? null : MessageFormat.format(mDateFormat, value);
            }
        
            public String format(final String value, final int format) {
                return format((Object) value, format);
            }
        
            public String format(final double value, final int format) {
                return format((Object) value, format);
            }
        
            private String format(final Object value, final int format) {
                mGeneralFormatInvocations.add(new Object[]{value, format});
        
                return (mGeneralFormat == null || "".equals(mGeneralFormat)) ? null : MessageFormat.format(mGeneralFormat, value);
            }
        
            public void printInvocations() {
                System.out.println(MessageFormat.format("Number format invocations ({0}):", mNumberFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mNumberFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
        
                System.out.println(MessageFormat.format("Date format invocations ({0}):", mDateFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mDateFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1] + ", calendar type: " + s[2]);
                }
        
                System.out.println(MessageFormat.format("General format invocations ({0}):", mGeneralFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mGeneralFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
            }
        
            private String mNumberFormat;
            private String mDateFormat;
            private String mGeneralFormat;
        
            private ArrayList mNumberFormatInvocations = new ArrayList();
            private ArrayList mDateFormatInvocations = new ArrayList();
            private ArrayList mGeneralFormatInvocations = new ArrayList();
        
        }
      • formatDateTime

        public abstract java.lang.String formatDateTime(java.util.Date value, java.lang.String format, int calendarType)
        Called when Aspose.Words applies a date/time format switch, i.e. \@ "dd.MM.yyyy". The implementation should return null to indicate that the default formatting should be applied.
        Parameters:
        calendarType - A CalendarType value.

        Example:

        Shows how to control how the field result is formatted.
        public void insertCustomFormattingField() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            doc.getFieldOptions().setResultFormatter(new FieldResultFormatter("${0}", "Date: {0}", "Item # {0}:"));
        
            // Insert a field with a numeric format
            builder.insertField(" = 2 + 3 \\# $###", null);
        
            // Insert a field with a date/time format
            builder.insertField("DATE \\@ \"d MMMM yyyy\"", null);
        
            // Insert a field with a general format
            builder.insertField("QUOTE \"2\" \\* Ordinal", null);
        
            // Formats will be applied and recorded by the formatter during the field update
            doc.updateFields();
            ((FieldResultFormatter) doc.getFieldOptions().getResultFormatter()).printInvocations();
        
            // Our formatter has also overridden the formats that were originally applied in the fields
            Assert.assertEquals(doc.getRange().getFields().get(0).getResult(), "$5");
            Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
            Assert.assertEquals(doc.getRange().getFields().get(2).getResult(), "Item # 2:");
        }
        
        /// <summary>
        /// Custom IFieldResult implementation that applies formats and tracks format invocations
        /// </summary>
        private static class FieldResultFormatter implements IFieldResultFormatter {
            public FieldResultFormatter(final String numberFormat, final String dateFormat, final String generalFormat) {
                mNumberFormat = numberFormat;
                mDateFormat = dateFormat;
                mGeneralFormat = generalFormat;
            }
        
            public String formatNumeric(final double value, final String format) {
                mNumberFormatInvocations.add(new Object[]{value, format});
        
                return (mNumberFormat == null || "".equals(mNumberFormat)) ? null : MessageFormat.format(mNumberFormat, value);
            }
        
            public String formatDateTime(final Date value, final String format, final int calendarType) {
                mDateFormatInvocations.add(new Object[]{value, format, calendarType});
        
                return (mDateFormat == null || "".equals(mDateFormat)) ? null : MessageFormat.format(mDateFormat, value);
            }
        
            public String format(final String value, final int format) {
                return format((Object) value, format);
            }
        
            public String format(final double value, final int format) {
                return format((Object) value, format);
            }
        
            private String format(final Object value, final int format) {
                mGeneralFormatInvocations.add(new Object[]{value, format});
        
                return (mGeneralFormat == null || "".equals(mGeneralFormat)) ? null : MessageFormat.format(mGeneralFormat, value);
            }
        
            public void printInvocations() {
                System.out.println(MessageFormat.format("Number format invocations ({0}):", mNumberFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mNumberFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
        
                System.out.println(MessageFormat.format("Date format invocations ({0}):", mDateFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mDateFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1] + ", calendar type: " + s[2]);
                }
        
                System.out.println(MessageFormat.format("General format invocations ({0}):", mGeneralFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mGeneralFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
            }
        
            private String mNumberFormat;
            private String mDateFormat;
            private String mGeneralFormat;
        
            private ArrayList mNumberFormatInvocations = new ArrayList();
            private ArrayList mDateFormatInvocations = new ArrayList();
            private ArrayList mGeneralFormatInvocations = new ArrayList();
        
        }
      • formatNumeric

        public abstract java.lang.String formatNumeric(double value, java.lang.String format)
        Called when Aspose.Words applies a numeric format switch, i.e. \# "#.##". The implementation should return null to indicate that the default formatting should be applied.

        Example:

        Shows how to control how the field result is formatted.
        public void insertCustomFormattingField() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
        
            doc.getFieldOptions().setResultFormatter(new FieldResultFormatter("${0}", "Date: {0}", "Item # {0}:"));
        
            // Insert a field with a numeric format
            builder.insertField(" = 2 + 3 \\# $###", null);
        
            // Insert a field with a date/time format
            builder.insertField("DATE \\@ \"d MMMM yyyy\"", null);
        
            // Insert a field with a general format
            builder.insertField("QUOTE \"2\" \\* Ordinal", null);
        
            // Formats will be applied and recorded by the formatter during the field update
            doc.updateFields();
            ((FieldResultFormatter) doc.getFieldOptions().getResultFormatter()).printInvocations();
        
            // Our formatter has also overridden the formats that were originally applied in the fields
            Assert.assertEquals(doc.getRange().getFields().get(0).getResult(), "$5");
            Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
            Assert.assertEquals(doc.getRange().getFields().get(2).getResult(), "Item # 2:");
        }
        
        /// <summary>
        /// Custom IFieldResult implementation that applies formats and tracks format invocations
        /// </summary>
        private static class FieldResultFormatter implements IFieldResultFormatter {
            public FieldResultFormatter(final String numberFormat, final String dateFormat, final String generalFormat) {
                mNumberFormat = numberFormat;
                mDateFormat = dateFormat;
                mGeneralFormat = generalFormat;
            }
        
            public String formatNumeric(final double value, final String format) {
                mNumberFormatInvocations.add(new Object[]{value, format});
        
                return (mNumberFormat == null || "".equals(mNumberFormat)) ? null : MessageFormat.format(mNumberFormat, value);
            }
        
            public String formatDateTime(final Date value, final String format, final int calendarType) {
                mDateFormatInvocations.add(new Object[]{value, format, calendarType});
        
                return (mDateFormat == null || "".equals(mDateFormat)) ? null : MessageFormat.format(mDateFormat, value);
            }
        
            public String format(final String value, final int format) {
                return format((Object) value, format);
            }
        
            public String format(final double value, final int format) {
                return format((Object) value, format);
            }
        
            private String format(final Object value, final int format) {
                mGeneralFormatInvocations.add(new Object[]{value, format});
        
                return (mGeneralFormat == null || "".equals(mGeneralFormat)) ? null : MessageFormat.format(mGeneralFormat, value);
            }
        
            public void printInvocations() {
                System.out.println(MessageFormat.format("Number format invocations ({0}):", mNumberFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mNumberFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
        
                System.out.println(MessageFormat.format("Date format invocations ({0}):", mDateFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mDateFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1] + ", calendar type: " + s[2]);
                }
        
                System.out.println(MessageFormat.format("General format invocations ({0}):", mGeneralFormatInvocations.size()));
                for (Object[] s : (Iterable<Object[]>) mGeneralFormatInvocations) {
                    System.out.println("\tValue: " + s[0] + ", original format: " + s[1]);
                }
            }
        
            private String mNumberFormat;
            private String mDateFormat;
            private String mGeneralFormat;
        
            private ArrayList mNumberFormatInvocations = new ArrayList();
            private ArrayList mDateFormatInvocations = new ArrayList();
            private ArrayList mGeneralFormatInvocations = new ArrayList();
        
        }