In This Topic
    Get the attachments
    In This Topic

    Introduction

    The following example demonstrates how get the files attached to a PDF document & then display some of their properties (name of the files, last time they were modified, etc.).

    Get the attachments of a PDF document (C#)
    Copy Code
    public static void GetAttachments()
    {
      Console.WriteLine( "=== GET ATTACHMENTS ===" );
    
      // Loads a document.
      var loadedDocument = "DocWithAttachments.pdf";
      using( var pdfInput = PdfDocument.Load( AttachmentsSample.AttachmentsSampleResourcesDirectory + loadedDocument ) )
      {
        var outputFileName = "GetAttachments.pdf";
        var outputPath = AttachmentsSample.AttachmentsSampleOutputDirectory + outputFileName;
    
        // Creates a PdfDocument.
        using( var pdfOutput = PdfDocument.Create( outputPath ) )
        {
          // Gets the first Page of the document.
          var page = pdfOutput.Pages.First();
    
          // Adds a title.
          var titleStyle = TextStyle.WithFont( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
          page.AddParagraph( "Get attachments", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
          // Adds text.
          var textStyle = TextStyle.WithFontAndColor( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 12, Brushes.Red );
          page.AddText( $"Getting attachments from document: {loadedDocument}", new Point( 100, 100 ), textStyle );
    
          // Gets the attachments of the loaded document.
          var attachments = pdfInput.Attachments;
    
          // For each attachment, displays some of the attachment's properties.
          for( int i = 0; i < attachments.Count; ++i )
          {
            var attachment = attachments[ i ];
    
            page.AddText( "Attachment " + ( i + 1 ) + ":", new Point( 30, 150 + ( i * 65 ) ) );
            page.AddText( $"  Name: {attachment.Name}", new Point( 30, 165 + ( i * 65 ) ) );
            page.AddText( $"  Description: {attachment.Description}", new Point( 30, 180 + ( i * 65 ) ) );
            page.AddText( $"  Modified date: {attachment.ModifiedDate}", new Point( 30, 195 + ( i * 65 ) ) );
          }
    
          // Saves the output document.
          pdfOutput.Save();
          Console.WriteLine( $"Info exported to path: {outputFileName}" );
        }
      }
    }
    

    See Also

    Add elements in a PDF document