Hi,
I am trying to update document tag data in document, it has been found that for some of tags like DROP_DOWN_LIST, COMBO_BOX, Date type, font has been updated from ‘calibri’ to ‘Courier 10cpi’.
package com.sirionlabs.api;
import com.aspose.words.Font;
import com.aspose.words.*;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.io.FileInputStream;
import java.io.InputStream;
public class Main4 {
public static void main(String... args) throws Exception {
com.aspose.words.License license = new com.aspose.words.License();
license.setLicense(new FileInputStream("/home/mohitkumar/IdeaProjects/contract-authoring/auto-tagging/target/test-classes/aspose-licence"));
Document document;
InputStream stream = null;
try {
document = new Document("/home/mohitkumar/Downloads/FontIsseDoc.docx");
} catch (Exception ex) {
throw new Exception("");
} finally {
if (stream != null) {
stream.close();
}
}
for (Object st : document.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {
StructuredDocumentTag std = (StructuredDocumentTag) st;
if (std.getTag().endsWith("206__206")) {
//String tagUniqueCode = std.getTag();
Object valueObj = "Saurabh";
/*if (valueObj == null) {
continue;
}*/
try {
if (std.getSdtType() == SdtType.PLAIN_TEXT || std.getSdtType() == SdtType.DATE) {
if (valueObj != null) {
Font contentsFont = null;
String value = "Saurabh123";
if (!StringUtils.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();
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 {
if (std.getSdtType() == SdtType.DATE) {
contentsFont = std.getContentsFont();
}
std.removeAllChildren();
run = new Run(document, value);
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 (std.getSdtType() != SdtType.DATE) {
std.isShowingPlaceholderText(false);
if (contentsFont != null) {
setFontFromRunFont(run.getFont(), contentsFont);
}
setFontFromRunFont(std.getContentsFont(), run.getFont());
}
}
}
} else if (std.getSdtType() == SdtType.DROP_DOWN_LIST) {
System.out.println(std.getPlaceholder().getText());
std.getListItems().add(new SdtListItem("Luminace Sunbeam Development Holdings, LLC", "680198"));
std.getListItems().removeAt(0);
NodeCollection childNodes = std.getChildNodes(NodeType.RUN, true);
std.getContentsFont().setHighlightColor(new Color(211, 211, 211));
SdtListItem tagDefaultValue = std.getListItems().get(0);
std.getListItems().setSelectedValue(tagDefaultValue);
} else if (std.getSdtType() == SdtType.COMBO_BOX) {
System.out.println(std.getPlaceholder().getText());
NodeCollection childNodes = std.getChildNodes(NodeType.RUN, true);
std.getListItems().add(new SdtListItem("Luminace Sunbeam Development Holdings, LLC", "680198"));
std.getListItems().removeAt(0);
std.getContentsFont().setHighlightColor(new Color(211, 211, 211));
SdtListItem tagDefaultValue = std.getListItems().get(0);
std.getListItems().setSelectedValue(tagDefaultValue);
}
} catch (Exception exception) {
}
}
}
document.save("/home/mohitkumar/Downloads/test_mohit.docx");
}
private static void setFontBasedOnCondition(StructuredDocumentTag std, Run firstRunChild, Font contentsFont) {
String text = StringUtils.isEmpty(std.getText()) ? firstRunChild.getText() : std.getText();
if (std.getSdtType() == SdtType.DATE && !StringUtils.isEmpty(text) && !text.startsWith("Click or tap")) {
setFontFromRunFont(contentsFont, firstRunChild.getFont());
}
}
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());
}
}
}
FontIsseDoc.docx (23.8 KB)
FontIsseDoc.docx (23.8 KB)
fontIssueGen.docx (19.3 KB)