Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Scale image to fit into pdf Page using Lazarus "fpPDF" Unit  (Read 273 times)

Weitentaaal

• Sr. Member
• Posts: 408
• Weitental is a very beautiful garbage depot.
Scale image to fit into pdf Page using Lazarus "fpPDF" Unit
« on: August 02, 2022, 02:37:34 pm »
Hello,

i was trying to print a pdf with a Picture on it, result is attachet.

How do i scale the Picture Down so it will fit ?

Here is my Code:

Code: Pascal  [Select][+][-]
1. procedure SimpleImage(Path: String; D: TPDFDocument; APage: integer);
2. Var
3.   P: TPDFPage;
4.   FtTitle: integer;
5.   IDX: Integer;
6.   W, H: TPDFFloat;
7. begin
8.   P := D.Pages[APage];
9.   // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
11.
12.   { Page title }
13.   P.SetFont(FtTitle,23);
14.   P.SetColor(clBlack, false);
15.   P.WriteText(25, 20, 'Sample Image Support');
16.
17.   P.SetFont(FtTitle,10);
18.   P.SetColor(clBlack, false);
19.
21.   W := PDFTomm(D.Images[IDX].Width);
22.   H := PDFTomm(D.Images[IDX].Height);
23.
24.   P.DrawImage(20, 20, w, h, IDX);
25. end;
26.
27. procedure SaveDocument(Path: String; D : TPDFDocument);
28. begin
29.   if FileExists('C:\Users\User1\Desktop\TEST.pdf') then DeleteFile('C:\Users\User1\Desktop\TEST.pdf');
30.   D.SaveToFile('C:\Users\User1\Desktop\TEST.pdf');
31. end;
32.
33. function SetUpDocument: TPDFDocument;
34. var
35.   P: TPDFPage;
36.   S: TPDFSection;
37.   i: integer;
38.   lPageCount: integer;
39. begin
40.   Result := TPDFDocument.Create(Nil);
41.   Result.Infos.Title := Application.Title;
42.   Result.Infos.Author := 'Graeme Geldenhuys';
43.   Result.Infos.Producer := 'fpGUI Toolkit 1.4.1';
44.   Result.Infos.ApplicationName := ApplicationName;
45.   Result.Infos.CreationDate := Now;
46.   Result.Infos.KeyWords:='fcl-pdf demo PDF support Free Pascal';
47.
48.   Result.StartDocument;
49.   S := Result.Sections.AddSection; // we always need at least one section
50.   lPageCount := 1;
51.   for i := 1 to lPageCount do
52.   begin
54.     P.PaperType := ptA4;
55.     P.UnitOfMeasure := uomMillimeters;
57.   end;
58. end;
59.
60. procedure TForm1.FormCreate(Sender: TObject);
61. var
62.    D: TPDFDocument;
63. begin
64.    D:= SetUpDocument;
65.    SimpleImage('C:\Users\User1\Desktop\Unbenannt.PNG', D, 0);
66.    SaveDocument(D);
67. end;
68.
69.
« Last Edit: August 02, 2022, 02:40:44 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

Weitentaaal

• Sr. Member
• Posts: 408
• Weitental is a very beautiful garbage depot.
Re: Scale image to fit into pdf Page using Lazarus "fpPDF" Unit
« Reply #1 on: August 02, 2022, 02:42:06 pm »
Found it out by Myself.

The TPDFPage Class has a Property Paper so you can use Something like this:

Code: Pascal  [Select][+][-]
1. Margin = 20
2.
3.  P.DrawImage(Margin, Margin, PDFTomm(P.Paper.W) - Margin, PDFTomm(P.Paper.h) - Margin, IDX);
4.
5.
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0