필드에 사용자 지정 서식 적용
때때로 사용자는 필드에 사용자 정의 서식을 적용해야합니다. 이 기사에서는 이것이 어떻게 수행 될 수 있는지에 대한 몇 가지 예를 살펴볼 것입니다.
자세한 내용은 해당 클래스의 각 필드 유형에 대한 전체 속성 목록을 참조하십시오.
필드 결과에 사용자 지정 서식을 적용하는 방법
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
조건을 평가하는 방법
Mail merge다음에IF
조건을 계산하려면 식 계산 결과를 즉시 반환하는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; | |
} | |
} | |
} |