Xceed PDF Library for .NET Documentation v1.0
In This Topic
    Add attachments
    In This Topic

    Introduction

    The following example demonstrates how to attach files to a PDF document. It covers how to do so using a path or a stream.


    Add attachments to a PDF document (C#)
    Copy Code
    public static void AddAttachments()
    {
      Console.WriteLine( "=== ADD ATTACHMENTS ===" );
      var outputFileName = "AddAttachments.pdf";
      var outputPath = AttachmentsSample.AttachmentsSampleOutputDirectory + outputFileName;
    
      // Create a pdf document.
      using( var pdfOutput = PdfDocument.Create( outputPath ) )
      {
        // Get the first page of the document.
        var page = pdfOutput.Pages.First();
    
        // Add title.
        var textStyle = TextStyle.WithFont( pdfOutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d );
        page.AddParagraph( "Add attachments", textStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) );
    
        // Add text.
        page.AddText( "This document contains 2 attachments.", new Point( 100, 100 ) );
    
        // Create an attachment from a path.
        var attachment = new Attachment( AttachmentsSample.AttachmentsSampleResourcesDirectory + "BasicTextInfo.pdf" );
        attachment.Description = "The Info";
        pdfOutput.Attachments.Add( attachment );
    
        // Create an attachment from a stream.
        var imageBytes = File.ReadAllBytes( AttachmentsSample.AttachmentsSampleResourcesDirectory + "Image_1.jpg" );
        var attachment2 = new Attachment( "myImage", new MemoryStream( imageBytes ) );
        attachment2.Description = "The Image";
        pdfOutput.Attachments.Add( attachment2 );
    
        // Save the output document.
        pdfOutput.Save();
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    }

    See Also