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.
Important

With the release of Version 4 of PDFtoolkit, form field processing has got simpler. PDF-to-image conversion is much more simpler. A special article titled "Form Fields Creation and Processing Using PDFtoolkit v4" has been written for users of Version 4. This article has been left here for users of older versions of PDFtoolkit.

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:

---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.