- Регистрация
- 9 Май 2015
- Сообщения
- 1,071
- Баллы
- 155
- Возраст
- 52

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

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;
More info about actions and the value for the destination can be found here:
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;