Blog
All Blog Posts | Next Post | Previous Post
Freebie Friday: Creating PDF Documents in Delphi / VCL
Friday, February 7, 2025
It's Friday, and it's time for a freebie.
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;
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;
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 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;
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
![](img/handwrite_message_24.png)
This blog post has received 5 comments.
![](img/bullets/6.png)
![](img/handwrite_message_24.png)
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
![](img/handwrite_message_24.png)
Ertan Baykal
![](img/handwrite_message_24.png)
please look at https://pdf.easeus.com/?_gl=1*tno29s*_gcl_au*MTkxMzAwNzIyOC4xNzM4OTg2NTMx
thanks for the good developer tools!
tecun jose
![](img/handwrite_message_24.png)
erva mikko
All Blog Posts | Next Post | Previous Post
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