Attach Files to a PDF with PDF Document API
- 4 minutes to read
You can attach a file to a PDF file in two ways:
- Embed a file into the document
- This file attachment does not appear on document pages. The file is stored among the PDF’s document-level attachments and is displayed in the Attachments panel in the PDF viewer application.
- Create an attachment annotation
- The file is attached to a specific location on a page as an annotation object. An annotation has a visual marker, usually an icon. Users can click this icon to open or download the attached file.
PDF Document API allows you to execute both scenarios. You can create, configure, and remove both embedded files and attachment annotations.
#Attach Files to PDF Documents
Create a PdfFileAttachment object and set the Data property to the attachment file contents. You can also specify the attachment’s parameters, such as creation date, description, file name, and so on.
Call the PdfDocumentProcessor.AttachFile method and pass the created PdfFileAttachment
instance as a parameter to attach a file to a document.
using DevExpress.Pdf;
using System;
using System.IO;
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 attached file.",
FileName = "MyAttach.txt",
Data = File.ReadAllBytes("..\\..\\..\\FileToAttach.txt"),
MimeType = "text/plain",
Relationship = PdfAssociatedFileRelationship.Supplement,
});
// The attached document.
processor.SaveDocument("..\\..\\Result.pdf");
}
#Obtain PDF File Attachments
Obtain the PdfDocument.FileAttachments property. You can retrieve an element based on the required criteria and inspect its parameters.
#Remove Attachments from the PDF File
To delete an attachment, call the PdfDocumentProcessor.DeleteAttachment method and pass the required PdfFileAttachment
object as a parameter.
The following code snippet removes all attachments that are larger than 1500 bytes:
using DevExpress.Pdf;
using System.Linq;
//...
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
// Load a document.
processor.LoadDocument("..\\..\\..\\Document.pdf");
var largeAttachments = processor.Document.FileAttachments.Where(x => x.Size > 1500).ToList();
foreach (PdfFileAttachment attachment in largeAttachments)
{
processor.DeleteAttachment(attachment);
}
processor.SaveDocument("..\\..\\..\\Result_removed.pdf");
}
#Create Attachment Annotations
The PdfPageFacade.Annotations property retrieves all page annotation properties. Use the PdfDocumentFacade.Pages property to obtain the PdfPageFacade instance.
Call the PdfPageFacade.AddFileAttachmentAnnotation method to create a file attachment annotation at the required page area. Use the PdfFileAttachment object to specify attachment data. Pass one of the PdfFileAttachmentAnnotationIconName fields to specify the annotation icon.
The code sample below creates a file attachment annotation:
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
// Load a document
processor.LoadDocument("..\\..\\Document.pdf");
// Access the first page properties
PdfPageFacade pageFacade = processor.DocumentFacade.Pages[0];
// Specify attachment data
PdfFileAttachment attachment = new PdfFileAttachment()
{
CreationDate = DateTime.Now,
Description = "This is my attached file.",
FileName = "MyAttach.txt",
Data = File.ReadAllBytes("..\\..\\FileToAttach.txt")
};
// Create a file attachment annotation
PdfFileAttachmentAnnotationFacade pdfFileAttachment =
pageFacade.AddFileAttachmentAnnotation
(new PdfPoint(700,100), attachment, PdfFileAttachmentAnnotationIconName.PaperClip);
pdfFileAttachment.Author = "Sabella Jaida";
pdfFileAttachment.Subject = "Attachment";
// Save the result
processor.SaveDocument("..\\..\\Result.pdf");
}
Refer to the following help topic for more information on annotations in PDF Document API: Annotations.