Issue merging rich text content into word document using OPEN XML-AltChuck

  • Thread starter Thread starter Noorul L
  • Start date Start date
N

Noorul L

Guest
Hello all - During a merge process in a docx with rich text content, below approach is used.

  • Place holders are configured in docx as "field code" as comments .
  • Get the rich text html content from the application.
  • Create a new Open XML altchunk element and embed the html content.
  • Append the updated altchunk into the respective placeholders in the word doc.
  • Issue is the thought the place holder is in the middle of the sentence or paragraph, the value is getting appended in the end of the statement. Red boxed text in the below example.Any suggestion/alternates ?

eg: This is the {test1} content

expected result after merge : This is the merged content

Actual result : This is the content merged .(appends the end)

<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f">
</v:shapetype>


if (!string.IsNullOrEmpty(dynamicField?.HtmlContent) && !dynamicField.HtmlContent.Equals("undefined"))
{
var htmlContentToBeReplaced = "<html><head><style type='text/css'> .dynamicfreetext {font-family: Times New Roman !important;font-size: 12pt !important;} </style>" +
"</head><body><pre><div class = 'dynamicfreetext'>" + dynamicField.HtmlContent + "</div></pre></body></html>";

var doc = new HtmlDocument();
doc.LoadHtml(htmlContentToBeReplaced);

var newListItemDetails = new List<Tuple<string, string>>();
var nodesList = doc.DocumentNode.Descendants();

}

if (newListItemDetails.Any())
{
foreach (var newListItem in newListItemDetails)
{
htmlContentToBeReplaced = htmlContentToBeReplaced.Replace(newListItem.Item1, newListItem.Item2);
}
}
}

var parentNode = p?.Parent;

var altChunkId = "richTextId" + Guid.NewGuid().ToString();
var ms = new MemoryStream(System.Text.Encoding.Unicode.GetPreamble().Concat(System.Text.Encoding.Unicode.GetBytes(htmlContentToBeReplaced)).ToArray());

// Create alternative format import part.
var formatImportPart = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId);

// Feed HTML data into format import part (chunk).
formatImportPart.FeedData(ms);
var altChunk = new AltChunk { Id = altChunkId };
altChunk.AltChunkProperties = new AltChunkProperties { MatchSource = new MatchSource { Val = true } };
CreateRunForDynamicRichTextArea(ref parentNode, ref runList, altChunk);
}

private static void CreateRunForDynamicRichTextArea(ref OpenXmlElement parentNode, ref List<Run> runList, AltChunk altChunk)
{
var txtNodes = (from run in runList
from text in run.Descendants<Text>()
select text);

if (txtNodes != null)
{
var txtContents = txtNodes.ToList();
foreach (var txtContent in txtContents)
{
txtContent.Text = string.Empty;
}
}

var runWithHtmlContent = new Run();
runWithHtmlContent.AppendChild(altChunk);
parentNode.AppendChild(runWithHtmlContent);
}

<v:shapetype coordsize="21600,21600" filled="f" id="_x0000_t75" o:preferrelative="t" o:spt="75" path="m@4@5l@4@11@9@11@9@5xe" stroked="f"> <v:stroke joinstyle="miter">
<v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0">
<v:f eqn="sum @0 1 0">
<v:f eqn="sum 0 0 @1">
<v:f eqn="prod @2 1 2">
<v:f eqn="prod @3 21600 pixelWidth">
<v:f eqn="prod @3 21600 pixelHeight">
<v:f eqn="sum @0 0 1">
<v:f eqn="prod @6 1 2">
<v:f eqn="prod @7 21600 pixelWidth">
<v:f eqn="sum @8 21600 0">
<v:f eqn="prod @7 21600 pixelHeight">
<v:f eqn="sum @10 21600 0">
</v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:f></v:formulas>
<v:path gradientshapeok="t" o:connecttype="rect" o:extrusionok="f">
<o:lock aspectratio="t" v:ext="edit">
</o:lock></v:path></v:stroke></v:shapetype> <v:shape alt="" id="Picture_x0020_1" o:spid="_x0000_i1025" style="width:358.5pt;height:242.25pt;" type="#_x0000_t75">
<v:imagedata o:href="cid:image005.jpg@01D5C482.D2DA1D80" src="file:///C:/Users/mxf2030/AppData/Local/Temp/msohtmlclip1/01/clip_image001.jpg">
</v:imagedata></v:shape>


Continue reading...
 
Back
Top