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

Reading and Writing PDF Document Information Properties In Delphi

Learn how to read and edit document information properties of a PDF file using PDFtoolkit VCL.
By V. Subhash

Document information properties are a very useful subset of a document's metadata. PDF metadata can be useful applications such as file indexing services, search engine spiders, and PDF viewers and printers. Document properties can also be valuable for human users.

In this article, you will to read and write document information properties such as title, author, subject, and keywords.

Reading Document Information Properties

The TgtPDFDocument.DocInfo property exposes the document information properties of a loaded PDF document. This property is represented by an instance of TgtPDFInfo class. The properties of the TgtPDFInfo instance represents various document information properties of a document.

Here is some example code for reading document information properties.

{
 This code example illustrates how to extract
 PDF document information properties.
}
program Examples_GetDocInfo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  gtPDFDoc;

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

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

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

      // Display document information properties 
      Writeln('Title: '
              + gtPDFDocument1.DocInfo.Title);
      Writeln('Author: '
              + gtPDFDocument1.DocInfo.Author);
      Writeln('Subject '
              + gtPDFDocument1.DocInfo.Subject);
      Writeln('Keywords: '
              + gtPDFDocument1.DocInfo.Keywords);
      Writeln('Creator: '
              + gtPDFDocument1.DocInfo.Creator);
      Writeln('Producer: '
              + gtPDFDocument1.DocInfo.Producer);
      Writeln('Created On: '
              + gtPDFDocument1.DocInfo.CreationDate);
      Writeln('Modified On: '
              + gtPDFDocument1.DocInfo.ModDate);

     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.

Writing Document Information Proeperties

Just as you can read the properties, you can also set their values, as shown below.

{
 This code example illustrates how to specify
 PDF document information properties.
}
program Examples_SetDocInfo;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  gtPDFDoc;

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

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

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

      // Set new document information properties
      gtPDFDocument1.DocInfo.Title := 'To Be Or Not To Be';
      gtPDFDocument1.DocInfo.Author := 'V. Subhash';
      gtPDFDocument1.DocInfo.Subject
                        := 'Hamlet''s Immortal Dilemma';
      gtPDFDocument1.DocInfo.Keywords
                        := 'hamlet, shakespeare, dilemma, english, drama, play';
      gtPDFDocument1.DocInfo.Creator
                        := 'Gnostice PDFtoolkit Newsletter Article Demo';

      // Save the modified document to a file
      gtPDFDocument1.SaveToFile('output_doc.pdf');

      Writeln('Output document written sucessfully.');

     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.

Please note that the Producer property is read-only. It will always be set to the Gnostice PDF processor used by PDFtoolkit.

---o0O0o---

Our Developer Tools
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.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

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

XtremeDocumentStudio .NET

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

PDFOne (for Java™)

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

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 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 XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).