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 .NET Developer Tools
XtremeDocumentStudio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.