Font issue while filling tag data in word document

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)

@mohitkumar1991

  1. DROP_DOWN_LIST, COMBO_BOX. If you select items in MS Word the font is also changed to Courier 10cpi. This if font specified in the Normal style.

  2. When you modify value of DATE SDT, you put a new run into it

run = new Run(document, value);

When a Run node is created from scratch default formatting is applied to it. In your case it is Courier 10cpi font specified in the Normal style.

Hi @alexey.noskov ,
As i am filling the tag data for other document fontNotIssue.docx which is also same and has same normal style font “Courier 10cpi”. But in this document font is working proper as coming calibri Please check for combobox field with text “Luminace Sunbeam Development Holdings, LLC”.
fontNotIssueGen.docx (19.7 KB)

fontNotIssue.docx (24.1 KB)

@mohitkumar1991 The documents are not actually the same. In the problematic document font is not specified in sdt rPr:

<w:sdt>
	<w:sdtPr>
		<w:rPr>
			<w:sz w:val="20"/>
			<w:szCs w:val="20"/>
			<w:highlight w:val="cyan"/>
		</w:rPr>

While in the fontNotIssue.docx it is specified:

<w:sdt>
	<w:sdtPr>
		<w:rPr>
			<w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:eastAsiaTheme="minorHAnsi" w:cstheme="minorBidi"/>
			<w:kern w:val="2"/>
			<w:highlight w:val="cyan"/>
			<w:lang w:val="en-IN"/>
			<w14:ligatures w14:val="standardContextual"/>
		</w:rPr>

So in the problematic document default font from Normal style is applied to the SDT content, and in the fontNotIssue.docx the font specified in sdt rPr is applied.

@alexey.noskov ,
as i checked the word file, it is showing the correct font ‘calibri’, if i click on words,
but if i click on starting of the word it is showing incorrect font as ‘Courier 10 cpi’.

attaching screenshot.
and after filling data incorrect font takes precedence.

Can we preserve correct font ‘calibri’, in this case.

@mohitkumar1991 You can explicitly specify content font of the SDT. For example, see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
for (StructuredDocumentTag sdt : (Iterable<StructuredDocumentTag>)doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true))
{
    if (sdt.getSdtType() == SdtType.DROP_DOWN_LIST || sdt.getSdtType() == SdtType.COMBO_BOX)
    {
        sdt.getContentsFont().setName("Calibri");
        SdtListItem tagDefaultValue = sdt.getListItems().get(0);
        sdt.getListItems().setSelectedValue(tagDefaultValue);
    }
}
doc.save("C:\\Temp\\out.docx");