private static Stream GetStreamDocx(Invoice data) { var invoiceData = InvoiceFormattedData.GetData(data, 0, 0); var logo = new byte[] {}; if (!string.IsNullOrEmpty(invoiceData.LogoBase64)) { logo = Convert.FromBase64String(invoiceData.LogoBase64); } else if (invoiceData.LogoBase64Id != 0) { logo = Convert.FromBase64String(OrganisationLogoManager.GetOrganisationLogoBase64(invoiceData.LogoBase64Id)); } using (var zip = ZipFile.Read(TemplatePath)) { var documentXmlStream = new MemoryStream(); foreach (var entry in zip.Entries.Where(entry => entry.FileName == DocumentXml)) { entry.Extract(documentXmlStream); } documentXmlStream.Position = 0; zip.RemoveEntry(DocumentXml); var document = new XmlDocument(); document.Load(documentXmlStream); var documentStr = GenerateDocumentXml(document, invoiceData, logo); zip.AddEntry(DocumentXml, documentStr, Encoding.UTF8); if (logo.Length > 0) { zip.UpdateEntry(DocumentLogoImage, logo); } var streamZip = new MemoryStream(); zip.Save(streamZip); streamZip.Seek(0, SeekOrigin.Begin); streamZip.Flush(); return streamZip; } }
private Stream GetStreamDocx(Invoice data) { var invoiceData = _invoiceFormattedData.GetData(data, 0, 0); var logo = new byte[] { }; if (!string.IsNullOrEmpty(invoiceData.LogoBase64)) { logo = Convert.FromBase64String(invoiceData.LogoBase64); } else if (invoiceData.LogoBase64Id != 0) { logo = Convert.FromBase64String(_organisationLogoManager.GetOrganisationLogoBase64(invoiceData.LogoBase64Id)); } var result = new MemoryStream(); using (var zipOutputStream = new ZipOutputStream(Template)) using (var zipInputStream = new ZipInputStream(Template)) { ZipEntry zipEntry; while ((zipEntry = zipInputStream.GetNextEntry()) != null) { zipOutputStream.PutNextEntry(new ZipEntry(zipEntry.Name)); if (zipEntry.Name == DocumentXml) { var documentXmlStream = new MemoryStream(); zipInputStream.CopyTo(documentXmlStream); documentXmlStream.Position = 0; var document = new XmlDocument(); document.Load(documentXmlStream); var documentStr = GenerateDocumentXml(document, invoiceData, logo); var documentStrAsStream = new MemoryStream(Encoding.UTF8.GetBytes(documentStr)); documentStrAsStream.CopyTo(zipOutputStream); continue; } if (zipEntry.Name == DocumentLogoImage && logo.Length > 0) { var logoAsStream = new MemoryStream(logo); logoAsStream.CopyTo(zipOutputStream); continue; } zipInputStream.CopyTo(zipOutputStream); } zipOutputStream.Position = 0; zipOutputStream.CopyTo(result); } return(result); }