将自定义格式应用于字段
有时,用户需要对字段应用自定义格式。在本文中,我们将通过几个示例来说明如何做到这一点。
要了解更多选项,请参阅 字段命名空间 中相应类中每种字段类型的属性的完整列表。
如何将自定义格式应用于字段结果
Aspose.Words 提供 API 来自定义字段结果的格式。您可以实现 IFieldResultFormatter 接口来控制字段结果的格式。您可以应用数字格式开关,即#"#.##",日期/时间格式开关,即@“dd.MM.yyyy”,以及数字格式开关,即* Ordinal。
以下代码示例显示如何对字段结果应用自定义格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
doc.FieldOptions.ResultFormatter = new FieldResultFormatter("[{0:N2}]", null); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Field field = builder.InsertField("=-1234567.89 \\# \"### ### ###.000\"", null); | |
field.Update(); | |
doc.Save(ArtifactsDir + "WorkingWithFields.FormatFieldResult.docx"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
class FieldResultFormatter : IFieldResultFormatter | |
{ | |
public FieldResultFormatter(string numberFormat, string dateFormat) | |
{ | |
mNumberFormat = numberFormat; | |
mDateFormat = dateFormat; | |
} | |
public FieldResultFormatter() | |
: this(null, null) | |
{ | |
} | |
public override string FormatNumeric(double value, string format) | |
{ | |
mNumberFormatInvocations.Add(new object[] { value, format }); | |
return string.IsNullOrEmpty(mNumberFormat) | |
? null | |
: string.Format(mNumberFormat, value); | |
} | |
public override string FormatDateTime(DateTime value, string format, CalendarType calendarType) | |
{ | |
mDateFormatInvocations.Add(new object[] { value, format, calendarType }); | |
return string.IsNullOrEmpty(mDateFormat) | |
? null | |
: string.Format(mDateFormat, value); | |
} | |
public override string Format(string value, GeneralFormat format) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override string Format(double value, GeneralFormat format) | |
{ | |
throw new NotImplementedException(); | |
} | |
private readonly string mNumberFormat; | |
private readonly string mDateFormat; | |
private readonly ArrayList mNumberFormatInvocations = new ArrayList(); | |
private readonly ArrayList mDateFormatInvocations = new ArrayList(); | |
} |
如何评估 IF
状况
如果要在 Mail Merge 后计算 IF
条件,可以使用立即返回表达式计算结果的 EvaluateCondition 方法。
以下代码示例展示了如何使用此方法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
FieldIf field = (FieldIf) builder.InsertField("IF 1 = 1", null); | |
FieldIfComparisonResult actualResult = field.EvaluateCondition(); | |
Console.WriteLine(actualResult); |
如何将自定义格式应用于时间字段
默认情况下,Aspose.Words 使用当前文化短时间格式更新 TIME
字段。我们发现 Microsoft Word 格式和 .NET/Windows 格式之间以及不同 .NET Framework 版本之间也存在差异。如果您想根据您的要求格式化 TIME
字段,您可以通过实现 IFieldUpdateCultureProvider 接口来实现。
以下代码示例显示如何将自定义格式应用于 TIME
字段:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertField(FieldType.FieldTime, true); | |
doc.FieldOptions.FieldUpdateCultureSource = FieldUpdateCultureSource.FieldCode; | |
doc.FieldOptions.FieldUpdateCultureProvider = new FieldUpdateCultureProvider(); | |
doc.Save(ArtifactsDir + "WorkingWithFields.FieldUpdateCulture.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
class FieldUpdateCultureProvider : IFieldUpdateCultureProvider | |
{ | |
public CultureInfo GetCulture(string name, Field field) | |
{ | |
switch (name) | |
{ | |
case "ru-RU": | |
CultureInfo culture = new CultureInfo(name, false); | |
DateTimeFormatInfo format = culture.DateTimeFormat; | |
format.MonthNames = new[] | |
{ | |
"месяц 1", "месяц 2", "месяц 3", "месяц 4", "месяц 5", "месяц 6", "месяц 7", "месяц 8", | |
"месяц 9", "месяц 10", "месяц 11", "месяц 12", "" | |
}; | |
format.MonthGenitiveNames = format.MonthNames; | |
format.AbbreviatedMonthNames = new[] | |
{ | |
"мес 1", "мес 2", "мес 3", "мес 4", "мес 5", "мес 6", "мес 7", "мес 8", "мес 9", "мес 10", | |
"мес 11", "мес 12", "" | |
}; | |
format.AbbreviatedMonthGenitiveNames = format.AbbreviatedMonthNames; | |
format.DayNames = new[] | |
{ | |
"день недели 7", "день недели 1", "день недели 2", "день недели 3", "день недели 4", | |
"день недели 5", "день недели 6" | |
}; | |
format.AbbreviatedDayNames = new[] | |
{ "день 7", "день 1", "день 2", "день 3", "день 4", "день 5", "день 6" }; | |
format.ShortestDayNames = new[] { "д7", "д1", "д2", "д3", "д4", "д5", "д6" }; | |
format.AMDesignator = "До полудня"; | |
format.PMDesignator = "После полудня"; | |
const string pattern = "yyyy MM (MMMM) dd (dddd) hh:mm:ss tt"; | |
format.LongDatePattern = pattern; | |
format.LongTimePattern = pattern; | |
format.ShortDatePattern = pattern; | |
format.ShortTimePattern = pattern; | |
return culture; | |
case "en-US": | |
return new CultureInfo(name, false); | |
default: | |
return null; | |
} | |
} | |
} |