Blog
All Blog Posts | Next Post | Previous PostTips & tricks creating PDF documents in Delphi / VCL
Wednesday, October 26, 2022
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;
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;
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;
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
This blog post has received 12 comments.
Pieter Scheldeman
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
Pieter Scheldeman
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
Pieter Scheldeman
1. Bookmarks / table of contents
2. Tables
Nice to have:
3. Import from RTF
Thanks,
Robin
Robin Pellatt
Pieter Scheldeman
Daljeet Arvin
You can find the link to the documentation here: https://download.tmssoftware.com/download/manuals/TMS%20TAdvPDFLib%20Developers%20Guide.pdf
Pieter Scheldeman
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
Bruno Fierens
All Blog Posts | Next Post | Previous Post
James Flejter