Docotic.Pdf Library Help > Samples > Attachments > Save (extract) attachment

This sample shows how to save attachment bytes (extract attached file).

Each attachment is represented by a PdfFileSpecification object. Use PdfFileSpecification.Contents property to retrieve reference to a PdfEmbeddedFile object.

If retrieved reference is null then the file specification refers to an external file (i.e. only reference to a file is attached).

If retrieved reference is non-null then the file is actually embedded in the document. Use can use PdfEmbeddedFile.Save method to extract contents of the attachment to a stream or a file.

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

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class SaveAttachment
    {
        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"))
            {
                PdfFileSpecification spec = pdf.SharedAttachments["File Attachment testing.doc"];
                if (spec != null && spec.Contents != null)
                {
                    const string pathToFile = "attachment.doc";
                    spec.Contents.Save(pathToFile);
                    Process.Start(pathToFile);
                }
                else
                {
                    MessageBox.Show("Can't save shared attachment", "Error");
                }
            }
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Windows.Forms

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class SaveAttachment
        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 spec As PdfFileSpecification = pdf.SharedAttachments("File Attachment testing.doc")
                If spec IsNot Nothing And spec.Contents IsNot Nothing Then
                    Const pathToFile As String = "attachment.doc"
                    spec.Contents.Save(pathToFile)
                    Process.Start(pathToFile)
                Else
                    MessageBox.Show("Can't save shared attachment", "Error")
                End If
            End Using
        End Sub
    End Class
End Namespace