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

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 post first.
Go-To a specific page
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;

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', '', 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: