PDF for UWP | ComponentOne
Using PDF for UWP / Attaching Files to a PDF Document
In This Topic
    Attaching Files to a PDF Document
    In This Topic

    Adding file attachments to PDF files is often a useful feature. Attachments can contain any kind of file, including spreadsheets with detailed information that would clutter the main document, multimedia files with movies and sound, sample code, and so on.

    Adding file attachments to your PDF for UWP documents is easy. All you have to do is call the C1PdfDocument.AddAttachment method and specify which file you want to attach, what area of the page should contain the attachment, and optionally, the appearance of the attachment.

    For example, the following code attaches all files in the application directory to the PDF document:

    Visual Basic
    Copy Code
    Dim rect As New Rect(100, 100, 60, 10)
    Dim font As New Font("Arial", 9)
    
    ' Attach some files.
    Dim path As String = "c:\temp\files"
    Dim file As String
    For Each file In Directory.GetFiles(path)
        Dim width As Single = rect.Width
        rect.Width = rect.Height
        _c1pdf.FillRectangle(Colors.Gray, rect)
        _c1pdf.AddAttachment(file, rect)
        rect.Width = width
        rect.X += rect.Height
        _c1pdf.DrawString(Path.GetFileName(file), font, Colors.Black, rect)
        rect.X -= rect.Height
        rect.Y += 2 * rect.Height
    Next file
    
    C#
    Copy Code
    Rect rect = new Rect(100, 100, 60, 10);
    Font font = new Font("Arial", 9);
    
    // Attach some files.
    string path = @"c:\temp\files";
    string[] files = Directory.GetFiles(path);
    foreach (string file in files)
    {
        float width = rect.Width;
        rect.Width = rect.Height;
        _c1pdf.FillRectangle(Colors.Gray, rect);
        _c1pdf.AddAttachment(file, rect);
        rect.Width = width;
        rect.X += rect.Height;
        _c1pdf.DrawString(Path.GetFileName(file), font, Colors.Black, rect);
        rect.X -= rect.Height;
        rect.Y += 2 * rect.Height;
    }
    

    Here's what the PDF document looks like in Adobe's Acrobat Reader:

     

    The attachments are displayed as icons (you can select from four predefined icons in the AttachmentIconEnum enumeration and you can also select the icon color). When the user moves the mouse over the attachment, the file name is displayed and the cursor changes to indicate there is an attachment available. The user can then right-click the attachment name to open the attachment or save it.