I have a rich text box contentControl in Word template. I'm trying to insert RTF data to the ContentControl and generate a word document out of it. I tried with AltChunk as told here. This will work with SdtBlock. Since the parent of SdtBlock is Body, we can Insert the AltChunk to the body directly. If word document has multiple rich text box contentControls, then word saves the control as SdtRun. The parent to SdtRun is Paragraph and it's parent is Body. If we try to do something like
SdtRun contentControl = (SdtRun)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<SdtRun>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox"));
contentControl.Parent.Parent.InsertAfter(altChunk, contentControl); //contentControl.Parent.Parent - Returns body of document
throws exception Operation is not valid due to the current state of the object
So I tried to use Paragraph
Paragraph contentControl = (Paragraph)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<Paragraph>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox"));
contentControl.Parent.InsertAfter(altChunk, contentControl); //contentControl.Parent - Returns body of document
This code is inserting data in RTF format. But the problem is I cannot insert to the proper position of Rich TextControl, as we are operating on Paragraph.
Full code is posted below
string templatePath = @"Template1.docx";
string newFile = @"Template1_Processed.docx";
System.IO.File.Copy(templatePath, newFile, true);
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(newFile, true))
{
Paragraph sdtElement = (Paragraph)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<Paragraph>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox"));
string innerText = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}{\colortbl ;\red139\green0\blue0;}\viewkind4\uc1\pard\cf1\f0\fs24 test\par}";
string altChunkId = "myId";
MainDocumentPart mainDocPart = wordprocessingDocument.MainDocumentPart;
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(innerText));
// Create alternative format import part.
AlternativeFormatImportPart formatImportPart =
mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Rtf, altChunkId);
// Feed HTML data into format import part (chunk).
formatImportPart.FeedData(ms);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
sdtElement.Parent.InsertAfter<AltChunk>(altChunk, sdtElement);
SdtElement contentControl = (SdtElement)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<SdtElement>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox"));
contentControl.Remove();
wordprocessingDocument.MainDocumentPart.CreateRelationshipToPart(formatImportPart);
sdtElement = (Paragraph)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<Paragraph>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox1"));
if (sdtElement != null)
{
innerText = @"<html><head></head><body><h1>HELLO</h1></body></html>";
altChunkId = "myId1";
ms = new MemoryStream(Encoding.UTF8.GetBytes(innerText));
// Create alternative format import part.
formatImportPart =
mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html, altChunkId);
// Feed HTML data into format import part (chunk).
formatImportPart.FeedData(ms);
altChunk = new AltChunk();
altChunk.Id = altChunkId;
sdtElement.Parent.InsertAfter(altChunk, sdtElement);
contentControl = (SdtElement)wordprocessingDocument.MainDocumentPart.RootElement.Descendants<SdtElement>().FirstOrDefault(x => x.Descendants<Tag>().Any(y => y.Val == "richtextbox1"));
contentControl.Remove();
wordprocessingDocument.MainDocumentPart.CreateRelationshipToPart(formatImportPart);
wordprocessingDocument.MainDocumentPart.Document.Save();
wordprocessingDocument.Close();
}
}
Attaching the screen shots of Template
and generated word document

Since we are inserting after to Paragraph, It is getting inserted very next to the Paragraph. So we cannot insert the data to the proper position where rich text data was there in the document.
Can anybody help me to fix this / suggest any alternate method to implement the same?