Blog

All Blog Posts  |  Next Post  |  Previous Post

Tips & tricks creating PDF documents in Delphi / VCL

Bookmarks: 

Wednesday, October 26, 2022

TMS Software Delphi  Components

TMS VCL UI Pack has a lot of powerful components, both visual and non-visual. One of them is the TAdvPDFLib component. TAdvPDFLib is capable of generating PDF files in a Delphi VCL application.

Getting Started

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

AdvPDFLib1.BeginDocument('MyPDF.pdf');
try
  AdvPDFLib1.NewPage;
finally
  AdvPDFLib1.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
  AdvGraphicsTypes, Types;

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

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

  finally
    AdvPDFLib1.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 TAdvGraphics class can be used for more complex drawing. Below is a sample with the descendant class TAdvGraphicsPDFEngine, specifically designed for exporting graphics through TAdvPDFLib. 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
  AdvGraphicsTypes, AdvGraphicsPDFEngine,
  AdvTypes, Types;

procedure TForm1.GeneratePDF;
var
  g: TAdvGraphicsPDFEngine;
  bmp: TAdvBitmap;
begin
  AdvPDFLib1.BeginDocument('MyPDF.pdf');
  g := TAdvGraphicsPDFEngine.Create(AdvPDFLib1);
  bmp := TAdvBitmap.CreateFromFile('tiger.svg');
  try
    AdvPDFLib1.NewPage;

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

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

TMS Software Delphi  Components

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

Go-To Actions

A go-to action changes the view to a specified destination (page, location, and magnification factor). With TAdvPDFLib 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
  AdvGraphicsTypes, Types;

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

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

    AdvPDFLib1.NewPage;

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

  finally
    AdvPDFLib1.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
  AdvPDFLib1.EmbedFonts := False;
  AdvPDFLib1.BeginDocument('MyPDF.pdf');
  try
    AdvPDFLib1.NewPage;
  finally
    AdvPDFLib1.EndDocument(True);
  end;
end;



Pieter Scheldeman


Bookmarks: 

This blog post has received 12 comments.


1. Thursday, October 27, 2022 at 2:08:20 PM

How would you do watermarks?

James Flejter


2. Thursday, October 27, 2022 at 2:25:45 PM

There is no watermark support included. This is on our todolist.

Pieter Scheldeman


3. Thursday, October 27, 2022 at 4:49:00 PM

Hi Peter,
Two other features for the todolist (they are a showstoppers for us with the current version):
- Text fields (PDF Forms)
- Drop-Down fields (PDF Forms)
Any chance of seeing that soon ?
Kind regards,

Carre Stephane


4. Thursday, October 27, 2022 at 4:52:01 PM

I''ll make sure to add this to our list and see if we can work on an update that includes these features. Thanks for the feedback Stephane!

Pieter Scheldeman


5. Friday, October 28, 2022 at 11:23:09 AM

Hi Peter,
Pdf Forms and complex pdf tax documents are common on our activity.
There is a way for LOAD an existing pdf (some pages) and then complete them with data generating a new filled document?
Now we obtain this with another product but the best is if this is possibile in TMS family, specially in FNC format.
Thanks in advance.
Kind Regards,

Monterisi Stefano


6. Friday, October 28, 2022 at 11:42:48 AM

Thanks for the feedback Stefano! Right now, the PDF library is capable of generating new documents only, but merging existing documents have been added to our feature request list.

Pieter Scheldeman


7. Tuesday, December 6, 2022 at 10:43:57 AM

A couple more requests:
1. Bookmarks / table of contents
2. Tables
Nice to have:
3. Import from RTF

Thanks,
Robin

Robin Pellatt


8. Tuesday, December 6, 2022 at 10:46:03 AM

Thanks Robin!

Pieter Scheldeman


9. Saturday, May 13, 2023 at 9:08:14 PM

is there a help file with the properties and methods for this ?


Daljeet Arvin


10. Sunday, May 14, 2023 at 5:38:22 PM

Hi,

You can find the link to the documentation here: https://download.tmssoftware.com/download/manuals/TMS%20TAdvPDFLib%20Developers%20Guide.pdf


Pieter Scheldeman


11. Friday, March 8, 2024 at 5:19:15 PM

Please, I need a PDF component that works exactly as Delphi Print.Canvas.
I program (with Delphi XE) an astrology software that print sky charts and reports.
All, graphics and text, are vcl.Canvas commands.
Is this possible with TAdvPDFLib component or is necessary to replace all Print.Canvas commands to AdvPDFLib commands?
Thank you.

Paulo Ferreira


12. Sunday, March 10, 2024 at 3:47:42 PM

It is needed to do the drawing on the Graphics: TTMSFNCGraphics object from the PDF component.

Bruno Fierens




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