Hi team,
Font and size of date tags are getting changes when we are updating the tag value.
below are my code and 3 document attached in zip.
public static void changeMethod(Document document) throws Exception {
if (document == null) return;
ObjectMapper objectMapper = new ObjectMapper();
Gson gson = new Gson();
for (Object st : document.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
StructuredDocumentTag std = (StructuredDocumentTag) st;
String tagUniqueCode = std.getTag();
Object valueObj = "06-08-2024";
if (valueObj == null) {
continue;
}
try {
if ((std.getSdtType() == SdtType.PLAIN_TEXT || std.getSdtType() == SdtType.DATE)) {
if (valueObj != null) {
Font contentsFont = null;
String value = (String) (valueObj);
if (std.getSdtType() == SdtType.DATE) {
contentsFont = std.getContentsFont();
std.setDateDisplayFormat("dd-mm-yyyy");
setFontFromRunFont(std.getContentsFont(), contentsFont);
}
if (!StringUtil.isEmpty(value)) {
Run run = null;
if (std.getLevel() == MarkupLevel.INLINE && std.getChildNodes(NodeType.ANY, false).getCount() > 0 && std.getFirstChild() instanceof Run) {
Run firstRunChild = (Run) std.getFirstChild();
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();
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();
run = (Run) p.getFirstChild().deepClone(true);
p.removeAllChildren();
run.setText(value);
p.appendChild(run);
} else {
if (std.getSdtType() == SdtType.DATE) {
contentsFont = std.getContentsFont();
}
std.removeAllChildren();
run = new Run(document, value);
setFont(run.getFont(), null);
std.getChildNodes(NodeType.ANY, false).add(run);
}
std.isShowingPlaceholderText(false);
run.getFont().setHighlightColor(new Color(211, 211, 211));
Color color = std.getContentsFont().getColor();
run.getFont().setColor(color);
if (contentsFont != null) {
setFontFromRunFont(run.getFont(), contentsFont);
}
setFontFromRunFont(std.getContentsFont(), run.getFont());
}
}
}
} catch (Exception exception) {
}
}
document.save("/home/hari/Downloads/420_DS_EN.docx");
}
public static void setFont(Font font, FontSelection fontSelection) {
if (fontSelection != null) {
font.setBold(fontSelection.isBold());
font.setItalic(fontSelection.isItalic());
font.setUnderline(fontSelection.isUnderline() ? 1 : 0);
setFontNameAndSize(font, fontSelection);
}
}
public static void setFontFromRunFont(Font destinationFont, Font sourceFont) {
if (sourceFont != null && destinationFont != null) {
destinationFont.setBold(sourceFont.getBold());
destinationFont.setItalic(sourceFont.getItalic());
destinationFont.setUnderline(sourceFont.getUnderline());
destinationFont.setName(sourceFont.getName());
destinationFont.setSize(sourceFont.getSize());
}
}
private static void setFontNameAndSize(Font font, FontSelection fontSelection) {
font.setName(fontSelection.getFont().getName());
font.setSize(fontSelection.getFontSize());
}
test doc.zip (61.7 KB)