eDocEngine VCL
Create documents and reports in 18 formats
Compatibility
Delphi C++Builder

Convert Multipage TIFF To PDF Using eDocEngine VCL

Also using Erik Van Bilsen's Delphi GDI+ library.
By V. Subhash

TIFF handling in GDI+ is extremely awkward. Erik Van Bilsen's Delphi GDI+ library makes it easy as pie. I can't sing enough praise of it.

If you follow our newsletter article, then you might know that we have used it before to demonstrate PDF-to-TIFF conversion. In this article, we will do the opposite - TIFF-to-PDF conversion.

Here is the multipage TIFF image that I used. Please note that this image has 6 frames but you see only the first frame.

The following is the Delphi code example to convert a TIFF image to a PDF document.

program TIFF_To_PDF_With_eDocEngine;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Windows, Graphics, jpeg,
  GdiPlus, GdiPlusHelpers, // Using Erik van Bilsen's Delphi GDI+ library
  gtPDFDoc, gtCstPDFDoc, gtClasses3, gtCstDocEng, gtCstPlnEng,
  gtCstPDFEng, gtExPDFEng, gtPDFEng;

var
  TIFFImage: IGPImage;
  PageGuid: TGUID;
  JPEGImage1: TJPEGImage;
  PageCount, I: Integer;
  PageDimensions: TGPSizeF;
  gtPDFEngine1: TgtPDFEngine;

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;
    PageCount := TIFFImage.GetFrameCount(PageGuid);

    if PageCount > 0  then begin
      // Create PDF engine
      gtPDFEngine1 := TgtPDFEngine.Create(nil);
      gtPDFEngine1.FileName := 'multi_page.pdf';
      gtPDFEngine1.MeasurementUnit := muPixels;
      gtPDFEngine1.Preferences.ShowSetupDialog := False;
      gtPDFEngine1.Page.PaperSize := Custom;

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

        // Set PDF engine page dimension same as
        // that of current frame of the TIFF image
        gtPDFEngine1.Page.Width := PageDimensions.Width;
        gtPDFEngine1.Page.Height := PageDimensions.Height;

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

        if i = 0 then begin
          // Create first PDF page
          gtPDFEngine1.BeginDoc; // Do this only after setting page dimensions
        end else begin
          // Create next PDF page
          gtPDFEngine1.NewPage;
        end;

        // Load temporary JPEG image
        JPEGImage1 := TJPEGImage.Create;
        JPEGImage1.LoadFromFile('temp_frame.jpg');
        // Render JPEG image on the current PDF page
        gtPDFEngine1.DrawImage(0,0, JPEGImage1);
      end;
      // Save PDF document to file
      gtPDFEngine1.EndDoc;
    end;

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

Here is the output 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.