اعمال قالب بندی سفارشی به زمینه ها
گاهی اوقات کاربران باید قالب بندی سفارشی را به زمینه ها اعمال کنند. در این مقاله، ما به چند نمونه از چگونگی انجام این کار نگاه خواهیم کرد.
برای کسب اطلاعات بیشتر در مورد گزینه ها، لیست کامل ویژگی های هر نوع فیلد در کلاس مربوطه را ببینید.
چگونه قالب بندی سفارشی را به نتیجه فیلد اعمال کنیم
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-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
Document document = builder.getDocument(); | |
Field field = builder.insertField("=-1234567.89 \\# \"### ### ###.000\"", null); | |
document.getFieldOptions().setResultFormatter(new FieldResultFormatter("[%0$s]", null)); | |
field.update(); | |
document.save(dataDir+"FormatFieldResult_out.docx"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public class FieldResultFormatter implements IFieldResultFormatter { | |
private final String mNumberFormat; | |
private final String mDateFormat; | |
private final ArrayList mNumberFormatInvocations = new ArrayList(); | |
private final ArrayList mDateFormatInvocations = new ArrayList(); | |
public FieldResultFormatter(String numberFormat, String dateFormat) { | |
mNumberFormat = numberFormat; | |
mDateFormat = dateFormat; | |
} | |
public FieldResultFormatter() { | |
mNumberFormat = null; | |
mDateFormat = null; | |
} | |
public String format(String arg0, int arg1) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
public String format(double arg0, int arg1) { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
public String formatNumeric(double value, String format) { | |
// TODO Auto-generated method stub | |
mNumberFormatInvocations.add(new Object[]{value, format}); | |
return (mNumberFormat.isEmpty() || mNumberFormat == null) ? null | |
: String.format(mNumberFormat, value); | |
} | |
public String formatDateTime(Date value, String format, int calendarType) { | |
mDateFormatInvocations | |
.add(new Object[]{value, format, calendarType}); | |
return (mDateFormat.isEmpty() || mDateFormat == null) ? null : String | |
.format(mDateFormat, value); | |
} |
چگونه وضعیت IF
را ارزیابی کنیم
اگر می خواهید شرایط IF
را پس از mail merge ارزیابی کنید، می توانید از روش EvaluateCondition استفاده کنید که بلافاصله نتیجه ارزیابی عبارت را باز می گرداند.
مثال کد زیر نشان می دهد که چگونه از این روش استفاده کنید:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
DocumentBuilder builder = new DocumentBuilder(); | |
FieldIf field = (FieldIf) builder.insertField("IF 1 = 1", null); | |
int actualResult = field.evaluateCondition(); | |
System.out.println(actualResult); |
چگونه قالب بندی سفارشی را به فیلد زمان اعمال کنیم
به طور پیش فرض Aspose.Words به روز رسانی TIME
زمینه با فرهنگ فعلی فرمت کوتاه مدت. اگر می خواهید فیلد TIME
را با توجه به نیاز خود فرمت کنید، می توانید با پیاده سازی رابط IFieldUpdateCultureProvider به این هدف برسید.
نمونه های کد زیر نشان می دهد که چگونه قالب بندی سفارشی را به فیلد TIME
اعمال کنیم:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.insertField(FieldType.FIELD_TIME, true); | |
doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE); | |
doc.getFieldOptions().setFieldUpdateCultureProvider(new FieldUpdateCultureProvider()); | |
doc.save(getArtifactsDir() + "WorkingWithFields.FieldUpdateCulture.pdf"); |
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static class FieldUpdateCultureProvider implements IFieldUpdateCultureProvider | |
{ | |
public CultureInfo getCulture(String name, Field field) | |
{ | |
switch (name) | |
{ | |
case "ru-RU": | |
CultureInfo culture = new CultureInfo(new Locale(name)); | |
DateTimeFormatInfo format = culture.getDateTimeFormat(); | |
format.setMonthNames(new String[] | |
{ | |
"месяц 1", "месяц 2", "месяц 3", "месяц 4", "месяц 5", "месяц 6", "месяц 7", "месяц 8", | |
"месяц 9", "месяц 10", "месяц 11", "месяц 12", "" | |
}); | |
format.setMonthGenitiveNames(format.getMonthNames()); | |
format.setAbbreviatedMonthNames(new String[] | |
{ | |
"мес 1", "мес 2", "мес 3", "мес 4", "мес 5", "мес 6", "мес 7", "мес 8", "мес 9", "мес 10", | |
"мес 11", "мес 12", "" | |
}); | |
format.setAbbreviatedMonthGenitiveNames(format.getAbbreviatedMonthNames()); | |
format.setDayNames(new String[] | |
{ | |
"день недели 7", "день недели 1", "день недели 2", "день недели 3", "день недели 4", | |
"день недели 5", "день недели 6" | |
}); | |
format.setAbbreviatedDayNames(new String[] | |
{ "день 7", "день 1", "день 2", "день 3", "день 4", "день 5", "день 6" }); | |
format.setShortestDayNames(new String[] { "д7", "д1", "д2", "д3", "д4", "д5", "д6" }); | |
format.setAMDesignator("До полудня"); | |
format.setPMDesignator("После полудня"); | |
final String PATTERN = "yyyy MM (MMMM) dd (dddd) hh:mm:ss tt"; | |
format.setLongDatePattern(PATTERN); | |
format.setLongTimePattern(PATTERN); | |
format.setShortDatePattern(PATTERN); | |
format.setShortTimePattern(PATTERN); | |
return culture; | |
case "en-US": | |
return new CultureInfo(new Locale(name)); | |
default: | |
return null; | |
} | |
} | |
} |