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

Convert Multipage TIFF To PDF Using PDFtoolkit VCL

Exploiting a rarely used content-creation method.
By V. Subhash

PDFtoolkit VCL works on existing PDF documents. You don't create new PDF documents with it. But, it does have a method that allows you to insert a blank page to an existing PDF document.

public procedure InsertBlankPageAt(
    APageNumber: Integer; 
    PageWidth: Double; 
    PageHeight: Double
);

So, you could create a new PDF document from scratch by:

  1. Loading an existing PDF document;
  2. Inserting new blank pages using the InsertBlankPageAt() method; and
  3. Deleting all old pages.

Yes, it is quite a useful method. In this article, we will not go in that direction. Instead, we will use the InsertBlankPageAt() method to create pages that will hold images exported from a multi-page TIFF image.

Delphi Code Sample

Following is the source code of a Delphi console application. It uses Erik Van Bilsen's Delphi GDI+ library to extract frames from a multipage TIFF image. The extracted images are then rendered on to a blank PDF page inserted using InsertBlankPageAt().

program TIFF_To_PDF_With_eDocEngine;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Windows, Graphics, jpeg,
  GdiPlus, GdiPlusHelpers, // Using Erik van Bilsen's Delphi GDI+ library
  gtPDFClasses, gtCstPDFDoc, gtExPDFDoc, gtExProPDFDoc, gtPDFDoc;

var
  TIFFImage: IGPImage;
  PageGuid: TGUID;
  Bitmap1: TBitmap;
  FrameCount, PageCount, I, J: Integer;
  PageRange: string;
  PageDimensions: TGPSizeF;
  gtPDFDocument1: TgtPDFDocument;
  gtImageWatermarkTemplate1: TgtImageWatermarkTemplate;


begin
  try
    // Load the mult-frame TIFF image
    TIFFImage := TGPBitmap.FromFile('multi_page.tiff');
    // Obtain the number of frames in the TIFF image
    PageGuid := FrameDimensionPage;
    FrameCount := TIFFImage.GetFrameCount(PageGuid);

    if PageCount > 0  then begin
      // Load an existing PDF document
      gtPDFDocument1 := TgtPDFDocument.Create(nil);
      gtPDFDocument1.LoadFromFile('input_doc.pdf');

      // Find last page number
      J := gtPDFDocument1.PageCount;

      // Iterate through all frames in the TIFF image
      for I := 0 to FrameCount - 1 do begin
        // Select a frame
        TIFFImage.SelectActiveFrame(PageGuid,I);
        // Obtain dimensins of current frame
        TIFFImage.GetPhysicalDimension(PageDimensions);

        // Insert new page after last page - new page has same
        // dimension as current frame of TIFF image
        J := J + 1;
        gtPDFDocument1.InsertBlankPageAt(J,
                                         PageDimensions.Width,
                                         PageDimensions.Height);

        // Save current frame to an temporary JPEG image
        TIFFImage.Save('temp_frame.bmp', TGPImageFormat.Bmp);

        // Load temporary image
        Bitmap1 := TBitmap.Create;
        Bitmap1.LoadFromFile('temp_frame.bmp');

        // Render image on the new PDF page as an image watermark
        gtImageWatermarkTemplate1 := TgtImageWatermarkTemplate.Create;
        with gtImageWatermarkTemplate1 do begin
          X := 0;
          Y := 0;
          Image := Bitmap1;
          HorizPos := hpCenter;
          VertPos := vpMiddle;
        end;
        gtPDFDocument1.InsertWatermark(gtImageWatermarkTemplate1, IntToStr(J));
        Bitmap1.FreeImage;
      end;

      // Save modified PDF document to a new file
      gtPDFDocument1.SaveToFile('output_doc.pdf');
      // Free resources
      gtPDFDocument1.Reset;
    end;

  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
      Readln;
    end;
  end;
end.

Screenshots

The above code used the following PDF document as the input file.

Bilsen's Delphi GDI+ library was used for extract frames from a multipage TIFF image.

After running the code, frames from the TIFF image were rendered new pages inserted into the PDF document.

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