Docotic.Pdf Library Help > Samples > Attachments > Add attachments and file annotations to PDF

This samples shows how to attach a file or add a file annotation to your PDF document.

Use PdfDocument.CreateFileAttachment(..) method to create a new attachment.

You can add created attachment to collection of document-level attachments using PdfDocument.SharedAttachments.Add(..) method. Or you can add created attachment as a file attachment annotation using PdfPage.AddFileAnnotation(..) method.

C#
using System.Diagnostics;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class AddAttachments
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "AddAttachments.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                // add shared attachment to the document
                PdfFileSpecification ammerland = pdf.CreateFileAttachment(@"Sample Data\ammerland.jpg");
                pdf.SharedAttachments.Add(ammerland);

                // add file attachment annotation to the first page
                PdfRectangle bounds = new PdfRectangle(20, 70, 100, 100);
                PdfFileSpecification jpeg = pdf.CreateFileAttachment(@"Sample Data\jpeg.pdf");
                pdf.Pages[0].AddFileAnnotation(bounds, jpeg);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class AddAttachments
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "AddAttachments.pdf"

            Using pdf As New PdfDocument()
                ' add shared attachment to the document
                Dim ammerland As PdfFileSpecification = pdf.CreateFileAttachment("Sample Data\ammerland.jpg")
                pdf.SharedAttachments.Add(ammerland)

                ' add file attachment annotation to the first page
                Dim bounds As New PdfRectangle(20, 70, 100, 100)
                Dim jpeg As PdfFileSpecification = pdf.CreateFileAttachment("Sample Data\jpeg.pdf")
                pdf.Pages(0).AddFileAnnotation(bounds, jpeg)

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace