Blog

All Blog Posts  |  Next Post  |  Previous Post

Freebie Friday: Creating PDF Documents in Delphi / VCL

Friday, February 7, 2025

TMS Software Delphi  Components

It's Friday, and it's time for a freebie.

TMS Software Delphi  Components

TMS FNC UI Pack has a lot of powerful components, both visual and non-visual. One of them is the TTMSFNCPDFLib component. TTMSFNCPDFLib is part of the FNC family and is able to target Windows, macOS, Linux, iOS, Android & WEB applications across multiple frameworks. Today we'll focus on export PDF files in a Delphi VCL application.

Getting Started

To get started, drop a TTMSFNCPDFLib component on the form. Generating a single page PDF is done with a minimum of 3 calls.

TMSFNCPDFLib1.BeginDocument('MyPDF.pdf');
try
  TMSFNCPDFLib1.NewPage;
finally
  TMSFNCPDFLib1.EndDocument(True);
end;

Basic drawing 

Generating a PDF starts by specifying a file name, adding the first page, and then the PDF context is ready to be accessed via the Graphics property. In the sample below, we draw a simple rectangle by setting the properties of the fill & stroke and by calling p.Graphics.DrawRectangle.

uses
  VCL.TMSFNCGraphicsTypes, Types;

procedure TForm1.GeneratePDF;
begin
  TMSFNCPDFLib1.BeginDocument('MyPDF.pdf');
  try
    TMSFNCPDFLib1.NewPage;

    TMSFNCPDFLib1.Graphics.Fill.Color := gcYellowgreen;
    TMSFNCPDFLib1.Graphics.Stroke.Color := gcGreen;
    TMSFNCPDFLib1.Graphics.Stroke.Width := 4;
    TMSFNCPDFLib1.Graphics.DrawRectangle(RectF(100, 100, 300, 300));

  finally
    TMSFNCPDFLib1.EndDocument(True);
  end;
end;

TMS Software Delphi  Components

Complex Drawing

The core drawing engine for the PDF has drawing calls for path, image and basic primitives. Additionally, The TTMSFNCGraphics class can be used for more complex drawing. Below is a sample with the descendant class TTMSFNCGraphicsPDFEngine, specifically designed for exporting graphics through TTMSFNCPDFLib. In the sample, we'll export an SVG file, which is first parsed and translated to individual drawing calls and then afterwards drawn with vector graphics through the engine.

uses
  VCL.TMSFNCGraphicsTypes, VCL.TMSFNCGraphicsPDFEngine,
  VCL.TMSFNCTypes, Types;

procedure TForm1.GeneratePDF;
var
  g: TTMSFNCGraphicsPDFEngine;
  bmp: TTMSFNCBitmap;
begin
  TMSFNCPDFLib1.BeginDocument('MyPDF.pdf');
  g := TTMSFNCGraphicsPDFEngine.Create(TMSFNCPDFLib1);
  bmp := TTMSFNCBitmap.CreateFromFile('tiger.svg');
  try
    TMSFNCPDFLib1.NewPage;

    g.DrawBitmap(RectF(50, 50, 400, 600), bmp);

  finally
    bmp.Free;
    g.Free;
    TMSFNCPDFLib1.EndDocument(True);
  end;
end;

TMS Software Delphi  Components

As you can notice, the graphics are sharp, even when zooming in.

Go-To Actions

go-to action changes the view to a specified destination (page, location, and magnification factor). With TTMSFNCPDFLib we can add actions with the following code. The code will add a clickable link that can be used to navigate to a different page, and make sure the page is completely visible in the reader.

uses
  VCL.TMSFNCGraphicsTypes, Types;

procedure TForm1.GeneratePDF;
begin
  TMSFNCPDFLib1.BeginDocument('MyPDF.pdf');
  try
    TMSFNCPDFLib1.NewPage;

    TMSFNCPDFLib1.Graphics.AddGoTo('Link', '1 /Fit', RectF(50, 50, 150, 150));

    TMSFNCPDFLib1.NewPage;

    TMSFNCPDFLib1.Graphics.Fill.Color := gcYellowgreen;
    TMSFNCPDFLib1.Graphics.Stroke.Color := gcGreen;
    TMSFNCPDFLib1.Graphics.Stroke.Width := 4;
    TMSFNCPDFLib1.Graphics.DrawRectangle(RectF(100, 100, 300, 300));

  finally
    TMSFNCPDFLib1.EndDocument(True);
  end;
end;
More info about actions and the value for the destination can be found here: https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.7old.pdf

Font Embedding

By default, the fonts that are used in a PDF document are embedded. The benefit of this is, that a PDF file can be read without the fonts needing to be present on the operating system of the reader. The downside of embedding fonts is that the size of the PDF file itself can quickly expand depending on the content and number of different fonts. If size is important, and the fonts being used in the PDF are available on the operating system of the reader, you could opt for excluding fonts while generating PDF files. To do so, use the following code snippet.

procedure TForm1.GeneratePDF;
begin
  TMSFNCPDFLib1.EmbedFonts := False;
  TMSFNCPDFLib1.BeginDocument('MyPDF.pdf');
  try
    TMSFNCPDFLib1.NewPage;
  finally
    TMSFNCPDFLib1.EndDocument(True);
  end;
end;


Pieter Scheldeman




This blog post has received 5 comments.


1. Friday, February 7, 2025 at 1:02:14 PM

Excellent topic.
However, what is missing in Delphi is real PDF management, starting from modifying existing PDFs, such as creating invoices or forms starting from a basic PDF, creating new pages starting from a PDF template or duplicating existing pages by completing them with data (text, images). In short, mainly to manage the form filler starting from an empty PDF.
Working in this sense would become very useful and productive in tax/ledger applications

Stefano Monterisi


2. Friday, February 7, 2025 at 1:18:52 PM

Hi,

TTMSFNCPDFLib focuses on PDF generation. It''s quite a challenging task to open and manipulate existing PDFs. However, it''s already on our feature request list so we still keep the opportunity open in the future for having our own PDF document manager. Thanks for the feedback!

Pieter Scheldeman


3. Friday, February 7, 2025 at 2:52:07 PM

A similar example to create MS WORD document would be very nice. After creating and saving a Word document, the user can open it, modify it, and save it as a PDF.

Ertan Baykal


4. Saturday, February 8, 2025 at 4:51:28 AM

agree 100% with Stefano. TMS tools is missing being able to EDI PDF files and put comments and stamps!
please look at https://pdf.easeus.com/?_gl=1*tno29s*_gcl_au*MTkxMzAwNzIyOC4xNzM4OTg2NTMx

thanks for the good developer tools!



tecun jose


5. Tuesday, February 11, 2025 at 2:56:33 PM

We have need to combine existing PDFs to single PDF. That would be nice feature in TMS products.

erva mikko




Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post