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 1)

Get right down to the metal and write code that will accomplish your PDF-related needs.
By Mohammed Najeemudheen and Suraj C. B.

This article is written with the assumption that you don't want a whole lot of technical information about the PDF format or PDFtoolkit. Instead, we assume that you wish to know just how easily PDFtoolkit can help you work with PDF documents.

Still, if you require it, a good introduction to PDF is available in an earlier article titled PDFtoolkit v3.0 and its new PDF Processor. A introduction to PDFtoolkit architecture is another article titled PDFtoolkit At A Glance.

To illustrate the capabilities of PDFtoolkit, we created a demo application that performs these simple but necessary functions.

Reading a PDF document

function LoadFromFile(const FileName: string): Boolean;

In PDFtoolkit VCL, TgtPDFDocument is the class representing a PDF document. To read a document, we call the LoadFromFile() function.

PDFDoc: TgtPDFDocument;
...
PDFDoc.LoadFromFile('input_file.pdf');

Determine number of pages in a PDF document

property PageCount: Integer;

The TgtPDFDocument class has a PageCount property that will tell you the number of pages in a PDF document.

lblNoOfPages: TLabel;
...
// Check if a document is loaded
if PDFDoc.IsLoaded then
 lblNoOfPages.Caption :=
       'Number of Pages: ' +
       IntToStr(PDFDoc.PageCount);

Write an HTML-formatted string to a PDF document

procedure TextOut(HTMLStr: String;
                  XPos, YPos: Double); overload;
procedure TextOut(HTMLStr: String;
                  PageRange: String;
                  XPos, YPos: Double); overload;

You can write formatted text with simple HTML style tags to a PDF document using TextOut() method.

edPageRange: TEdit;
edXcord: TEdit;
edYCord: TEdit;

if PDFDoc.IsLoaded then
 begin
  if edPageRange.Text <> '' then
   PDFDoc.TextOut('<i>Hello, <b>World!</b></i>',
                  edPageRange.Text,
                  StrToFloat(edXCord.Text),
                  StrToFloat(edYCord.Text))
  else
   PDFDoc.TextOut('<i>Hello, <b>World!</b></i>',
                  StrToFloat(edXCord.Text),
                  StrToFloat(edYCord.Text));

end;

You can use simple HTML tags such as b (bold), i (italic), s (strikeout), and u (underline).

Specify a PowerPoint-style transition effect for a PDF page

procedure SetPageTransitionEffect(
      PageRange: string;
      Duration: Single;
      TransitionEffect: TgtTransitionEffect); virtual;

PDF supports Microsoft® PowerPoint®-style page transitions. PDFtoolkit allows you to add transitions to pages using the SetPageTransitionEffect() method.

edTransitionPageRange: TEdit;
cbTransitionEffect: TComboBox;
edDuration: TEdit;

...
PDFDoc.SetPageTransitionEffect(
           edTransitionPageRange.Text,
	   StrToInt(edDuration.Text),
	   TgtTransitionEffect(cbTransitionEffect.ItemIndex));

TgtTransitionEffect is an enumeration that defines various transition styles. To see page transitions in action, view the PDF in full screen in Adobe® Reader™.

Delete a page in a PDF document

procedure DeletePages(PageRange: string);

To delete a page, load the document, call the DeletePages() method, and save the document.

edDelPageRange: TEdit;
...
if PDFDoc.IsLoaded then
 begin
  PDFDoc.DeletePages(Trim(edDelPageRange.Text));
  SaveDocument; // See below
 end

Insert pages from another PDF document

procedure InsertPagesFrom(
   Document: TgtPDFDocument;
   Pages: string;
   InsertAfterPage: Integer); overload; virtual;
procedure InsertPagesFrom(
   FileName: string;
   Pages: string;
   InsertAfterPage: Integer); overload; virtual;

You can insert pages from an existing document specified by its pathname or by a loaded TgtPDFDocument object.

edLoadSecondDoc: TEdit;
edInsertPages: TEdit;
edInsertafter: TEdit;
...
procedure TForm1.InsertPagesFromDoc;
var
 LTempDoc: TgtPDFDocument;
begin
 try
  LTempDoc := TgtPDFDocument.Create(NIL);
  LTempDoc.LoadFromFile(Trim(edLoadSecondDoc.Text));
  if PDFDoc.IsLoaded then
   begin
    PDFDoc.InsertPagesFrom(
              LTempDoc,
	      Trim(edInsertPages.Text),
              StrToInt(edInsertafter.Text));
    SaveDocument;  // See below
   end;
 finally
  LTempDoc.Free
 end;
end;

Extract pages to another PDF document

procedure ExtractPagesTo(
    Document: TgtPDFDocument;
    PageRange: string); overload; virtual;
procedure ExtractPagesTo(
    FileName: string;
    PageRange: string); overload; virtual;

PDFtoolkit can extract pages to a specified pathname or a loaded TgtPDFDocument object.

procedure TForm1.ExtractPage;
begin
  if PDFDoc.IsLoaded then
  begin
    // Extracts pages
    PDFDoc.ExtractPagesTo(
             edExtractPagesToFileName.Text,
	     edExtractPagesToPagerRange.Text);
  end;
end;

Saving a PDF document that has been modified

procedure SaveToFile(
    const FileName: string;
    NeedAppearance: TgtNeedAppearanceState = naNoChange);

To save a file that has been loaded, call the SaveToFile() method.

edSaveTofile: TEdit;
...
procedure TForm1.SaveDocument;
begin
 if edSaveTofile.Text <> '' then
  begin
   PDFDoc.ShowSetupDialog := False;
   PDFDoc.OpenAfterSave := True;
   PDFDoc.SaveToFile(Trim(edSaveTofile.Text),naEnable);
  end
 else
  PDFDoc.SaveToFile('Output.pdf');
end;

Next month, we will continue this series with some other everyday PDF-related tasks that can be performed with PDFtoolkit.

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