PdfFileAttachment Class
Represents an attachment where a file can be embedded to a PDF document.
Namespace: DevExpress.Pdf
Assembly: DevExpress.Pdf.v18.2.Core.dll
Declaration
Related API Members
The following members return PdfFileAttachment objects:
Library | Related API Members |
---|---|
WinForms Controls | PdfFileAttachmentOpeningEventArgs.FileAttachment |
WPF Controls | AttachmentOpeningEventArgs.Attachment |
Remarks
You can specify the file attachment creation date (the PdfFileAttachment.CreationDate property), modification date (the PdfFileAttachment.ModificationDate property), content data of an attached file (the PdfFileAttachment.Data property), the attached file name (the PdfFileAttachment.FileName property), description (the PdfFileAttachment.Description property) and other settings.
Example
This example shows how to programmatically attach a file to the PDF document.
To do this:
- Create a
PdfFileAttachment
object; - Specify the attachment creation date, description, and file name using PdfFileAttachment.CreationDate, PdfFileAttachment.Description, and PdfFileAttachment.FileName properties. To specify the data for the attachment, use the PdfFileAttachment.Data property;
- If required, you can specify additional properties of an attached file, for example, the file’s mime type and relationship using PdfFileAttachment.MimeType and PdfFileAttachment.Relationship properties, respectively.
- Call the PdfDocumentProcessor.AttachFile method with a file attachment object used as a parameter.
- Save the attachment to a document by calling the PdfDocumentProcessor.SaveDocument method.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-attach-a-file-to-the-document-t304234
using DevExpress.Pdf;
using System;
using System.IO;
namespace AttachFile {
class Program {
static void Main(string[] args) {
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load a document.
processor.LoadDocument("..\\..\\Document.pdf");
// Attach a file to the PDF document.
processor.AttachFile(new PdfFileAttachment() {
CreationDate = DateTime.Now,
Description = "This is my attach file.",
FileName = "MyAttach.txt",
Data = File.ReadAllBytes("..\\..\\FileToAttach.txt")
});
// The attached document.
processor.SaveDocument("..\\..\\Result.pdf");
}
}
}
}