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.
| Placeholder | Substituted By |
| PageNo | Number of current page |
| TotPage | Total number of pages |
| ShortDate | Date in short format |
| ShortTime | Time in short format |
| LongDate | Date in long format |
| LongTime | Date in long format |
| Author | Document author property (See TgtPDFDocument.DocInfo) |
| Creator | Document creator property (See TgtPDFDocument.DocInfo) |
| Subject | Document subject property (See TgtPDFDocument.DocInfo) |
| Title | Document 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.
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.