Insert html to content control Issue

Hi Team,

I am trying to insert html in content control. The problem is if the html contains paragraph/table then the result is not displayed inside content control
e.g

<p>This is a test</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>How is evrything</p>
<p>&nbsp;</p>

Note : We are trying to import data from UI to word document. In UI we have rich text editor where user can press enter in between lines .And also user can copy anything and paste in the rich text editor it could be a table but while importing that data into word using aspose we are not getting the desired output.

How we can achieve this?

newsdt = new StructuredDocumentTag(e.Document, SdtType.RichText , MarkupLevel.Inline);
 Run r = new Run(document);
r.Text = "";
newsdt.AppendChild(r);
mBuilder.MoveTo(newsdt.FirstChild); 
mBuilder.InsertHtml(result);

This works only with simple html not with paragraph/table.

@pratom01
Thanks for your inquiry. We will appreciate it if you please attach the following resources as zip file here for testing. We will look into it and will guide you accordingly.
Please attach the output Word file that shows the undesired behavior.
Please attach the expected output Word file that shows the desired behavior.
Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

Hi Team,
Below is the url where you can download the solution.
https://drive.google.com/file/d/0B2JlVEwfXSMeUU1FS296NTlfdk0/view?usp=sharing
I have a merge field in the document and I am trying to replace the merge field with content control and put some html inside the content control.
This is a web application but you should not have any problem running it.
The data folder contains the expected output.
It contains 3 files input,output and expected output.

@pratom01
Thanks for sharing the additional information. If you want to add HTML text in SDT, it should be Block level node. Please check following code snippet. Hopefully it will help you to accomplish the task.output.zip (10.9 KB)

Document doc = new Document(inputpath);
ArrayList nodes = new ArrayList();
DocumentBuilder mBuilder = new DocumentBuilder(doc);

mBuilder.MoveToMergeField("Name");
StructuredDocumentTag newsdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(newsdt);
newsdt.IsShowingPlaceholderText = false;
newsdt.RemoveAllChildren();

Paragraph para = new Paragraph(doc);
Run run = new Run(doc);
run.Text = "";
para.AppendChild(run);
newsdt.AppendChild(para);
mBuilder.MoveTo(newsdt.FirstChild);
mBuilder.InsertHtml(html);
doc.Save(outputpath);

Hi Team,

Thanks for the response.

The content control is not inserted in the position where the merge field exists.Now I have added a new merge field in the 3rd column of a table and its not working as expected.

Uploaded the input,output and expected output files.Data.zip (39.9 KB)

Note:The content control needs to be inserted in the position of mergefield.

string html = "<p>&nbsp;</p><p>This is a test</p><p>Next Para1</p><p>&nbsp;</p><p>Next Para2</p><p>&nbsp;</p><p>&nbsp;</p>";
string html1 = "<div><h3>This is a heading</h3>  </div>";
string inputpath = Server.MapPath("~/Data/input.docx");
string outputpath = Server.MapPath("~/Data/output.docx");
Document doc = new Document(inputpath);
ArrayList nodes = new ArrayList();
DocumentBuilder mBuilder = new DocumentBuilder(doc);

mBuilder.MoveToMergeField("Name");
StructuredDocumentTag newsdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(newsdt);
newsdt.IsShowingPlaceholderText = false;
newsdt.RemoveAllChildren();

Paragraph para = new Paragraph(doc);
Run run = new Run(doc);
run.Text = "";
para.AppendChild(run);
newsdt.AppendChild(para);
mBuilder.MoveTo(newsdt.FirstChild);
mBuilder.InsertHtml(html);


mBuilder.MoveToMergeField("Name1");
StructuredDocumentTag newsdt1 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(newsdt1);
newsdt1.IsShowingPlaceholderText = false;
newsdt1.RemoveAllChildren();

Paragraph para1 = new Paragraph(doc);
Run run1 = new Run(doc);
run1.Text = "";
para1.AppendChild(run1);
newsdt1.AppendChild(para1);
mBuilder.MoveTo(newsdt1.FirstChild);
mBuilder.InsertHtml(html1);

doc.Save(outputpath);

@pratom01

Thanks for your feedback. You may achieve your required output with implementation of IFieldMergingCallBack as following. Hopefully it will help.

    string inputpath = (@"input.docx");
    string outputpath = (@"output.docx");

    Document document = new Document(inputpath);
    document.MailMerge.FieldMergingCallback
    = new HandleMergeField();

    document.MailMerge.Execute(new string[] { "Name","Name1" }, new object[] { "", ""});
    document.Save("E:/Data/Output_179.docx");
    ///////////////////////////////////
    public class HandleMergeField : IFieldMergingCallback

    {

    ///

    /// This handler is called for every mail merge field found in the document,

    /// for every record found in the data source.

    ///

    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

    {
        if (e.FieldName == "Name")
        {
            string html = "<p>&nbsp;</p><p>This is a test</p><p>Next Para1</p><p>&nbsp;</p><p>Next Para2</p><p>&nbsp;</p><p>&nbsp;</p>";
            StructuredDocumentTag sdt = new StructuredDocumentTag(e.Document, SdtType.RichText, MarkupLevel.Block);
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            
            // Get actual merge field
            FieldMergeField mergeField = (FieldMergeField)e.Field;


            // Insert SDT after this merge field
            Paragraph para1 = mergeField.Start.ParentParagraph;
            para1.ParentNode.InsertAfter(sdt, para1);
            
            sdt.IsShowingPlaceholderText = false;
            sdt.RemoveAllChildren();
            Paragraph para = new Paragraph(e.Document);
            Run run = new Run(e.Document);
            run.Text = "";
            para.AppendChild(run);
            sdt.AppendChild(para);
            builder.MoveTo(sdt.FirstChild);
            builder.InsertHtml(html);

            //Remove merge field
            mergeField.Remove();
        }

        if (e.FieldName == "Name1")
        {
            string html = "<h1>Heading 1</h1>";
            StructuredDocumentTag sdt = new StructuredDocumentTag(e.Document, SdtType.RichText, MarkupLevel.Block);
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            
            // Get actual merge field
            FieldMergeField mergeField = (FieldMergeField)e.Field;
            // Insert SDT after this merge field

            Paragraph para1 = mergeField.Start.ParentParagraph;
            para1.ParentNode.InsertAfter(sdt, para1);
            
            sdt.IsShowingPlaceholderText = false;
            sdt.RemoveAllChildren();

            Paragraph para = new Paragraph(e.Document);
            Run run = new Run(e.Document);
            run.Text = "";
            para.AppendChild(run);
            sdt.AppendChild(para);
            builder.MoveTo(sdt.FirstChild);
            builder.InsertHtml(html);

            //Remove merge field
            mergeField.Remove();
        }

    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)

    {

    // Do nothing.

    }

    }

That was cool and worked for me.
Thanks