Ejemplo n.º 1
0
        /// <inheritdoc />
        public async Task <MemoryStream> Save()
        {
            MemoryStream stream = DocxFilePath.Create();

            stream = await Document.WriteInto(stream, "word/document.xml");

            stream = await Footnotes.WriteInto(stream, "word/footnotes.xml");

            stream = await ContentTypes.WriteInto(stream, ContentTypesInfo.Path);

            stream = await DocumentRelations.WriteInto(stream, DocumentRelsInfo.Path);

            stream = await FootnoteRelations.WriteInto(stream, "word/_rels/footnotes.xml.rels");

            stream = await Styles.WriteInto(stream, "word/styles.xml");

            stream = await Numbering.WriteInto(stream, "word/numbering.xml");

            stream = await Theme1.WriteInto(stream, "word/theme/theme1.xml");

            foreach (ChartInformation item in Charts)
            {
                stream = await item.Chart.WriteInto(stream, $"word/{item.Name}");
            }

            return(stream);
        }
Ejemplo n.º 2
0
        public IActionResult Index(
            [NotNull] [ItemNotNull] IEnumerable<IFormFile> files,
            [CanBeNull] string format,
            [CanBeNull] string title,
            [CanBeNull] string publisher,
            [CanBeNull] string website,
            [CanBeNull] string stylesheetUrl,
            [CanBeNull] IFormFile stylesheet)
        {
            if (files is null)
                throw new ArgumentNullException(nameof(files));

            IFormFile[] uploadedFiles = files.ToArray();

            if (uploadedFiles.Length == 0)
                return BadRequest("No files uploaded.");

            if (uploadedFiles.Any(x => x.Length <= 0))
                return BadRequest("Invalid file length.");

            if (uploadedFiles.Any(x => x.ContentType != MicrosoftWordDocument.ToString() &&
                                       x.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase) &&
                                       x.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase)))
                return BadRequest("Invalid file format.");

            if (stylesheet is IFormFile s && !s.FileName.EndsWith(".css"))
                return BadRequest($"Invalid stylesheet:{stylesheet.FileName}.");

            Queue<Package> packagesQueue = new Queue<Package>(uploadedFiles.Length);

            foreach (IFormFile file in uploadedFiles)
            {
                if (file.FileName.EndsWith(".docx", StringComparison.OrdinalIgnoreCase))
                {
                    packagesQueue.Enqueue(Package.Open(file.OpenReadStream()));
                }
                else if (file.FileName.EndsWith(".md", StringComparison.OrdinalIgnoreCase))
                {
                    MDocument mDocument = new MDocument();
                    using (StreamReader reader = new StreamReader(file.OpenReadStream()))
                    {
                        MarkdownVisitor visitor = new MarkdownVisitor();
                        ReadOnlySpan<char> span;
                        while ((span = reader.ReadLine()) != null)
                        {
                            mDocument.Append(visitor.Visit(in span));
                        }
                    }

                    Package package = DocxFilePath.Create().ToPackage(FileAccess.ReadWrite);

                    mDocument.ToOpenXml().WriteTo(package.GetPart(Document.PartUri));

                    packagesQueue.Enqueue(package);
                }
            }

            Package output =
                Process(
                    packagesQueue,
                    title ?? "[REPORT TITLE]",
                    publisher ?? "[PUBLISHER]",
                    website ?? "[PUBLISHER WEBSITE]");

            foreach (Package package in packagesQueue)
            {
                package.Close();
            }

            switch (format)
            {
                case "docx":
                {
                    return File(output.ToStream(), MicrosoftWordDocument, $"{title ?? "result"}.docx");
                }
                case "html":
                {
                    string styles = stylesheet is null ? null : new StreamReader(stylesheet.OpenReadStream()).ReadToEnd();
                    OpenXmlPackageVisitor ooxml = new OpenXmlPackageVisitor(output);
                    HtmlVisitor html = new HtmlVisitor(ooxml.Document.ChartReferences, ooxml.Document.ImageReferences);
                    XObject htmlResult = html.Visit(ooxml.Document.Content, ooxml.Footnotes.Content, title, stylesheetUrl, styles);
                    return File(Encoding.UTF8.GetBytes(htmlResult.ToString()), "text/html", $"{title ?? "result"}.html");
                }
                case "xml":
                {
                    OpenXmlPackageVisitor xml = new OpenXmlPackageVisitor(output);
                    XElement xmlResult = xml.Document.Content;
                    return File(Encoding.UTF8.GetBytes(xmlResult.ToString()), "application/xml", $"{title ?? "result"}.xml");
                }
                default:
                {
                    return BadRequest(ModelState);
                }
            }
        }
Ejemplo n.º 3
0
 /// <inheritdoc />
 /// <summary>
 /// Initializes an <see cref="T:AD.OpenXml.Visitors.OpenXmlVisitor" /> by reading document parts into memory from a default <see cref="MemoryStream"/>.
 /// </summary>
 public OpenXmlVisitor() : this(DocxFilePath.Create())
 {
 }