As i am trying to update date format in traditional chinese locale (“zh-Hant”) using the following code attached template.
but format is not supported.
package com.sirionlabs;
import com.aspose.words.*;
import com.aspose.words.Font;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sirionlabs.model.EntityModel;
import com.sirionlabs.model.IdName;
import com.sirionlabs.service.TemplateDataUtilService;
import com.sirionlabs.util.AuthoringStringUtils;
import com.sirionlabs.util.ListUtils;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.io.FileInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
public class Main4 {
TemplateDataUtilService templateDataUtilService = new TemplateDataUtilService();
public static void main(String[] args) throws Exception {
String test[] = new String[1];
System.out.println(test);
License wordLicense = new License();
wordLicense.setLicense(new FileInputStream("/home/mohitkumar/IdeaProjects/contract-authoring/auto-tagging/target/test-classes/aspose-licence"));
Document document = new Document("/home/mohitkumar/Downloads/Date test CLause.docx");
Main4 main3 = new Main4();
Map<Integer, Object> idToValueMap = new HashMap<>();
idToValueMap.put(11144, "24-November-2024");
idToValueMap.put(11452, "25-November-2025");
idToValueMap.put(39545, "؋");
main3.updateTagsInDocument(document, idToValueMap);
document.save("/home/mohitkumar/Downloads/updatedTags.docx");
}
private String getValueForPlainCC(Object valueObj) throws Exception {
String value = "";
if (valueObj instanceof String) {
value = String.valueOf(valueObj);
} else if (valueObj instanceof List) {
List<IdName> values = new Gson().fromJson(new ObjectMapper().writeValueAsString(valueObj), new TypeToken<List<IdName>>() {
}.getType());
List<String> valueNames = new ArrayList<>();
for (IdName defaultValue : values) {
valueNames.add(defaultValue.getName());
}
value = StringUtils.join(valueNames, ", ");
} else {
value = new ObjectMapper().convertValue(valueObj, IdName.class).getName();
}
return value;
}
public Integer getTagIdFromContentControl(String contentControlId) {
Integer tagId;
if (AuthoringStringUtils.isUniqueCodeBasic(contentControlId)) {
EntityModel entityModel = templateDataUtilService.getEntityByCode(contentControlId);
tagId = entityModel.getEntityId();
} else {
try{
tagId = Integer.parseInt(contentControlId);
}catch (Exception ex){
return null;
}
}
return tagId;
}
public void updateTagsInDocument(Document document, Map<Integer, Object> idToValue) {
if (document == null) return;
ObjectMapper objectMapper = new ObjectMapper();
Gson gson = new Gson();
String dateFormat = "dd-MMMM-yyyy";
String entityDateFormat = "dd-MMMM-yyyy";
IdName dateLocaleInfo = new IdName(1028, "zh-Hant");
for (Object st : document.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
StructuredDocumentTag std = (StructuredDocumentTag) st;
if (AuthoringStringUtils.isUniqueCodeBasic(std.getTag())) {
String tagUniqueCode = std.getTag();
Integer tagId = this.getTagIdFromContentControl(tagUniqueCode);
if (tagId == null) {
continue;
}
Object valueObj = idToValue.get(tagId);
if (valueObj == null) {
continue;
}
try {
if (std.getSdtType() == SdtType.PLAIN_TEXT || std.getSdtType() == SdtType.DATE) {
if (valueObj != null) {
Font contentsFont = null;
String value = getValueForPlainCC(valueObj);
if (!StringUtils.isEmpty(value)) {
Run run = null;
if (std.getSdtType() == SdtType.DATE) {
std.setDateDisplayLocale(getLocaleInMultiLingualEnable(false, dateLocaleInfo, std));
std.setDateDisplayFormat(dateFormat);
std.setFullDate(this.getDateFromString(value, dateFormat));
std.getContentsFont().setHighlightColor(new Color(211, 211, 211));
if (std.getLevel() == MarkupLevel.INLINE && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Run) {
run = (Run) std.getFirstChild();
} else if (std.getLevel() == MarkupLevel.BLOCK && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Paragraph) {
Paragraph p = (Paragraph) std.getFirstChild();
if (p.getCount() == 0 || !(p.getFirstChild() instanceof Run)) {
continue;
}
run = (Run) p.getFirstChild();
} else if (std.getLevel() == MarkupLevel.CELL && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Cell) {
Cell c = (Cell) std.getFirstChild();
if (c.getCount() == 0 || !(c.getFirstChild() instanceof Paragraph)) {
continue;
}
Paragraph p = (Paragraph) c.getFirstChild();
if (p.getCount() == 0 || !(p.getFirstChild() instanceof Run)) {
continue;
}
run = (Run) p.getFirstChild();
}
} else if (std.getLevel() == MarkupLevel.INLINE && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Run) {
Run firstRunChild = (Run) std.getFirstChild();
//setFontBasedOnCondition(std, firstRunChild, contentsFont);
run = (Run) std.getFirstChild().deepClone(true);
std.removeAllChildren();
run.setText(value);
std.getChildNodes(NodeType.ANY, false).add(run);
} else if (std.getLevel() == MarkupLevel.BLOCK && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Paragraph) {
Paragraph p = (Paragraph) std.getFirstChild().deepClone(true);
if (p.getCount() == 0 || !(p.getFirstChild() instanceof Run)) {
continue;
}
std.removeAllChildren();
Run firstRunChild = (Run) p.getFirstChild();
// setFontBasedOnCondition(std, firstRunChild, contentsFont);
run = (Run) p.getFirstChild().deepClone(true);
p.removeAllChildren();
run.setText(value);
p.appendChild(run);
std.getChildNodes(NodeType.ANY, false).add(p);
} else if (std.getLevel() == MarkupLevel.CELL && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Cell) {
Cell c = (Cell) std.getFirstChild();
if (c.getCount() == 0 || !(c.getFirstChild() instanceof Paragraph)) {
continue;
}
Paragraph p = (Paragraph) c.getFirstChild();
if (p.getCount() == 0 || !(p.getFirstChild() instanceof Run)) {
continue;
}
Run firstRunChild = (Run) p.getFirstChild();
// setFontBasedOnCondition(std, firstRunChild, contentsFont);
run = (Run) p.getFirstChild().deepClone(true);
p.removeAllChildren();
run.setText(value);
p.appendChild(run);
} else {
std.removeAllChildren();
run = new Run(document, value);
// AsposeHelper.setFont(run.getFont(), templateStyle != null ? templateStyle.getGeneralFont() : null);
std.getChildNodes(NodeType.ANY, false).add(run);
}
run.getFont().setHighlightColor(new Color(211, 211, 211));
Color color = std.getContentsFont().getColor();
run.getFont().setColor(color);
if (std.getSdtType() != SdtType.DATE) {
std.isShowingPlaceholderText(false);
if (contentsFont != null) {
//AsposeHelper.setFontFromRunFont(run.getFont(), contentsFont);
}
//AsposeHelper.setFontFromRunFont(std.getContentsFont(), run.getFont());
}
}
}
} else if (std.getSdtType() == SdtType.DROP_DOWN_LIST) {
IdName value = gson.fromJson(gson.toJson(valueObj), IdName.class);
//convertIdnameAccordingToLanguageForSSTag(tagToEntityTypeIdMap, multiLingualEnable, std, tagId, value);
//selectValueForComplexCC(templateStyle, std, value.getName(), String.valueOf(value.getId()));
} else if (std.getSdtType() == SdtType.COMBO_BOX) {
List<IdName> values = new Gson().fromJson(objectMapper.writeValueAsString(valueObj), new TypeToken<List<IdName>>() {
}.getType());
//convertIdnameAccordingToLanguageForMSTag(tagToEntityTypeIdMap, multiLingualEnable, std, tagId, values);
List<String> valueNames = new ArrayList<>();
List<String> valueIds = new ArrayList<>();
for (IdName value : values) {
valueNames.add(value.getName());
valueIds.add(String.valueOf(value.getId()));
}
if (ListUtils.isEmpty(valueNames) || ListUtils.isEmpty(valueIds)) {
continue;
}
//selectValueForComplexCC(templateStyle, std, StringUtil.join(valueNames, ", "), StringUtil.join(valueIds, ","));
}
} catch (Exception exception) {
//log.error("Unable to update clause tag : {} due to: ", tagUniqueCode, exception);
}
}
}
}
public static Date getDateFromString(String dateString, String dateFormat) {
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Date date = null;
try {
date = formatter.parse(dateString);
} catch (ParseException e) {
}
return date;
}
private Integer getLocaleInMultiLingualEnable(boolean multiLingualEnable, IdName localeInfo, StructuredDocumentTag std) {
if (multiLingualEnable && std.getPlaceholder() != null) {
String placeHolderText = std.getPlaceholder().getText().replace("\f", "");
if (StringUtils.isNumeric(placeHolderText)) {
return Integer.parseInt(placeHolderText);
}
}
if (localeInfo != null && localeInfo.getId() != null) {
return localeInfo.getId();
}
return 1033;//Default for english
}
}
COPortingFile.docx (96.8 KB)