Read html value in the RichText Control in a 'Word Template'

Hi,

I am using Aspose.words for java v16.1.0.

Working with RichText Control,
I don’t see the option for reading html value in the RichText Control.

I need to read html value in the RichText Control in a ‘Word Template’,
programmatic read html value in the RichText Control.

Is there any way to programmatic read html value in the RichText Control ?
Please suggest.

Thanks
Naseer

Hi Naseer,

Thanks for your inquiry. Could you please attach your input Word document and expected html output here for our reference? We will then provide you more information on this along with code.

String htmlData = "<p><strong>New media</strong> most commonly refers to content available on-demand through the</p>"
                            + "<p><strong>Internet</strong>, accessible on any digital device, usually containing interactive user feedback and creative participation. Common examples of new media include websites such as online</p>"
                            + "<ul><li>newspapers, </li><li>blogs, or wikis, </li><li>social media.</li></ul>"
                            + "<p>New Media transmit content through connection and conversation. It enables people around the world to share, comment on, and discuss a wide variety of topics. Unlike any of past technologies, New Media is grounded on an interactive community.</p>"
                            + "<p><span style='background-color:rgb(255, 255, 255); color:rgb(37, 37, 37)'><strong>Newspapers </strong>are typically published </span>daily<span style='background-color:rgb(255, 255, 255); color:rgb(37, 37, 37)'> or </span>weekly.</p>"
                            + "<p><span style='background-color:rgb(255, 255, 255); color:rgb(37, 37, 37)'>A </span><strong>television program</strong><span style='background-color:rgb(255, 255, 255); color:rgb(37, 37, 37)'> is a segment of content intended for broadcast on over-the-air, cable television.</span></p>";

if (tag.getLevel() == MarkupLevel.BLOCK)
{
    tag.removeAllChildren();

    // Now, create an empty paragraph and append it to the content control.
    tag.appendChild(new Paragraph(doc));
    // Move DocumentBuilder cursor to the first child of SDT (newly created paragraph).
    builder.moveTo(tag.getFirstChild());
    // Insert some HTML content.
    builder.insertHtml((String)htmlData);
}

Using DocumentBuilder.insertHtml(), i could able to populate ‘htmlData’ into the RichText control.

but when i want to read the RichText control with ‘htmlData’ as you can see in the ‘name_Out.docx’ file (‘TestWord Template_Out.docx’).

StructuredDocumentTag tag;
switch (tag.getSdtType())
{
    case SdtType.RICH_TEXT:
        // read RichText content control
        // don't find any html read method need help
        // tag.readHtml() Suggestion to read html method for RichText Control

        val = tag.getText();

        break;
}

tag.getText() method don’t give ‘htmlData’ in return.

Is there any way to programmatic read html value in the RichText Control ?
Please suggest.

Thanks
Naseer

Hi Naseer,

Thanks for sharing the detail. Please use Node.toString(SaveFormat.HTML) method to export the content of node into string in Html format. Please check following code snippet for your kind reference. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
// Your code...
StructuredDocumentTag sdt = (StructuredDocumentTag )doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
String html = sdt.toString(SaveFormat.HTML);
System.out.println(html);

Hi Tahir,

Thanks for the solution, it worked.