program Project1;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, Graphics, fppdf, fpttf, LCLIntf;
const
colarr: Array [1..5] of TColor = (clYellow, clAqua, clLime, clRed, clSkyBlue);
var
d: TPDFDocument;
s: TPDFSection;
p: TPDFPage;
g: TFPFontCacheList;
f: TFPFontCacheItem;
i, j, pw, ph: Integer;
fname: String;
begin
fname := 'fclpdf-test.pdf';
g := TFPFontCacheList.Create;
g.SearchPath.Add('C:\Windows\Fonts\');
g.BuildFontCache;
//doc properties
d := TPDFDocument.Create(nil);
d.Options := [poCompressText, poCompressImages, poUseRawJPEG, poNoEmbeddedFonts, poPageOriginAtTop];
d.Infos.Title := 'sample pdf';
d.Infos.Author := 'me';
d.Infos.Producer := 'lazarus';
d.Infos.ApplicationName := 'test';
d.Infos.CreationDate := Now;
d.StartDocument;
s := d.Sections.AddSection;
f := g.Find('Courier', False, False);
if f <> nil then
d.AddFont(f.FileName, f.FamilyName);
f := g.Find('Arial', False, False);
if f <> nil then
d.AddFont(f.FileName, f.FamilyName);
f := g.Find('Times New Roman', False, False);
if f <> nil then
d.AddFont(f.FileName, f.FamilyName);
//
for j := 1 to 5 do
begin
p := d.Pages.AddPage;
case j of
2: p.PaperType := ptA5;
3: p.PaperType := ptLetter;
5: p.PaperType := ptComm10;
else
p.PaperType := ptA4;
end;
if j mod 2 = 0 then
p.Orientation := ppoLandscape
else
p.Orientation := ppoPortrait;
p.UnitOfMeasure := uomMillimeters;
s.AddPage(p);
pw := trunc(p.Paper.W * 25.4 / 72);
ph := trunc(p.Paper.H * 25.4 / 72);
for i := 10 to ph - 10 do
begin
if (i mod 10 = 0) or (i = ph - 10) then
p.DrawLine(10, i, pw - 10, i, 0.1);
end;
for i := 10 to pw - 10 do
begin
if (i mod 10 = 0) or (i = pw - 10) then
p.DrawLine(i, 10, i, ph - 10, 0.1);
end;
p.SetFont(0, 40);
p.SetColor(clBlack, false);
p.WriteText(20, 20, 'Page ' + IntToStr(j));
p.SetColor(colarr[j], False);
p.DrawRect(100, 100, 50, 50, 1, True, False);
if j = 1 then
begin
p.SetColor(clMaroon, False);
p.SetFont(1, 24);
p.WriteText(10, 50, 'table of contents:');
for i := 2 to 5 do
begin
p.WriteText(10, 50 + (i * 10) - 10, 'Page ' + IntToStr(i));
{$IF FPC_FULLVERSION>30202}p.AddInternalLink(10, 50 + (i * 10) - 10, 30, 8, i - 1){$ENDIF}; //link to page
end;
end
else
begin
p.SetColor(clMaroon, False);
p.SetFont(1, 24);
p.WriteText(10, 50, 'goto page 1');
p.AddInternalLink(10, 50, 30, 8, 0);
end;
end;
d.SaveToFile(fname);
d.Free;
OpenDocument(fname);
end.