We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting rich Text content control

LS,


I am trying to set data from an xml structured file into a Word document.
In this Word document i have put several content controls, varying from Rich text controls to date controls. But when running my code against Aspose Words, i get an java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.

I have attached the xmlfile, the word file to add the data in and my code.

What am i doign wrong?

Regards,

Jasper

Hi Jasper,


Thanks for your inquiry. The problem occurs because you are trying to append nodes at an incorrect position in DOM. Please apply the following changes in your code:
if (sdt.getSdtType() == SdtType.PLAIN_TEXT) {
if (nodename.trim().equalsIgnoreCase(sdt.getTag().trim()) || nodename.trim().equalsIgnoreCase(sdt.getTitle().trim())) {
System.out.println("plain text nodename: " + nodename.trim() + " – Tag: " + sdt.getTag().trim() + " — value: " + nodevalue);
Run run = new Run(document);
run.setText(nodevalue);
sdt.removeAllChildren();
sdt.appendChild(run);
}
} else if (sdt.getSdtType() == SdtType.RICH_TEXT) {
// System.out.println("Document contains a RICH TEXT object control named: "
// + sdt.getTitle());
if (nodename.trim().equalsIgnoreCase(sdt.getTag().trim()) || nodename.trim().equalsIgnoreCase(sdt.getTitle().trim())) {
System.out.println("Rich text nodename: " + nodename.trim() + " – Tag: " + sdt.getTag().trim() + " — value: " + nodevalue);
    <span style="background-color: rgb(255, 255, 0);"><font color="RED"><b>if</b></font> <font color="BLUE"><b>(</b></font>sdt<font color="BLUE"><b>.</b></font>getFirstChild<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getNodeType<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">=</font><font color="BLUE">=</font> NodeType<font color="BLUE"><b>.</b></font>CELL<font color="BLUE"><b>)</b></font> <font color="BLUE"><b>{</b></font>
        <font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Cell<font color="BLUE"><b>)</b></font> sdt<font color="BLUE"><b>.</b></font>getFirstChild<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>removeAllChildren<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        <font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Cell<font color="BLUE"><b>)</b></font> sdt<font color="BLUE"><b>.</b></font>getFirstChild<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>ensureMinimum<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Run run <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Run<font color="BLUE"><b>(</b></font>document<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        run<font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font>nodevalue<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        <font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Cell<font color="BLUE"><b>)</b></font> sdt<font color="BLUE"><b>.</b></font>getFirstChild<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getFirstParagraph<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font>run<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    <font color="BLUE"><b>}</b></font> <font color="RED"><b>else</b></font> <font color="BLUE"><b>{</b></font>
        sdt<font color="BLUE"><b>.</b></font>removeAllChildren<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Paragraph para <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Paragraph<font color="BLUE"><b>(</b></font>document<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        Run run <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Run<font color="BLUE"><b>(</b></font>document<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        run<font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font>nodevalue<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        para<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font>run<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        sdt<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font>para<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
    <font color="BLUE"><b>}</b></font></span>
<font color="BLUE"><b>}</b></font>

} else if (sdt.getSdtType() == SdtType.BIBLIOGRAPHY) {
System.out.println("Document contains a BIBLIOGRAPHY object control named: " + sdt.getTitle());
}

I hope, this helps.

Best regards,