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

How To Add Auto Text To PDF Documents

Learn to use placeholders in PDFtoolkit text outs on PDF pages.
By V. Subhash

If you have used word-processing software such as Microsoft Word, you may be familiar with the concept of "auto text." PDFtoolkit brings the same functionality to PDF by supporting placeholders in PDF page text-outs. You can use placeholders with TgtTextWatermarkTemplate class and the TgtDFDocument.TextOut method. PDFtoolkit comes with the following built-in placeholders.

PlaceholderSubstituted By
PageNoNumber of current page
TotPageTotal number of pages
ShortDateDate in short format
ShortTimeTime in short format
LongDateDate in long format
LongTimeDate in long format
AuthorDocument author property (See TgtPDFDocument.DocInfo)
CreatorDocument creator property (See TgtPDFDocument.DocInfo)
SubjectDocument subject property (See TgtPDFDocument.DocInfo)
TitleDocument title property (See TgtPDFDocument.DocInfo)

PDFtoolkit also supports custom or user-defined placeholders. Your application can provide values for custom placeholders by handling the TgtPDFDocument.OnCalcVariables event.

Both built-in and custom placeholders need to be delimited by <% and %>. When the output document is being paged, the placeholders will be replace with their values available at run time. Here is an example.

{
 This code example illustrates how to write text
 watermarks to a document. It also illustrates the
 use of autotext placeholders.
}
program Examples_Autotext;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  gtPDFDoc, gtCstPDFDoc;

var
  gtPDFDocument1: TgtPDFDocument;
  gtTextWatermarkTemplate1: TgtTextWatermarkTemplate;
begin
  // Create a document object
  gtPDFDocument1 := TgtPDFDocument.Create(Nil);
  gtTextWatermarkTemplate1 := TgtTextWatermarkTemplate.Create();

  try
    // Load input document
    gtPDFDocument1.LoadFromFile('input_doc.pdf');

    // Check if document has loaded successfully
    if gtPDFDocument1.IsLoaded then
     begin

      // Set watermark properties
      With gtTextWatermarkTemplate1 do
      begin
        Font.Name := 'Courier New';
        Font.Size := 8;
        Text := 'Autotext example [PageNo - <%PageNo%>, TotPage - <%TotPage%>, '
                   + 'ShortDate - <%ShortDate%>, Author - <%Author%>]';
        Overlay := false;      // Underlay
        OpacityStroke := 100;  // Full opaqueness
        HorizPos := hpCenter;  // Relative positioning
        VertPos := vpCustom;   // Absolute positioning
        Y := 10;               // Absolute vertical position
      end;      
      
      // Apply the watermark on all pages
      gtPDFDocument1.InsertWatermark(gtTextWatermarkTemplate1);

      // Save modified document
      gtPDFDocument1.SaveToFile('output_doc.pdf');

     end
    else
     begin
      // Output error message
      Writeln('Sorry, I could not load input_doc.pdf.');
     end;
  except on Err:Exception do
   begin
     Writeln('Sorry, an exception was raised. ');
     Writeln(Err.Classname + ':' + Err.Message);
   end;
  end;
  // Free resources
  gtPDFDocument1.Reset;
  // Destroy PDF document object
  FreeAndNil(gtPDFDocument1);

  Writeln('Press Enter to exit.');
  Readln;
end.

And, here is the output.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .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
Gnostice Document Studio 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
Gnostice Document Studio 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-2023 Gnostice Information Technologies Private Limited. All rights reserved.