Document Solutions for PDF
Features / Attachment
In This Topic
    Attachment
    In This Topic

    Attachments contain reference to documents or files which are embedded in a PDF document. The content of these external files can be referred by a PDF using file specification which is represented by FileSpecification class in DsPdf. The file specification refers to an embedded file within the referring PDF file which allows the file contents to be stored or transmitted along with the PDF document. When a Pdf file containing file specification that refers to a external file is transmitted, it needs to be ensured that the references remain valid. This can be handled by the embedded file streams which are represented by the EmbeddedFileStream class in DsPdf. The embedded file stream allows the content of the referenced files to be embedded directly within the PDF file. For more information on file specification and embedded file streams, see PDF specification 2.0 (Section 7.11.1 and 7.11.4).

    Document Attachment

    An attachment which is attached to a PDF document at the document level is a document attachment. DsPdf allows you to embed the files in a PDF document and refer to them through file specifications. These files are attached to the PDF document using the Add method.

    PDF attachments

    To attach files to a PDF document at document level:

    1. Create a variable of type string to store the path of the files to be attached.
    2. Create an object of FileSpecification class to refer to the embedded file.
    3. Add the attachments to the document using the Add method.
      C#
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
          Page page = doc.NewPage();
      
          string[] files = new string[]
          {
          "road.jpg",
          "sea.jpg"
          };
                  
      StringBuilder sb = new StringBuilder();
          foreach (var fn in files)
              sb.AppendLine(fn);
      
      //Add string related to the attachment names
      page.Graphics.DrawString(sb.ToString() , new TextFormat(), new PointF(10, 50));
                  
      //Add attachments
          foreach (string fn in files)
          {
              string file = Path.Combine("Resources", fn);
              FileSpecification fspec = FileSpecification.FromEmbeddedFile(
                                        EmbeddedFileStream.FromFile(doc, file));
              doc.EmbeddedFiles.Add(file, fspec);
          }
          //Save the document
          doc.Save("DocAttachment.pdf");
      
    Back to Top

    File Attachment

    File attachment in a PDF document is attached on a page and is displayed as a link that jumps to the attached file on clicking the drawn graphics. DsPdf allows you to attach files to a PDF using the FileAttachmentAnnotation class. This class also allows you to set the icon to display the attachment using Icon property which accepts value from the FileAttachmentAnnotationIcon enum.

    File Attachments

    To add an attachment to a PDF document on a page:

    1. Create an object of GcPdfDocument and FileAttachmentAnnotation class.
    2. Set the required properties of FileAttachmentAnnotation object.
    3. Call the Add method to add the file attachment.
      C#
      Copy Code
      public void CreatePDF(Stream stream)
      {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
      
            var rc = Common.Util.AddNote("We have attached a single file from" + 
                "Resources/Images folder in this PDF document.", page);
            var ip = new PointF(rc.X, rc.Bottom + 9);
            var attSize = new SizeF(36, 18);    
                  
            string file = Path.Combine("Resources", "Images", "sea.jpg");
            FileAttachmentAnnotation faa = new FileAttachmentAnnotation()
            {
                 Color = Color.Gold,
                 UserName = "Admin",
                 Rect = new RectangleF(ip.X, ip.Y, attSize.Width, attSize.Height),
                 Contents = $"Attached file {file}",
                 Icon = FileAttachmentAnnotationIcon.Paperclip,
                 File = FileSpecification.FromEmbeddedFile(EmbeddedFileStream.FromFile(doc, file)),
            };
            page.Annotations.Add(faa);
      
            // Done:
            doc.Save(stream);
      }
      
    Back to Top

    For more information about how to work with file attachments using DsPdf, see DsPdf sample browser.