Document Solutions for PDF
Features / Annotations / File Attachment Annotation
In This Topic
    File Attachment Annotation
    In This Topic

    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.

    File attachment annotation in a PDF file

    FileAttachmentAnnotation class provides the following properties to set various options for the file attachment annotation:

    Property Description
    UserName Adds the user name to the text label in the title bar of the annotation’s pop-up window when the annotation is open and active.
    Subject Adds the text representing the subject of the annotation.
    Contents Adds the text to the annotation for display.
    RichText Adds the text to the annotation for display in the pop-up window when opened. You can format this text using HTML tags.
    Opacity Sets the opacity of the annotation.
    Color Sets the annotation color, popup window color, line color, etc.
    File Adds the referenced file.
    Icon Sets the type of icon used to display the annotation.
    PdfRect Sets the rectangle that defines the location and size of the annotation on a page in PDF user space coordinates. The positive X axis extends horizontally to the right, and the positive Y axis extends vertically upward, with the origin usually in the lower left corner of the page.

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

    C#
    Copy Code
    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");
    }