ใช้การจัดรูปแบบแบบกำหนดเองกับฟิลด์

บางครั้งผู้ใช้จำเป็นต้องใช้การจัดรูปแบบแบบกำหนดเองกับฟิลด์ ในบทความนี้ เราจะดูตัวอย่างสองสามตัวอย่างว่าสามารถทำได้อย่างไร

หากต้องการเรียนรู้ตัวเลือกเพิ่มเติม โปรดดูรายการคุณสมบัติทั้งหมดสำหรับช่องแต่ละประเภทในคลาสที่เกี่ยวข้องใน เนมสเปซของฟิลด์

วิธีการใช้การจัดรูปแบบแบบกำหนดเองกับผลลัพธ์ของฟิลด์

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

หากคุณต้องการประเมินเงื่อนไข IF หลังจาก Mail Merge คุณสามารถใช้วิธี 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;
}
}
}