Quantcast
Channel: TMS Software
Viewing all articles
Browse latest Browse all 1006

Freebie Friday: Creating PDF Documents in Delphi / VCL

$
0
0



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;



Viewing all articles
Browse latest Browse all 1006

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>