Docotic.Pdf Library Help > Samples > Attachments > List attachments and file annotations names

This sample shows how to list names of all files attached to a PdfDocument.

Attached files can be added to collection of document-level attachments or used in file attachment annotations.

Document level attachments are accessible through PdfDocument.SharedAttachments property.

In order to list names of all files used in file attachment annotations you should loop through all document pages. File attachment annotations are accessible through PdfPage.Widgets property and have type equal to PdfFileAttachmentAnnotation.

Note that the same attachment can be used in a file attachment annotation and added to collection of document-level annotations. You can find unique attachments by comparing their file specifications. File specifications can be compared using object.Equals method or simple "==" operator.

C#
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class GetAllAttachments
    {
        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.

            using (PdfDocument pdf = new PdfDocument(@"Sample Data\Attachments.pdf"))
            {
                StringBuilder attachmentNames = new StringBuilder(); 

                // collect names of files attached to the document
                foreach (PdfFileSpecification spec in pdf.SharedAttachments)
                    attachmentNames.AppendLine(spec.Specification);

                // collect names of files used in file attachment annotations
                foreach (PdfPage page in pdf.Pages)
                {
                    foreach (PdfWidget widget in page.Widgets)
                    {
                        if (widget.Type != PdfWidgetType.FileAttachment)
                            continue;

                        PdfFileAttachmentAnnotation fileAnnot = (PdfFileAttachmentAnnotation)widget;
                        attachmentNames.AppendLine(fileAnnot.File.Specification);
                    }
                }

                MessageBox.Show(attachmentNames.ToString(), "Attachments List");
            }
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Text
Imports System.Windows.Forms

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class GetAllAttachments
        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.

            Using pdf As New PdfDocument("Sample Data\Attachments.pdf")
                Dim attachmentNames As New StringBuilder()

                ' collect names of files attached to the document
                For Each spec As PdfFileSpecification In pdf.SharedAttachments
                    attachmentNames.AppendLine(spec.Specification)
                Next

                ' collect names of files used in file attachment annotations
                For Each page As PdfPage In pdf.Pages
                    For Each widget As PdfWidget In page.Widgets
                        If widget.Type <> PdfWidgetType.FileAttachment Then
                            Continue For
                        End If

                        Dim fileAnnot As PdfFileAttachmentAnnotation = DirectCast(widget, PdfFileAttachmentAnnotation)
                        attachmentNames.AppendLine(fileAnnot.File.Specification)
                    Next
                Next

                MessageBox.Show(attachmentNames.ToString(), "Attachments List")
            End Using
        End Sub
    End Class
End Namespace