procedure Sample (const aFileName: string);
var
doc : TPDFDocument;
sec : TPDFSection;
page : TPDFPage;
fs : TFileStream;
fontText : integer;
pageWidth, pageHeight: integer;
x, y : double;
horizBorder, vertBorder: integer;
currentPage, i, split, rowHeight: integer;
tableCols : array [1..4] of integer;
tableCoords : array [1..5] of integer;
tmp: String;
function EstimatePos (aBorderDx : integer; aText : string) : double;
begin
Result := aBorderDx - 2 - (Length(aText) * 4.77);
end;
procedure WriteFooter (const aFineDoc : boolean);
var
tString : String;
begin
page.SetColor(clDkGray, false);
page.SetFont(fontText, 10);
tString := 'PAGE ' + IntToStr(currentPage);
page.WriteText(x, 20, tString);
page.SetColor(clBlack, false);
end;
procedure AddPageIfNecessary();
begin
if (currentPage = 0) or (y <= vertBorder) then
begin
if currentPage > 0 then
WriteFooter(false);
inc (currentPage);
page := doc.Pages.AddPage;
page.PaperType:= ptA4;
page.UnitOfMeasure:= uomPixels;
page.Orientation:= ppoPortrait;
pageWidth := round(page.Paper.Printable.R - page.Paper.Printable.L);
pageHeight := round(page.Paper.Printable.B - page.Paper.Printable.T);
sec.AddPage(page);
x := horizBorder;
y := pageHeight - vertBorder;
// write a copyright notice on the right side of the page
page.SetFont(fontText, 5);
page.WriteText(pageWidth, y, '42 L.t.d. ' + UTF8Encode('©') + ' www.42.com', 270);
end;
end;
begin
doc := TPDFDocument.Create(Nil);
try
doc.Infos.Title:= 'My memories';
doc.Infos.Author:= 'Zaphod Beeblebrox';
doc.Infos.Producer:= 'Sample application';
doc.Infos.CreationDate:= Now;
// with poPageOriginAtTop: coords are calculated from the top of the page
doc.Options:= [poCompressFonts, poCompressText, poCompressImages];
doc.StartDocument;
sec:= doc.Sections.AddSection;
fontText:= doc.AddFont('c:\temp\Times.ttf', 'Times');
rowHeight:= 12;
horizBorder:= 30;
vertBorder:= 30;
AddPageIfNecessary;
split := (pageWidth div 6);
tableCols[1] := (split);
tableCols[2] := (split * 2); // doubled size
tableCols[3] := (split * 2); // doubled size
tableCols[4] := (split);
tableCoords[1] := trunc(x);
tableCoords[2] := tableCols[1] + TableCoords[1];
tableCoords[3] := tableCols[2] + TableCoords[2];
tableCoords[4] := tableCols[3] + TableCoords[3];
tableCoords[5] := tableCols[4] + TableCoords[5];
for i := 0 to 9999 do
begin
y := y - rowHeight;
AddPageIfNecessary;
page.SetFont(fontText, 10);
page.DrawRect(tableCoords[1], y , tableCols[1], rowHeight, 0.2, false, true);
page.WriteText(tableCoords[1] + 2, y + 3 , 'COL_1');
page.DrawRect(tableCoords[2], y , tableCols[1], rowHeight, 0.2, false, true);
page.WriteText(tableCoords[2] + 2, y + 3 , 'COL_2');
page.DrawRect(tableCoords[3], y , tableCols[1], rowHeight, 0.2, false, true);
page.WriteText(tableCoords[3] + 2, y + 3 , 'COL_3');
tmp := FormatFloat('#,##0.00', 333.554);
page.DrawRect(tableCoords[4], y , tableCols[4], rowHeight, 0.2, false, true);
page.WriteText(EstimatePos(tableCoords[5], tmp), y + 3 , tmp);
end;
y := y - 16;
AddPageIfNecessary;
page.SetFont(fontText, 14);
page.WriteText(x, y, 'END of the document');
WriteFooter(true);
fs := TFileStream.Create(aFileName, fmCreate);
try
doc.SaveToStream(fs);
finally
fs.Free;
end;
finally
doc.Free;
end;
end;