Beispiel #1
0
        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);
        }