Attachments

This module uses the DevExpress PDF Document API (PdfDocumentProcessor) to attach files to a PDF document. You can process the predefined sample file or supply your own document. To do the latter, select Upload a File in the file selection drop-down menu.

Use the Create Attachment Annotation option to add a visual annotation for the attachment.

Click Add Attachment and Save to attach the predefined file to the PDF and download the result.

Select a Document
Sample_Alternative.pdf
Default image



using DevExpress.Pdf;

Stream AttachFile(Stream documentStream, string fileName, string fileDescription, string fileMimeType, byte[] fileData, bool createAnnotation) {
    using var processor = new PdfDocumentProcessor();

    processor.LoadDocument(documentStream);

    var attachment = new PdfFileAttachment() {
        CreationDate = DateTime.UtcNow,
        FileName = fileName,
        Data = fileData,
        MimeType = fileMimeType,
        Description = fileDescription,
        ModificationDate = DateTime.UtcNow,
        Relationship = PdfAssociatedFileRelationship.Supplement
    };

    if(createAnnotation) {
        var page = processor.Document.Pages[0];
        var pageFacade = processor.DocumentFacade.Pages[0];
        pageFacade.AddFileAttachmentAnnotation(new PdfPoint(page.MediaBox.Right - 50, page.MediaBox.Bottom + 50), attachment, PdfFileAttachmentAnnotationIconName.PaperClip);
    } else
        processor.AttachFile(attachment);

    var outputStream = new MemoryStream();
    processor.SaveDocument(outputStream);

    return outputStream;
}