I have a WebAPI in C#, and I need to load a Document by POST, modify some parameters, and save it to an Azure Blob account.
I can read the file, I can modify the parameters, but when I save it to Azure, the document is saved as the original file.
/* file es del tipo System.Net.Http.StreamContent que viene en un POST */
byte[] byteArray = await file.ReadAsByteArrayAsync();
MemoryStream docStream = new MemoryStream();
docStream.Write(byteArray, 0, byteArray.Length);
docStream.Seek(0x00000000, SeekOrigin.Begin);
WordprocessingDocument doc = WordprocessingDocument.Open(docStream, true);
MainDocumentPart mainDocumentPart = doc.MainDocumentPart;
var descedants = mainDocumentPart.Document.Body.Descendants<SdtElement>();
var idRef = descedants.Where(c => c.FirstChild.Elements<SdtAlias>().Where(v => v.Val == "Asunto").Count() > 0).FirstOrDefault();
if (idRef != null && user != null)
{
var tNomRef = idRef.Elements<SdtContentRun>().FirstOrDefault().FirstOrDefault();
var textNomRef = (DocumentFormat.OpenXml.OpenXmlLeafTextElement)tNomRef.LastOrDefault();
textNomRef.Text = info.idDocumento;
}
var titRef = descedants.Where(c => c.FirstChild.Elements<SdtAlias>().Where(v => v.Val == "Título").Count() > 0).FirstOrDefault();
if (titRef != null && user != null)
{
var tNomRef = idRef.Elements<SdtContentRun>().FirstOrDefault().FirstOrDefault();
var textNomRef = (DocumentFormat.OpenXml.OpenXmlLeafTextElement)tNomRef.LastOrDefault();
textNomRef.Text = info.nomDocumento;
}
/* Este objeto se encarga de subir el archivo a un Blob Storage de Azure */
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(xNombre);
docStream.Position = 0;
blob.UploadFromStream(docStream);
docStream.Close();
docStream.Dispose();
After the doc(WordprocessingDocument) has been modified, you need to save the changes back to docStream(MemoryStream) before uploading the docStream to blob storage. What you need do is just adding doc.Save() before uploading the docStream. Code below is for your reference.
doc.Save();
CloudBlockBlob blob = imagesContainer.GetBlockBlobReference(xNombre);
docStream.Position = 0;