Blog

All Blog Posts  |  Next Post  |  Previous Post

Advanced PDF actions in Delphi

Today

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. This blog covers more in depth code snippets to add navigation to your PDF document. To get started with TAdvPDFLib, read through this blog post first.


Go-To a specific page

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;


TMS Software Delphi  Components


Go-To a specific rectangle

Let's say you have a part of the PDF that is requiring attention. It's possible to navigate to a specific rectangle in your page. To do this, use the following code.

var
  l, r, t, b: Integer;
begin
  AdvPDFLib1.BeginDocument('MyPDF.pdf');
  try
    AdvPDFLib1.NewPage;

    l := 100;
    t := 100;
    r := 300;
    b := 300;

    AdvPDFLib1.Graphics.AddGoTo('Link', 1, '/FitR ' + l.ToString + ' ' + (AdvPDFLib1.PageHeight - t).ToString + ' ' +
      r.ToString + ' ' + (AdvPDFLib1.PageHeight - b).ToString, 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(l, t, r, b));

  finally
    AdvPDFLib1.EndDocument(True);
  end;
end;
Please note that the `AddGoTo` parameter list includes an overload that allows you to specify a page index as an integer and an options parameter as a string. Since PDF output uses a different coordinate system, we need to perform some calculations before passing the rectangle to the options parameter.

Go-To an URL

Go-To actions not only navigate within the document, it's also possible to include URLs in the PDF document. To do so, use the following code

AdvPDFLib1.BeginDocument('MyPDF.pdf');
try
  AdvPDFLib1.NewPage;
  AdvPDFLib1.Graphics.AddURL('Link', 'https://www.tmssoftware.com', RectF(50, 50, 150, 150));
finally
  AdvPDFLib1.EndDocument(True);
end;


Conclusion

TAdvPDFLib provides powerful functionality for working with PDFs in Delphi VCL applications. With features like the ability to add navigation actions such as go-to links, specific rectangles, and URLs, you can create interactive PDF documents. More info about actions and the possible optional parameters can be found here: https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/pdfreference1.7old.pdf



Pieter Scheldeman




This blog post has not received any comments yet.



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