Documents for PDF .NET Edition
Features / Attachment
In This Topic
    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 GcPdf. 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 GcPdf. 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 1.7 (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. GcPdf 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.
      Copy Code
      GcPdfDocument doc = new GcPdfDocument();
          Page page = doc.NewPage();
          string[] files = new string[]
      StringBuilder sb = new StringBuilder();
          foreach (var fn in files)
      //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
    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. GcPdf 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.
      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 = "GrapeCity 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)),
            // Done:
    Back to Top

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