I've setup Rich Text Box in InfoPath form, my program parses through the Infopath XML as below:
XPathNavigator formNameNode = root.SelectSingleNode("/my:myFields/my:Responses/my:Q1", nsMgr);
string response1 = formNameNode.InnerXml;
The following code is then used to open a word document and get a Plain Text Content Control called response1:
using (WordprocessingDocument myDoc =
WordprocessingDocument.Open(ms, true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
List<OpenXmlElement> sdtList = InfoPathToWord.GetContentControl(mainPart.Document, "response1");
InfoPathToWord.AddRichText(0, response1, ref mainPart, ref sdtList);
}
The code then calls InfoPathToWord.AddRichText which is as below:
public static void AddRichText(int id, string rtfValue,
ref MainDocumentPart mainPart, ref List<OpenXmlElement> sdtList)
{
if (sdtList.Count != 0)
{
id++;
string altChunkId = "AltChunkId" + id;
AlternativeFormatImportPart chunk =
mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Xhtml, altChunkId);
using (MemoryStream ms = new MemoryStream(System.Text.Encoding.Default.GetBytes(rtfValue)))
{
chunk.FeedData(ms);
ms.Close();
}
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
InfoPathToWord.ReplaceContentControl(sdtList, altChunk);
}
}
And finally the altChunk replaces the "response1"
public static void ReplaceContentControl(
List<OpenXmlElement> sdtList, OpenXmlElement element)
{
if (sdtList.Count != 0)
{
foreach (OpenXmlElement sdt in sdtList)
{
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(element, sdt);
sdt.Remove();
}
}
}
The issue is that it replaces the text but the formatting is not correct and shows "?" character in the Output text.
Not sure if its being caused because of encoding, I've also tried System.Text.Encoding.UTF8.GetBytes(rtfValue), System.Text.Encoding.ASCII.GetBytes(rtfValue)
but none of this seems to help.
Please could someone tell me what I'm doing wrong.
Thanks in advance.
Mave