PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

How To Add and Extract PDF File Attachments

PDFtoolkit VCL 2.5 has introduced a new method that will help you extract attachments from a PDF document.
By Mohammed Najeemudheen and Shine Babu

On May 30, 2007, Gnostice released Version 2.50 of PDFtoolkit VCL. Among the new features is a new method that allows you to extract file attachments from PDF documents and save them to the file system. In this article, I will demonstrate how to make use of this feature. But first, I will demonstrate an old feature of PDFtoolkit that allowed users to add attachments to a PDF document.

Adding a File Attachment to a PDF Document

In this code snippet, two files (a text file and an BMP image file) are added to a PDF document as file attachments using the method InsertFileAttachment.

var
  FileAttach1, FileAttach2: TgtPDFFileAttachment;
begin
  // Creates PDF file attachment objects
  FileAttach1:= TgtPDFFileAttachment.Create;
  FileAttach2:= TgtPDFFileAttachment.Create;

  // Specifies pathname of file for the attachment
  FileAttach1.FileName:= 'TextAttachment.txt';
  // Specifies file name of attachment in the document
  FileAttach1.AttachmentName:= 'Text.txt';
  // Specifies where on a page to place attachment icon
  FileAttach1.SetBounds(555, 590, 530, 610);
  // Specifies icon used to identify the attachment
  FileAttach1.FileAttachmentIcon:= faPaperclip;
  
  FileAttach2.FileName:='ImageAttachment.bmp';
  FileAttach2.AttachmentName:= 'Image.bmp';
  FileAttach2.SetBounds(300, 311, 340, 331);
  FileAttach2.FileAttachmentIcon:= faPushPin;

  PDFDoc.LoadFromFile('Input.pdf');

  // Attaches files to page 1
  PDFDoc.InsertFileAttachment(FileAttach1, 1);
  PDFDoc.InsertFileAttachment(FileAttach2, 1);
  
  PDFDoc.ShowSetupDialog:= False;
  PDFDoc.SaveToFile('Output.pdf');

  FreeAndNil(FileAttach1);
  FreeAndNil(FileAttach2);
end;

Extracting a File Attachment

Extracting a file attachment involves the use of the new method SaveFileAttachmentTo and a new event PDFDocAttachmentNameChange event where you specify a file name for the saved attachment on the file system.

begin
 // Allows user to select a PDF file
 OpenDialog1.Execute;
 // Loads the selected PDF file
 PDFDoc.LoadFromFile(OpenDialog1.FileName);
 // Saves files attachments to a folder
 PDFDoc.SaveFileAttachmentTo('E:\output');
end;

procedure TForm1.PDFDocAttachmentNameChange(
   Sender: TgtCustomPDFDocument;
   const AFileName: String; 
   var ANewFileName: String);
begin
  // Specifies an alternate name for the saved attachment
  if AFileName = 'avi.jpg' then
    ANewFileName := 'Najeeb.jpg';
end;
Privacy | Legal | Feedback | Newsletter © 2002-2010 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.