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;

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 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 XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).