FileAttachments.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Pdf.Actions
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' This sample demonstrates how to create file attachment annotations on a page.
'' See also the DocAttachments sample that demonstrates document level file attachments.
Public Class FileAttachments
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

        Dim rc = Util.AddNote(
            "Some files from the sample's Resources/Images folder are attached to this page." + vbLf +
            "Some viewers may not show attachments, so we draw rectangles to indicate their (usually clickable) locations.",
            page)
        Dim ip = New PointF(rc.X, rc.Bottom + 9)
        Dim attSize = New SizeF(36, 12)
        Dim gap = 8
        Dim files As String() = {
            "tudor.jpg",
            "sea.jpg",
            "puffins.jpg",
            "lavender.jpg",
            "skye.jpg",
            "fiord.jpg",
            "newfoundland.jpg"
        }
        For Each fn In files
            Dim File = Path.Combine("Resources", "Images", fn)
            Dim faa = New FileAttachmentAnnotation() With {
                    .Color = Color.FromArgb(&HFFC540A5),
                    .UserName = "Jaime Smith",
                    .Rect = 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)
            g.FillRectangle(faa.Rect, Color.FromArgb(&HFF40C5A3))
            g.DrawRectangle(faa.Rect, Color.FromArgb(&HFF6040C5))
            ip.Y += attSize.Height + gap
        Next
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class