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

Using PDFtoolkit VCL for Your Everyday PDF-Related Tasks (Part 2)

Learn to extract text, fill form field, and add attachments.
By Mohammed Najeemudheen and Suraj C. B.

Last month in our series, we saw how to perform some basic tasks in meeting everyday PDF-related needs.

This month, we will see how to:

  1. Extracting text from a PDF document:
    function ExtractText(
              APageNo: Integer): TStringList;
    function ExtractTextFormatted(
              APageNo: Integer): TStringList; 
    
    This method extracts text from a specified page and returns the text in a TStringList.
    PDFDoc: TgtPDFDocument;
    LStrList: TStringList;
    
    edExtPageno: TEdit;
    edExtTxtFileName: TEdit;
    edExtPageno: TEdit;
    rbExtTxt: TRadioButton;
    ...
    PDFDoc.LoadFromFile('input_file.pdf')
    ...
    
    if PDFDoc.IsLoaded then
     begin
      try
       LStrList := TStringList.Create;
       if rbExtTxt.Checked then
        // Extracts text without worrying about formatting
        LStrList := 
          PDFDoc.ExtractText((edExtPageno.Text))
       else
        // Extracts text with whatever formatting is possible
        LStrList := 
           PDFDoc.ExtractTextFormatted(
              StrToInt(edExtPageno.Text));
        LStrList.SaveToFile(edExtTxtFileName.Text);
      finally
       LStrList.Free;
      end;
    end;
    

  2. Fill a form field: If you know the name of a form field, you can change its value by using the method:
    procedure SetFormFieldValue(FieldName: string; 
                                Value: string);
    

    Specify the field name and the new value, and its done.

    edEditFormFieldName: TEdit;
    edNewValue: TEdit;
    ...
    if PDFDoc.IsLoaded then
     begin
      PDFDoc.SetFormFieldValue(
                edEditFormFieldName.Text,
                edNewValue.Text);
     end;
    SaveDocument;
    
  3. Embedding A File Attachment: First create an object of TgtPDFFileAttachment, specify the file that needs to be attached, specify the file name for the attachment in the PDF, location where the attachment icon will be, type of the icon used for the attachment, and call the method:
    procedure InsertFileAttachment(
       AFile: TgtPDFFileAttachment; 
       PageNumber: Integer); 
    
    property FileAttachmentIcon: TgtFileAttachmentIcon 
       read FFileAttachmentIcon 
       write FFileAttachmentIcon;
    
    TgtFileAttachmentIcon = 
       (faGraph, faPaperclip, faPushPin, faTag); 
    
    Here is the code:
    LFileAttach: TgtPDFFileAttachment;
    edAttachment: TEdit;
    edAttachmentName: TEdit;
    edAttachPageno: TEdit;
    
    ...
    try
     LFileAttach := TgtPDFFileAttachment.Create;
     // pathname of the attachment in the file system
     LFileAttach.FileName := edAttachment.Text;
     // name of the attachment in the document
     LFileAttach.AttachmentName := edAttachmentName.Text;
     // location of the attachment icon
     LFileAttach.SetBounds(50,50,300,300);
     // type of the attachment icon
     LFileAttach.FileAttachmentIcon := faGraph;
     // Insert the attachment
     PDFDoc.InsertFileAttachment(
         LFileAttach,
         StrToInt(edAttachPageno.Text));
    
     SaveDocument;
    finally
     LFileAttach.Free;
    end; 
    
---o0O0o---

Downloads:

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.