Aspose.Pdf makes it possible for its users to attach any file to the PDF document. Aspose.Pdf specially provides Attachment class to serve this purpose. The attachment type can be specified with the help of an enumeration, AttachmentType . AttachmentType enumeration supports many pre-defined types of attachments as follows:
Attachment Type |
Description |
File |
This value shows that a file is going to be used as attachment |
None |
It shows that no attachment is being used |
Note |
It means that attachment is a note |
To attach a file, we simply set the AttachmentType property of Attachment class to AttachmentType.File enumeration value. Then we specify the full name of the file using AttachedFileName property of Attachment class. After finishing with this, we also set the type of file to AttachedFileType property.
We know that when a file is attached to a PDF document, we can make this attachment link look like an icon in the PDF document. Aspose.Pdf also provides some built-in icons that can be applied by using FileIconType enumeration. FileIconType enumeration has many icon shapes as listed below in the diagram:
|
Figure: FileIconType enumeration values and shapes |
Aspose.Pdf also allows to customize the icon color that can be achieved by setting the IconColor property of Attachment class to any desired Color instance.
Note: A file attachment is displayed in the PDF document as an icon. Double clicking on the icon, opens the file attachment with appropriate software. Using FileIconType enumeration, we can manage that which icon is to display in the PDF document to represent a file attachment.
Example:
[C#]
//Instantiate attachment instance by calling its empty constructor
Attachment fileAttachment = new Attachment();
//Add attachment in the paragraphs collection of the section
sec1.Paragraphs.Add(fileAttachment);
//Set attachment type to File using AttachmentType enumeration
fileAttachment.AttachmentType = AttachmentType.File;
//Set the path of the attachment file
fileAttachment.AttachedFileName = "c:/images/ccitt.tif";
//Set the type of the file to be attached
fileAttachment.AttachedFileType = "tif";
//Set the file icon type to Graph
fileAttachment.FileIconType = FileIconType.Graph;
//Set the color of the icon to Brown
fileAttachment.IconColor = new Aspose.Pdf.Color("Brown");
[VB.NET]
'Instantiate attachment instance by calling its empty constructor
Dim fileAttachment As Attachment = New Attachment()
'Add attachment in the paragraphs collection of the section
sec1.Paragraphs.Add(fileAttachment)
'Set attachment type to File using AttachmentType enumeration
fileAttachment.AttachmentType = AttachmentType.File
'Set the path of the attachment file
fileAttachment.AttachedFileName = "c:/images/ccitt.tif"
'Set the type of the file to be attached
fileAttachment.AttachedFileType = "tif"
'Set the file icon type to Graph
fileAttachment.FileIconType = FileIconType.Graph
'Set the color of the icon to Brown
fileAttachment.IconColor = New Aspose.Pdf.Color("Brown")
[XML]
<Attachment AttachmentType="File" AttachedFileName="c:/images/ccitt.tif"
AttachedFileType="tif" FileIconType="Graph" IconColor="Brown">
</Attachment>