Ejemplo n.º 1
0
        public static MemoryStream GenerateSvgDocument(SvgDocumentEditModel model)
        {
            string tag       = model.ElementName;
            string attribute = model.Attribute;

            XmlDocument doc = new XmlDocument();

            doc.Load(model.TemplateDocumentPath);

            var elementList = doc.GetElementsByTagName(tag);

            for (int i = 0; i < elementList.Count; i++)
            {
                string idValue = elementList[i].Attributes[attribute].Value;
                if (model.ElementsForSubstitution.ContainsKey(idValue))
                {
                    elementList[i].ChildNodes[0].InnerText = model.ElementsForSubstitution[idValue][model.ValueIndex];
                }
            }
            var memory = new MemoryStream();

            doc.Save(memory);
            memory.Flush();
            memory.Position = 0;
            return(memory);
        }
        public IActionResult GenerateDocument([Bind("DocId, InputText, OutputType")] SvgDocumentForm svgDocument)
        {
            Dictionary <string, string[]> jsonInput;
            int documentId = svgDocument.DocId;

            try
            {
                jsonInput = DataExtractor.GetJsonData(svgDocument.InputText);
            }
            catch (Exception e)
            {
                var error = e.Message;
                return(Content(error));
            }

            var document = _context.SvgDocuments.Find(documentId);

            string path = _hostingEnvironment.WebRootPath + document.DocumentPath;

            int            max         = jsonInput.First().Value.Length;
            int            index       = 0;
            List <ZipItem> outputFiles = new List <ZipItem>();


            foreach (var item in jsonInput)
            {
                int currentLength = item.Value.Length;
                if (currentLength != max)
                {
                    var error = "Value array error in column: " + item.Key + ". Expected length: " + max
                                + ", current length: " + currentLength;
                    return(Content(error));
                }
            }

            for (int i = 0; i < max; i++)
            {
                SvgDocumentEditModel model = new SvgDocumentEditModel()
                {
                    TemplateDocumentPath    = path,
                    ElementName             = "text",
                    Attribute               = "id",
                    ValueIndex              = index,
                    ElementsForSubstitution = jsonInput
                };
                index++;

                MemoryStream readySvgDocument = SvgEditor.GenerateSvgDocument(model);
                //MemoryStream jpegDocument = SvgEditor.SvgToJpeg(readySvgDocument);
                ZipItem zipItem = new ZipItem("Document" + index + ".svg", readySvgDocument);
                outputFiles.Add(zipItem);
                //jpegDocument.Close();
                //readySvgDocument.Close();
            }

            var resultZip = Zipper.Zip(outputFiles);

            return(File(resultZip, "application/octet-stream", "Documents.zip"));
        }