PDF File Attachment

This demo illustrates how to attach files to a document.
Click the button below to attach the DevExpress
logo to the sample document.
@model AspNetCoreDemos.OfficeFileAPI.PdfFileAttachmentModel
@using DevExtreme.AspNet.Mvc

@using (Html.BeginForm("PdfAttachFile", "ContentManipulation", FormMethod.Post)) {
    <div class="demo-simple-view">
        <div class="demo-simple-view-caption">
            This demo illustrates how to attach files to a document.
        </div>
        <div class="demo-simple-view-first-line">
            Click the button below to attach the DevExpress
        </div>
        <div class="demo-simple-view-line">
            logo to the sample document.
        </div>
        <div style="margin-bottom: 75px">
            @(Html.DevExtreme().Button()
                               .Text("Attach and Download")
                               .Type(ButtonType.Default)
                               .StylingMode(ButtonStylingMode.Contained)
                               .UseSubmitBehavior(true)
            )
        </div>
    </div>
}
using Microsoft.AspNetCore.Hosting;

namespace AspNetCoreDemos.OfficeFileAPI {
    public partial class ContentManipulationController : DocumentProcessingController {
        public ContentManipulationController(IWebHostEnvironment hostingEnvironment) : base(hostingEnvironment) {
        }
    }
}
using System.IO;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreDemos.OfficeFileAPI {
    public partial class ContentManipulationController {
        const string fileAttachmentDefaultFile = "/Documents/Pdf/FileAttachment.pdf";
        const string fileAttachmentImageFile = "/Documents/DevExpress.png";

        public IActionResult PdfFileAttachment() {
            PdfFileAttachmentModel model = new PdfFileAttachmentModel();
            return View(model);
        }

        public IActionResult PdfAttachFile() {
            using(PdfFileAttachmentModel model = new PdfFileAttachmentModel()) {
                string filePath = HostingEnvironment.ContentRootPath + fileAttachmentDefaultFile;
                string attachmentPath = HostingEnvironment.ContentRootPath + fileAttachmentImageFile;
                Stream stream = model.CreateDocument(filePath, attachmentPath);
                return CreateFileStreamResult(stream, "application/pdf", "pdf", "Result");
            }
        }
    }
}
using System;
using System.IO;
using DevExpress.Pdf;

namespace AspNetCoreDemos.OfficeFileAPI {
    public class PdfFileAttachmentModel : PdfModelBase {

        public Stream CreateDocument(string filePath, string attachPath) {
            Processor.LoadDocument(filePath);

            PdfFileAttachment attachment = new PdfFileAttachment();
            attachment.Data = File.ReadAllBytes(attachPath);
            attachment.FileName = "DevExpress.png";
            attachment.Description = "To open the attachment in the Attachments tab, you can either click the 'Open file in its native application' icon or double - click the attachment.";
            attachment.CreationDate = DateTime.Now;
            Processor.AttachFile(attachment);

            PdfDocument document = Processor.Document;
            document.Creator = "PDF Document Processor Demo";
            document.Author = "DevExpress Inc.";
            document.Keywords = "PDF, document, server, code, generation, file atatchment";
            document.Subject = "PDF Document Processor - File Attachment";
            document.Title = "PDF File Attachment";
            document.Producer = "Developer Express Inc., " + AssemblyInfo.Version;

            Stream stream = new MemoryStream();
            Processor.SaveDocument(stream);
            return stream;
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using DevExpress.Pdf;

namespace AspNetCoreDemos.OfficeFileAPI {
    public class PdfModelBase : IDisposable {
        readonly PdfDocumentProcessor processor;
        MemoryStream stream;
        List<PdfPageModel> items = new List<PdfPageModel>();

        public MemoryStream Stream { get { return stream; } }
        protected PdfDocumentProcessor Processor { get { return processor; } }
        protected PdfDocument Document { get { return processor.Document; } }
        public List<PdfPageModel> Items { get { return items; } }

        public string DocumentUrl { get; set; }
        public int PageIndex { get; set; }
        internal virtual string SessionKey { get; }
        public PdfPreviewModel PreviewModel { get; internal set; }

        public PdfModelBase() {
            processor = new PdfDocumentProcessor();
            PreviewModel = new PdfPreviewModel();
        }

        protected void CreateEmptyDocument() {
            stream = new MemoryStream();
            Processor.CreateEmptyDocument(stream);
            Document.Creator = "PDF Document Processor Demo";
            Document.Producer = "Developer Express Inc., " + AssemblyInfo.Version;
            Document.Author = "DevExpress Inc.";
        }

        public virtual void LoadDocument(byte[] data) {
            using(MemoryStream stream = new MemoryStream(data))
                LoadDocument(stream);
        }

        protected void LoadDocument(Stream stream) {
            processor.LoadDocument(stream, true);
            for(int pageNumber = 1; pageNumber <= processor.Document.Pages.Count; pageNumber++)
                Items.Add(new PdfPageModel(processor, pageNumber));
        }

        public void Dispose() {
            if(processor != null)
                processor.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}