I’m desperatly trying to insert the HTML content of a text editor received through the API in a docx template.
After going through the method, the altChunk is correctly inserted but the content is empty.
I’ve tried to inserted the text directly and it works fine, but it’s not working with html.
<code> public ResponseJson<DocGenerique> PostGenerique(WordContent content)
{
ResponseJson<DocGenerique> response = new ResponseJson<DocGenerique>();
//response.Data = new DocGenerique();
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string subDirectory = "App_LocalResources";
string originalFileName = "DocName.docx";
string modifiedFileName = "Copy_DocName.docx";
var originalFilePath = Path.Combine(basePath, subDirectory, originalFileName);
var modifiedFilePath = Path.Combine(basePath, subDirectory, modifiedFileName);
try
{
File.Copy(originalFilePath, modifiedFilePath, true);
using (MemoryStream modifiedDocumentStream = new MemoryStream())
{
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(modifiedFilePath, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
var textElements = mainPart.Document.Descendants<Text>();
foreach (var textElement in textElements)
{
if (textElement.Text.Contains("#texte"))
{
content.TextContent[0] = "<html><head><meta charset="utf-8"></meta></head><body><p>texte<br></p></body></html>";
string sChunkID = "ChunkId";
AlternativeFormatImportPart oChunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, sChunkID);
using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(serviceDescriptionHTML)))
{
oChunk.FeedData(memoryStream);
}
AltChunk oAltChunk = new AltChunk();
oAltChunk.Id = sChunkID;
textElement.InsertAfterSelf(oAltChunk);
mainPart.Document.Save();
}
foreach (var element in content.Elements)
{
if (textElement.Text.Contains(element.Key))
{
textElement.Text = textElement.Text.Replace(element.Key, element.Value);
}
}
}
var test2 = doc.MainDocumentPart.Document.Body;
doc.Dispose();
}
modifiedDocumentStream.Position = 0;
using (FileStream fileStream = File.OpenRead(modifiedFilePath))
{
fileStream.CopyTo(modifiedDocumentStream);
}
byte[] byteArray = modifiedDocumentStream.ToArray();
var fileName = "CRI_" + content.Info.IdIntervention + "_" + content.Info.IdDpCri;
response.Data = new DocGeneriqueDAO().PostDocGenerique(content.Info.IdDpCri, content.Info.Matricule, content.Info.IdSection, content.Info.IdIntervention, ".docx", fileName, byteArray)[0];
response.AddMessage(1, "Success");
File.Delete(modifiedFilePath);
return response;
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
File.Delete(modifiedFilePath);
return response;
}
}
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
return response;
}
}
</code>
<code> public ResponseJson<DocGenerique> PostGenerique(WordContent content)
{
ResponseJson<DocGenerique> response = new ResponseJson<DocGenerique>();
//response.Data = new DocGenerique();
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string subDirectory = "App_LocalResources";
string originalFileName = "DocName.docx";
string modifiedFileName = "Copy_DocName.docx";
var originalFilePath = Path.Combine(basePath, subDirectory, originalFileName);
var modifiedFilePath = Path.Combine(basePath, subDirectory, modifiedFileName);
try
{
File.Copy(originalFilePath, modifiedFilePath, true);
using (MemoryStream modifiedDocumentStream = new MemoryStream())
{
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(modifiedFilePath, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
var textElements = mainPart.Document.Descendants<Text>();
foreach (var textElement in textElements)
{
if (textElement.Text.Contains("#texte"))
{
content.TextContent[0] = "<html><head><meta charset="utf-8"></meta></head><body><p>texte<br></p></body></html>";
string sChunkID = "ChunkId";
AlternativeFormatImportPart oChunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, sChunkID);
using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(serviceDescriptionHTML)))
{
oChunk.FeedData(memoryStream);
}
AltChunk oAltChunk = new AltChunk();
oAltChunk.Id = sChunkID;
textElement.InsertAfterSelf(oAltChunk);
mainPart.Document.Save();
}
foreach (var element in content.Elements)
{
if (textElement.Text.Contains(element.Key))
{
textElement.Text = textElement.Text.Replace(element.Key, element.Value);
}
}
}
var test2 = doc.MainDocumentPart.Document.Body;
doc.Dispose();
}
modifiedDocumentStream.Position = 0;
using (FileStream fileStream = File.OpenRead(modifiedFilePath))
{
fileStream.CopyTo(modifiedDocumentStream);
}
byte[] byteArray = modifiedDocumentStream.ToArray();
var fileName = "CRI_" + content.Info.IdIntervention + "_" + content.Info.IdDpCri;
response.Data = new DocGeneriqueDAO().PostDocGenerique(content.Info.IdDpCri, content.Info.Matricule, content.Info.IdSection, content.Info.IdIntervention, ".docx", fileName, byteArray)[0];
response.AddMessage(1, "Success");
File.Delete(modifiedFilePath);
return response;
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
File.Delete(modifiedFilePath);
return response;
}
}
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
return response;
}
}
</code>
public ResponseJson<DocGenerique> PostGenerique(WordContent content)
{
ResponseJson<DocGenerique> response = new ResponseJson<DocGenerique>();
//response.Data = new DocGenerique();
string basePath = AppDomain.CurrentDomain.BaseDirectory;
string subDirectory = "App_LocalResources";
string originalFileName = "DocName.docx";
string modifiedFileName = "Copy_DocName.docx";
var originalFilePath = Path.Combine(basePath, subDirectory, originalFileName);
var modifiedFilePath = Path.Combine(basePath, subDirectory, modifiedFileName);
try
{
File.Copy(originalFilePath, modifiedFilePath, true);
using (MemoryStream modifiedDocumentStream = new MemoryStream())
{
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(modifiedFilePath, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
var textElements = mainPart.Document.Descendants<Text>();
foreach (var textElement in textElements)
{
if (textElement.Text.Contains("#texte"))
{
content.TextContent[0] = "<html><head><meta charset="utf-8"></meta></head><body><p>texte<br></p></body></html>";
string sChunkID = "ChunkId";
AlternativeFormatImportPart oChunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, sChunkID);
using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(serviceDescriptionHTML)))
{
oChunk.FeedData(memoryStream);
}
AltChunk oAltChunk = new AltChunk();
oAltChunk.Id = sChunkID;
textElement.InsertAfterSelf(oAltChunk);
mainPart.Document.Save();
}
foreach (var element in content.Elements)
{
if (textElement.Text.Contains(element.Key))
{
textElement.Text = textElement.Text.Replace(element.Key, element.Value);
}
}
}
var test2 = doc.MainDocumentPart.Document.Body;
doc.Dispose();
}
modifiedDocumentStream.Position = 0;
using (FileStream fileStream = File.OpenRead(modifiedFilePath))
{
fileStream.CopyTo(modifiedDocumentStream);
}
byte[] byteArray = modifiedDocumentStream.ToArray();
var fileName = "CRI_" + content.Info.IdIntervention + "_" + content.Info.IdDpCri;
response.Data = new DocGeneriqueDAO().PostDocGenerique(content.Info.IdDpCri, content.Info.Matricule, content.Info.IdSection, content.Info.IdIntervention, ".docx", fileName, byteArray)[0];
response.AddMessage(1, "Success");
File.Delete(modifiedFilePath);
return response;
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
File.Delete(modifiedFilePath);
return response;
}
}
}
catch (Exception ex)
{
response.AddErreur(400, ex.Message);
return response;
}
}
}
Thanks for your help!