testfppdf.lpr 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. { This program generates a multi-page PDF document and tests various
  2. functionality on each of the 5 pages.
  3. You can also specify to generate single pages by using the -p <n>
  4. command line parameter.
  5. eg: testfppdf -p 1
  6. testfppdf -p 2
  7. Use -h to see more command line parameter options.
  8. }
  9. program testfppdf;
  10. {$mode objfpc}{$H+}
  11. {$codepage utf8}
  12. uses
  13. {$ifdef unix}cwstring,{$endif} // required for UnicodeString handling.
  14. classes,
  15. sysutils,
  16. custapp,
  17. fpimage,
  18. fpreadjpeg,
  19. fppdf,
  20. fpparsettf;
  21. type
  22. TPDFTestApp = class(TCustomApplication)
  23. private
  24. Fpg: integer;
  25. FRawJPEG,
  26. FImageCompression,
  27. FTextCompression,
  28. FFontCompression: boolean;
  29. FDoc: TPDFDocument;
  30. function SetUpDocument: TPDFDocument;
  31. procedure SaveDocument(D: TPDFDocument);
  32. procedure EmptyPage;
  33. procedure SimpleText(D: TPDFDocument; APage: integer);
  34. procedure SimpleLinesRaw(D: TPDFDocument; APage: integer);
  35. procedure SimpleLines(D: TPDFDocument; APage: integer);
  36. procedure SimpleImage(D: TPDFDocument; APage: integer);
  37. procedure SimpleShapes(D: TPDFDocument; APage: integer);
  38. procedure SampleMatrixTransform(D: TPDFDocument; APage: integer);
  39. protected
  40. procedure DoRun; override;
  41. public
  42. procedure WriteHelp;
  43. end;
  44. var
  45. Application: TPDFTestApp;
  46. function TPDFTestApp.SetUpDocument: TPDFDocument;
  47. var
  48. P: TPDFPage;
  49. S: TPDFSection;
  50. i: integer;
  51. lPageCount: integer;
  52. lOpts: TPDFOptions;
  53. begin
  54. Result := TPDFDocument.Create(Nil);
  55. Result.Infos.Title := Application.Title;
  56. Result.Infos.Author := 'Graeme Geldenhuys';
  57. Result.Infos.Producer := 'fpGUI Toolkit 0.8';
  58. Result.Infos.ApplicationName := ApplicationName;
  59. Result.Infos.CreationDate := Now;
  60. lOpts := [];
  61. if FFontCompression then
  62. Include(lOpts, poCompressFonts);
  63. if FTextCompression then
  64. Include(lOpts,poCompressText);
  65. if FImageCompression then
  66. Include(lOpts,poCompressImages);
  67. if FRawJPEG then
  68. Include(lOpts,poUseRawJPEG);
  69. Result.Options := lOpts;
  70. Result.StartDocument;
  71. S := Result.Sections.AddSection; // we always need at least one section
  72. lPageCount := 6;
  73. if Fpg <> -1 then
  74. lPageCount := 1;
  75. for i := 1 to lPageCount do
  76. begin
  77. P := Result.Pages.AddPage;
  78. P.PaperType := ptA4;
  79. P.UnitOfMeasure := uomMillimeters;
  80. S.AddPage(P); // Add the Page to the Section
  81. end;
  82. end;
  83. procedure TPDFTestApp.SaveDocument(D : TPDFDocument);
  84. var
  85. F: TFileStream;
  86. begin
  87. F := TFileStream.Create('test.pdf',fmCreate);
  88. try
  89. D.SaveToStream(F);
  90. Writeln('Document used ',D.ObjectCount,' PDF objects/commands');
  91. finally
  92. F.Free;
  93. end;
  94. end;
  95. procedure TPDFTestApp.EmptyPage;
  96. var
  97. D: TPDFDocument;
  98. begin
  99. D := SetupDocument;
  100. try
  101. SaveDocument(D);
  102. finally
  103. D.Free;
  104. end;
  105. end;
  106. { all units of measure are in millimeters }
  107. procedure TPDFTestApp.SimpleText(D: TPDFDocument; APage: integer);
  108. var
  109. P : TPDFPage;
  110. FtTitle, FtText1, FtText2, FtText3: integer;
  111. begin
  112. P := D.Pages[APage];
  113. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  114. FtTitle := D.AddFont('Helvetica', clRed);
  115. FtText1 := D.AddFont('FreeSans.ttf', 'FreeSans', clGreen); // TODO: this color value means nothing - not used at all
  116. FtText2 := D.AddFont('Times-BoldItalic', clBlack);
  117. // FtText3 := D.AddFont('arial.ttf', 'Arial', clBlack);
  118. FtText3 := FtText1; // to reduce font dependecies, but above works too if you have arial.ttf available
  119. { Page title }
  120. P.SetFont(FtTitle, 23);
  121. P.SetColor(clBlack, false);
  122. P.WriteText(25, 20, 'Sample Text');
  123. // -----------------------------------
  124. // Write text using PDF standard fonts
  125. P.SetFont(FtTitle, 12);
  126. P.SetColor(clBlue, false);
  127. P.WriteText(25, 50, '(25mm,50mm) Helvetica: The quick brown fox jumps over the lazy dog.');
  128. P.SetFont(ftText2,16);
  129. P.SetColor($c00000, false);
  130. P.WriteText(60, 100, '(60mm,100mm) Times-BoldItalic: Big text at absolute position');
  131. // -----------------------------------
  132. // TrueType testing purposes
  133. P.SetFont(ftText3, 13);
  134. P.SetColor(clBlack, false);
  135. P.WriteText(15, 120, 'Languages: English: Hello, World!');
  136. P.WriteText(40, 130, 'Greek: Γειά σου κόσμος');
  137. P.WriteText(40, 140, 'Polish: Witaj świecie');
  138. P.WriteText(40, 150, 'Portuguese: Olá mundo');
  139. P.WriteText(40, 160, 'Russian: Здравствуйте мир');
  140. P.WriteText(40, 170, 'Vietnamese: Xin chào thế giới');
  141. P.SetFont(ftText1, 13);
  142. P.WriteText(15, 185, 'Box Drawing: ╠ ╣ ╦ ╩ ├ ┤ ┬ ┴');
  143. P.WriteText(15, 200, 'Typography: “What’s wrong?”');
  144. P.WriteText(40, 210, '£17.99 vs £17·99');
  145. P.WriteText(40, 220, '€17.99 vs €17·99');
  146. P.WriteText(40, 230, 'OK then… (êçèûÎÐð£¢ß) \\//{}()#<>');
  147. P.WriteText(25, 280, 'B субботу двадцать третьего мая приезжает твоя любимая теща.');
  148. end;
  149. procedure TPDFTestApp.SimpleLinesRaw(D: TPDFDocument; APage: integer);
  150. var
  151. P: TPDFPage;
  152. FtTitle: integer;
  153. lPt1, lPt2: TPDFCoord;
  154. begin
  155. P:=D.Pages[APage];
  156. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  157. FtTitle := D.AddFont('Helvetica', clBlack);
  158. { Page title }
  159. P.SetFont(FtTitle,23);
  160. P.SetColor(clBlack, False);
  161. P.WriteText(25, 20, 'Sample Line Drawing (DrawLine)');
  162. P.SetColor(clBlack, True);
  163. P.SetPenStyle(ppsSolid);
  164. lPt1.X := 30; lPt1.Y := 100;
  165. lPt2.X := 150; lPt2.Y := 150;
  166. P.DrawLine(lPt1, lPt2, 0.2);
  167. P.SetColor(clBlue, True);
  168. P.SetPenStyle(ppsDash);
  169. lPt1.X := 50; lPt1.Y := 70;
  170. lPt2.X := 180; lPt2.Y := 100;
  171. P.DrawLine(lPt1, lPt2, 0.1);
  172. { we can also use coordinates directly, without TPDFCoord variables }
  173. P.SetColor(clRed, True);
  174. P.SetPenStyle(ppsDashDot);
  175. P.DrawLine(40, 140, 160, 80, 1);
  176. P.SetColor(clBlack, True);
  177. P.SetPenStyle(ppsDashDotDot);
  178. P.DrawLine(60, 50, 60, 120, 1.5);
  179. P.SetColor(clBlack, True);
  180. P.SetPenStyle(ppsDot);
  181. P.DrawLine(10, 80, 130, 130, 0.5);
  182. end;
  183. procedure TPDFTestApp.SimpleLines(D: TPDFDocument; APage: integer);
  184. var
  185. P: TPDFPage;
  186. FtTitle: integer;
  187. TsThinBlack, TsThinBlue, TsThick, TsThinRed, TsThinBlackDot: Integer;
  188. lPt1, lPt2: TPDFCoord;
  189. begin
  190. P:=D.Pages[APage];
  191. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  192. FtTitle := D.AddFont('Helvetica', clRed);
  193. { Page title }
  194. P.SetFont(FtTitle,23);
  195. P.SetColor(clBlack, false);
  196. P.WriteText(25, 20, 'Sample Line Drawing (DrawLineStyle)');
  197. // write the text at position 100 mm from left and 120 mm from top
  198. TsThinBlack := D.AddLineStyleDef(0.2, clBlack, ppsSolid);
  199. TsThinBlue := D.AddLineStyleDef(0.1, clBlue, ppsDash);
  200. TsThinRed := D.AddLineStyleDef(1, clRed, ppsDashDot);
  201. TsThick := D.AddLineStyleDef(1.5, clBlack, ppsDashDotDot);
  202. TsThinBlackDot := D.AddLineStyleDef(0.5, clBlack, ppsDot);
  203. lPt1.X := 30; lPt1.Y := 100;
  204. lPt2.X := 150; lPt2.Y := 150;
  205. P.DrawLineStyle(lPt1, lPt2, tsThinBlack);
  206. lPt1.X := 50; lPt1.Y := 70;
  207. lPt2.X := 180; lPt2.Y := 100;
  208. P.DrawLineStyle(lPt1, lPt2, tsThinBlue);
  209. { we can also use coordinates directly, without TPDFCoord variables }
  210. P.DrawLineStyle(40, 140, 160, 80, tsThinRed);
  211. P.DrawLineStyle(60, 50, 60, 120, tsThick);
  212. P.DrawLineStyle(10, 80, 130, 130, tsThinBlackDot);
  213. end;
  214. procedure TPDFTestApp.SimpleImage(D: TPDFDocument; APage: integer);
  215. Var
  216. P: TPDFPage;
  217. FtTitle: integer;
  218. IDX: Integer;
  219. W, H: Integer;
  220. begin
  221. P := D.Pages[APage];
  222. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  223. FtTitle := D.AddFont('Helvetica', clBlack);
  224. { Page title }
  225. P.SetFont(FtTitle,23);
  226. P.SetColor(clBlack, false);
  227. P.WriteText(25, 20, 'Sample Image Support');
  228. P.SetFont(FtTitle,10);
  229. P.SetColor(clBlack, false);
  230. IDX := D.Images.AddFromFile('poppy.jpg',False);
  231. W := D.Images[IDX].Width;
  232. H := D.Images[IDX].Height;
  233. { full size image }
  234. P.DrawImageRawSize(25, 130, W, H, IDX); // left-bottom coordinate of image
  235. P.WriteText(145, 90, '[Full size (defined in pixels)]');
  236. { half size image }
  237. P.DrawImageRawSize(25, 190, W shr 1, H shr 1, IDX); // could also have used: Integer(W div 2), Integer(H div 2)
  238. P.WriteText(90, 165, '[Quarter size (defined in pixels)]');
  239. { scalled image to 2x2 centimeters }
  240. P.DrawImage(25, 230, 20.0, 20.0, IDX); // left-bottom coordinate of image
  241. P.WriteText(50, 220, '[2x2 cm scaled image]');
  242. end;
  243. procedure TPDFTestApp.SimpleShapes(D: TPDFDocument; APage: integer);
  244. var
  245. P: TPDFPage;
  246. FtTitle: integer;
  247. lPt1: TPDFCoord;
  248. begin
  249. P:=D.Pages[APage];
  250. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  251. FtTitle := D.AddFont('Helvetica', clBlack);
  252. { Page title }
  253. P.SetFont(FtTitle,23);
  254. P.SetColor(clBlack);
  255. P.WriteText(25, 20, 'Basic Shapes');
  256. // ========== Rectangles ============
  257. { PDF origin coordinate is Bottom-Left, and we want to use Image Coordinate of Top-Left }
  258. lPt1.X := 30;
  259. lPt1.Y := 60+20; // origin + height
  260. P.SetColor(clRed, true);
  261. P.SetColor($37b344, false); // some green color
  262. P.DrawRect(lPt1.X, lPt1.Y, 40, 20, 3, true, true);
  263. lPt1.X := 20;
  264. lPt1.Y := 50+20; // origin + height
  265. P.SetColor(clBlue, true);
  266. P.SetColor($b737b3, false); // some purple color
  267. P.DrawRect(lPt1.X, lPt1.Y, 40, 20, 1, true, true);
  268. P.SetPenStyle(ppsDashDot);
  269. P.SetColor(clBlue, true);
  270. P.DrawRect(110, 70+20 {origin+height}, 40, 20, 1, false, true);
  271. P.SetPenStyle(ppsDash);
  272. P.SetColor($37b344, true); // some green color
  273. P.DrawRect(100, 60+20 {origin+height}, 40, 20, 2, false, true);
  274. P.SetPenStyle(ppsSolid);
  275. P.SetColor($b737b3, true); // some purple color
  276. P.DrawRect(90, 50+20 {origin+height}, 40, 20, 4, false, true);
  277. // ========== Ellipses ============
  278. P.SetPenStyle(ppsSolid);
  279. P.SetColor($c00000, True);
  280. P.DrawEllipse(60, 150, -40, 20, 3, False, True);
  281. lPt1.X := 60;
  282. lPt1.Y := 150;
  283. P.SetColor(clBlue, true);
  284. P.SetColor($b737b3, false); // some purple color
  285. P.DrawEllipse(lPt1, 10, 10, 1, True, True);
  286. P.SetPenStyle(ppsDashDot);
  287. P.SetColor($b737b3, True);
  288. P.DrawEllipse(140, 150, 35, 20, 1, False, True);
  289. // ========== Lines Pen Styles ============
  290. P.SetPenStyle(ppsSolid);
  291. P.SetColor(clBlack, True);
  292. P.DrawLine(30, 200, 70, 200, 1);
  293. P.SetPenStyle(ppsDash);
  294. P.SetColor(clBlack, True);
  295. P.DrawLine(30, 210, 70, 210, 1);
  296. P.SetPenStyle(ppsDot);
  297. P.SetColor(clBlack, True);
  298. P.DrawLine(30, 220, 70, 220, 1);
  299. P.SetPenStyle(ppsDashDot);
  300. P.SetColor(clBlack, True);
  301. P.DrawLine(30, 230, 70, 230, 1);
  302. P.SetPenStyle(ppsDashDotDot);
  303. P.SetColor(clBlack, True);
  304. P.DrawLine(30, 240, 70, 240, 1);
  305. // ========== Line Attribute ============
  306. P.SetPenStyle(ppsSolid);
  307. P.SetColor(clBlack, True);
  308. P.DrawLine(100, 170, 140, 170, 0.2);
  309. P.DrawLine(100, 180, 140, 180, 0.3);
  310. P.DrawLine(100, 190, 140, 190, 0.5);
  311. P.DrawLine(100, 200, 140, 200, 1);
  312. P.SetColor(clRed, True);
  313. P.DrawLine(100, 210, 140, 210, 2);
  314. P.SetColor($37b344, True);
  315. P.DrawLine(100, 220, 140, 220, 3);
  316. P.SetColor(clBlue, True);
  317. P.DrawLine(100, 230, 140, 230, 4);
  318. P.SetColor($b737b3, True);
  319. P.DrawLine(100, 240, 140, 240, 5);
  320. end;
  321. procedure TPDFTestApp.SampleMatrixTransform(D: TPDFDocument; APage: integer);
  322. var
  323. P: TPDFPage;
  324. FtTitle: integer;
  325. procedure OutputSample;
  326. var
  327. b: boolean;
  328. begin
  329. b := P.Matrix._11 = -1;
  330. P.SetFont(FtTitle, 10);
  331. P.WriteText(10, 10, 'Matrix transform: ' + BoolToStr(b, True));
  332. P.DrawLine(0, 0, 100, 100, 1);
  333. P.WriteText(100, 100, '(line end point)');
  334. end;
  335. begin
  336. P:=D.Pages[APage];
  337. // create the fonts to be used (use one of the 14 Adobe PDF standard fonts)
  338. FtTitle := D.AddFont('Helvetica', clBlack);
  339. { Page title }
  340. P.SetFont(FtTitle,23);
  341. P.SetColor(clBlack);
  342. P.WriteText(75, 20, 'Matrix Transform');
  343. OutputSample;
  344. // enables Cartesian coordinate system for the page
  345. P.Matrix.SetYScalation(1);
  346. P.Matrix.SetYTranslation(0);
  347. OutputSample;
  348. end;
  349. { TPDFTestApp }
  350. procedure TPDFTestApp.DoRun;
  351. Function BoolFlag(C : Char;ADefault : Boolean) : Boolean;
  352. Var
  353. V : Integer;
  354. begin
  355. Result:=ADefault;
  356. if HasOption(C, '') then
  357. begin
  358. v := StrToIntDef(GetOptionValue(C,''),-1);
  359. if Not (V in [0,1]) then
  360. Raise Exception.Create('Error in -'+C+' parameter. Valid range is 0-1.');
  361. Result:=(v=1);
  362. end
  363. end;
  364. var
  365. ErrorMsg: String;
  366. begin
  367. StopOnException:=True;
  368. inherited DoRun;
  369. // quick check parameters
  370. ErrorMsg := CheckOptions('hp:f:t:i:j:', '');
  371. if ErrorMsg <> '' then
  372. begin
  373. WriteLn('ERROR: ' + ErrorMsg);
  374. Writeln('');
  375. Terminate;
  376. Exit;
  377. end;
  378. // parse parameters
  379. if HasOption('h', '') then
  380. begin
  381. WriteHelp;
  382. Terminate;
  383. Exit;
  384. end;
  385. Fpg := -1;
  386. if HasOption('p', '') then
  387. begin
  388. Fpg := StrToInt(GetOptionValue('p', ''));
  389. if (Fpg < 1) or (Fpg > 5) then
  390. begin
  391. Writeln('Error in -p parameter. Valid range is 1-5.');
  392. Writeln('');
  393. Terminate;
  394. Exit;
  395. end;
  396. end;
  397. FFontCompression := BoolFlag('f',true);
  398. FTextCompression := BoolFlag('t',False);
  399. FImageCompression := BoolFlag('i',False);
  400. FRawJPEG:=BoolFlag('j',False);
  401. FDoc := SetupDocument;
  402. try
  403. FDoc.FontDirectory := 'fonts';
  404. if Fpg = -1 then
  405. begin
  406. SimpleText(FDoc, 0);
  407. SimpleShapes(FDoc, 1);
  408. SimpleLines(FDoc, 2);
  409. SimpleLinesRaw(FDoc, 3);
  410. SimpleImage(FDoc, 4);
  411. SampleMatrixTransform(FDoc, 5);
  412. end
  413. else
  414. begin
  415. case Fpg of
  416. 1: SimpleText(FDoc, 0);
  417. 2: SimpleShapes(FDoc, 0);
  418. 3: SimpleLines(FDoc, 0);
  419. 4: SimpleLinesRaw(FDoc, 0);
  420. 5: SimpleImage(FDoc, 0);
  421. 6: SampleMatrixTransform(FDoc, 0);
  422. end;
  423. end;
  424. SaveDocument(FDoc);
  425. finally
  426. FDoc.Free;
  427. end;
  428. // stop program loop
  429. Terminate;
  430. end;
  431. procedure TPDFTestApp.WriteHelp;
  432. begin
  433. writeln('Usage:');
  434. writeln(' -h Show this help.');
  435. writeln(' -p <n> Generate only one page. Valid range is 1-5.' + LineEnding +
  436. ' If this option is not specified, then all 5 pages are' + LineEnding +
  437. ' generated.');
  438. writeln(' -f <0|1> Toggle embedded font compression. A value of 0' + LineEnding +
  439. ' disables compression. A value of 1 enables compression.');
  440. writeln(' -t <0|1> Toggle text compression. A value of 0' + LineEnding +
  441. ' disables compression. A value of 1 enables compression.');
  442. writeln(' -i <0|1> Toggle image compression. A value of 0' + LineEnding +
  443. ' disables compression. A value of 1 enables compression.');
  444. writeln(' -j <0|1> Toggle use of JPEG. A value of 0' + LineEnding +
  445. ' disables use of JPEG images. A value of 1 writes jpeg file as-is');
  446. writeln('');
  447. end;
  448. begin
  449. Application := TPDFTestApp.Create(nil);
  450. Application.Title := 'fpPDF Test Application';
  451. Application.Run;
  452. Application.Free;
  453. end.