Question

I have this situation:

The user has an editor on his page and he enters text(with colors, formating, hyperlinks and he can also add pictures). When he clicks Submit the data from the editor(with the proper formating) must be sent to a specific placeholder in a Microsoft Office Word document.

I am using OpenXml SDK to write in the document and I tried HtmlToOpenXml so I can read the html.

I use HtmlToOpenXml and from the html string(from the user) I det a couple of paragraphs and now I have to insert them in the content control. Do you know how can I find the control and append them in it(if possible)

Was it helpful?

Solution

I managed to fix this and here is the code I used

            //name of the file which will be saved
            const string filename = "test.docx";
            //html string to be inserted and rendered in the word document
            string html = @"<b>Test</b>"; 
            //the Html2OpenXML dll supports all the common html tags

            //open the template document with the content controls in it(in my case I used Richtext Field Content Control)
            byte[] byteArray = File.ReadAllBytes("..."); // template path

            using (MemoryStream generatedDocument = new MemoryStream())
            {
                generatedDocument.Write(byteArray, 0, byteArray.Length);

                using (WordprocessingDocument doc = WordprocessingDocument.Open(generatedDocument, true))
                {

                    MainDocumentPart mainPart = doc.MainDocumentPart;
                    //just in case
                    if (mainPart == null)
                    {
                        mainPart = doc.AddMainDocumentPart();
                        new Document(new Body()).Save(mainPart);
                    }

                    HtmlConverter converter = new HtmlConverter(mainPart);
                    Body body = mainPart.Document.Body;

                    //sdtElement is the Content Control we need. 
                    //Html is the name of the placeholder we are looking for
                    SdtElement sdtElement = doc.MainDocumentPart.Document.Descendants<SdtElement>()
                        .Where(
                            element =>
                            element.SdtProperties.GetFirstChild<SdtAlias>() != null &&
                            element.SdtProperties.GetFirstChild<SdtAlias>().Val == "Html").FirstOrDefault();

                    //the HtmlConverter returns a set of paragraphs.
                    //in them we have the data which we want to insert in the document with it's formating
                    //After that we just need to append all paragraphs to the Content Control and save the document
                    var paragraphs = converter.Parse(html);

                    for (int i = 0; i < paragraphs.Count; i++)
                    {
                        sdtElement.Append(paragraphs[i]);
                    }

                    mainPart.Document.Save();
                }

                File.WriteAllBytes(filename, generatedDocument.ToArray());
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top