[]
        
(Showing Draft Content)

File Attachment Annotation

A file attachment annotation represents a reference to a file that is typically embedded in the PDF file. The file attachment annotation appears as a paper clip icon on the PDF file. Users can double-click the icon to open the embedded file. This gives users a chance to view or store the file in the system. DsPdf provides FileAttachmentAnnotation class to enable users to apply file attachment annotations to the PDF file.

DsPdf demonstrating how to add file attachment annotation in a PDF document

Refer to the following example code to add the file attachment annotation to a PDF document:

public void CreateFileAttachmentAnnotation()
{
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    RectangleF rc = new RectangleF(50, 50, 400, 80);
    g.DrawString("Some files from the sample's Resources/Images folder are attached to this page. Some viewers" +
        " may not show attachments, so we draw rectangles to indicate their(usually clickable) location",
        new TextFormat() { Font = StandardFonts.Times, FontSize = 11 }, rc);

    var ip = new PointF(rc.X, rc.Bottom + 9);
    var attSize = new SizeF(36, 12);
    var gap = 8;
    string[] files = new string[]
    {
        "tudor.jpg",
        "sea.jpg",
        "puffins.jpg",
        "lavender.jpg",
    };
    foreach (string fn in files)
    {
        string file = Path.Combine("Resources", "Images", fn);
        //Create an instance of FileAttachmentAnnotation class and set its relevant properties
        FileAttachmentAnnotation faa = new FileAttachmentAnnotation()
        {
            Color = Color.FromArgb(unchecked((int)0xFFc540a5)),
            UserName = "Jaime Smith",
            PdfRect = new RectangleF(ip.X, ip.Y, attSize.Width, attSize.Height),
            Contents = "Attached file: " + file,
            Icon = FileAttachmentAnnotationIcon.Paperclip,
            File = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, file)),
        };
        page.Annotations.Add(faa); //Add the file attachment annotation
        g.FillRectangle(faa.Rect, Color.FromArgb(unchecked((int)0xFF40c5a3)));
        g.DrawRectangle(faa.Rect, Color.FromArgb(unchecked((int)0xFF6040c5)));
        ip.Y += attSize.Height + gap;
    }

    doc.Save("FileAttachmentAnnotation.pdf");
}