summaryrefslogtreecommitdiff
path: root/src/reportengine/u_report.pas
blob: 50672245d29b4c7dcb605cbc78f438b6f98b244c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
{
  fpGUI  -  Free Pascal GUI Toolkit

  Copyright (C) 2006 - 2012 See the file AUTHORS.txt, included in this
  distribution, for details of the copyright.

  See the file COPYING.modifiedLGPL, included in this distribution,
  for details about redistributing fpGUI.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Description:
    This unit form part of the PDF Reporting Engine. It interfaces with
    the user program.

    The PDF Reporting Engine was originally written by
    Jean-Marc Levecque <jean-marc.levecque@jmlesite.fr>
}

unit U_Report;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  StrUtils,
  fpg_base,
  fpg_main,
  fpg_dialogs,
  fpg_utils,
  fpg_imgfmt_bmp,
  fpg_imgfmt_jpg,
  U_Command,
  U_Pdf;

type
  TPaperType   = (A4, Letter, Legal, Executive, Comm10, Monarch, DL, C5, B5);
  TOrient      = (oPortrait, oLandscape);
  TMeasureUnit = (msMM, msInch);
  TPreparation = (ppPrepare, ppVisualize, ppPdfFile);

  T_Report = class(TObject)
  private
    OldSeparator: char;
    FVersion: char;
    FPaper: TPaper;
    FPaperType: TPaperType;
    FOrientation: TOrient;
    FCurrentMargin: TDimensions;
    FMeasureUnit: TMeasureUnit;
    FPreparation: TPreparation;
    FVisualization: Boolean;
    FCanvas: TfpgCanvas;
    FCurrentFont: integer;
    FCurrentLineSpace: integer;
    FCurrentColor: integer;
    FNmSection: integer;
    FNmPage: integer;
    FNmPageSect: integer;
    FPosRef: TRefPos;            // absolute writting position
    FHeaderHeight: single;       // end of text vertical position in the header
    FPageHeight: single;         // end of text vertical position in the page
    FFooterHeight: single;       // beginning of text vertical position in the footer
    FGroup: Boolean;
    FDefaultFile: string;
    function Dim2Pixels(Value: single): single;
    function Pixels2Dim(Value: single): single;
    function AddLineBreaks(const Txt: TfpgString; AMaxLineWidth: integer; AFnt: TfpgFont): string;
    function TxtHeight(AWid: integer; const ATxt: TfpgString; AFnt: TfpgFont; ALSpace: integer = 2): integer;
    function Convert2Alpha(Valeur: integer): string;
    function GetPaperHeight: integer;
    function GetPaperWidth: integer;
    procedure Bv_VisuPaint(Sender: TObject);
    procedure PrepareFormat;
    procedure CreateVisu;
    procedure PrintPage(PageNumero: integer);
    procedure ShiftFooterLines(Shift: single);
    procedure ShiftPageLines(Shift: single);
    procedure ShiftGroup(Shift: single);
    function WriteText(PosX, PosY: single; Column, Text, FontNum, BkColorNum, BordNum, SpLine: integer; TxtFlags: TfpgTextFlags; Zone: TZone): single;
    function WriteNumber(PosX, PosY: single; Column, TextNum, TextTot, FontNum, BkColorNum, BordNum, SpLine: integer; TxtFlags: TfpgTextFlags; Total, Alpha: Boolean; Zone: TZone; SPNum: TSectPageNum): single;
    function InsertSpace(PosY: single; Column: integer; SpaceHeight: single; BkColorNum: integer; Zone: TZone): single;
    procedure LineEnd(Zone: TZone);
    procedure DrawAFrame(StyLine, XLeft, XRight, YTop,YBottom: integer; Zone: TZone);
    procedure DrawALine(XBegin, YBegin, XEnd, YEnd: single; StyLine: integer);
    procedure DrawAHorizLine(XBegin, YBegin: single; Column: integer; XEnd: single; StyLine: integer; Zone: TZone);
    procedure PaintSurface(Points: T_Points; Couleur: TfpgColor);
    procedure PaintImage(PosX, PosY: single; Column, ImgNum: integer; Zone: TZone);
    function GetSectionTitle: string;
    procedure SetSectionTitle(ATitle: string);
  public
    constructor Create;
    destructor Destroy; override;
    procedure BeginWrite(IniOrientation: TOrient = oPortrait; IniPaperType: TPaperType = A4; IniMeasure: TMeasureUnit = msMM; IniVersion: char = 'F'; IniVisu: Boolean = True);
    // starts preview and printing process with initializations
    // IniOrientation = paper orientation >> oPortrait or oLandscape
    // IniPaperType = (A4, Letter,Legal,Executive,Comm10,Monarch,DL,C5,B5)
    // IniMeasure = millimeters (msMM) or inches (msInches)
    // IniVersion = version française 'F' or version English 'E', or other, to come
    // IniVisu = True (Preview) or False (direct printing or PDF generation)
    procedure EndWrite;
    procedure WriteDocument;
    procedure PagePreview;
    procedure Section(MgLeft, MgRight, MgTop, MgBottom: single; BackPos: single = 0; IniOrientation: TOrient = oPortrait);
    // new section with initialization of margins
    // BackPos = additional margin which can be necessary when frames are drawn
    // IniOrientation = paper orientation >> oPortrait or oLandscape
    procedure Page;
    // new page in the current section
    function BackColor(FdColor: TfpgColor): integer;
    // returns the number allocated to the color
    // FdColor = background color
    function Font(FtNom: string; FtColor: TfpgColor): integer;
    // returns the number allocated to the font
    // FtNom = FontDesc of the font
    // FtColor = font color
    function LineStyle(StThick: single; StColor: TfpgColor; StStyle: TfpgLineStyle): integer;
    // returns the number allocated to the line style
    // StThick = thickness of the line in pixels
    // StColor = line color
    // StStyle = line style
    function Border(BdFlags: TBorderFlags; BdStyle: integer): integer;
    // returns the number allocated to the border
    // BdFlags = position of the border (bdTop,bdBottom,bdLeft,bdRight)
    // BdStyle = border line style: thickness, color, style
    function Column(ClnPos, ClnWidth: single; ClnMargin: single = 0; ClnColor: TfpgColor = clWhite): integer;
    // returns the number allocated to the column
    // ClnPos = left position in numeric value in the measurement unit (msMM or msInch)
    // ClnWidth = width in numeric value in the measurement unit (msMM or msInch)
    // ClnMargin = left and right margins in numeric value in the measurement unit (msMM or msInch)
    // ClnColor = column background color
    procedure WriteHeader(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Text = text to be written
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    function WritePage(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1): single;
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Text = text to be written
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure WriteFooter(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Text = text to be written
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumSectionHeader(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
      LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TexteSection = text to be written before the section number
    // TextTot = text to be written before the number of sections
    // Total= True => displays the number of sections
    // Alpha= True => displays the number of sections using letters in alphabetic order
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumSectionFooter(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
      LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TexteSection = text to be written before the section number
    // TextTot = text to be written before the number of sections
    // Total= True => displays the number of sections
    // Alpha= True => displays the number of sections using letters in alphabetic order
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumPageHeader(Horiz, Verti: single; TextePage: string = ''; TextTot: string = ''; Total: Boolean = False; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TextePage = text to be written before the page number in the document
    // TextTot = text to be written before the number of pages of the document
    // Total= True > displays the number of pages of the document
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumPageFooter(Horiz, Verti: single; TextePage: string = ''; TextTot: string = ''; Total: Boolean = False; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TextePage = text to be written before the page number in the document
    // TextTot = text to be written before the number of pages of the document
    // Total= True > displays the number of pages of the document
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumPageSectionHeader(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
      LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TextePage = text to ba written before the page number in the section
    // TextTot = text to be written before the number of pages of the section
    // Total= True > displays the number of pages of the section
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure NumPageSectionFooter(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
      LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
    // Horiz = horizontal position in column (cnLeft,cnCenter,cnRight)
    //         or numeric value in the measurement unit (msMM or msInch)
    // Verti = line position in column (lnCourante,lnFin)
    //         or numeric value in the measurement unit (msMM or msInch)
    // TextePage = text to ba written before the page number in the section
    // TextTot = text to be written before the number of pages of the section
    // Total= True > displays the number of pages of the section
    // ColNum = column reference, default between left and right margins
    // FontNum = font reference
    // LineSpNum = space between lines reference
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    // BordNum = border reference, if > -1
    procedure HorizLineHeader(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
    // SpBefore = empty space before the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // SpAfter =  empty space after the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // StyleNum = reference of the line style
    procedure HorizLinePage(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
    // SpBefore = empty space before the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // SpAfter =  empty space after the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // StyleNum = reference of the line style
    procedure HorizLineFooter(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
    // SpBefore = empty space before the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // SpAfter =  empty space after the horizontal line : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // StyleNum = reference of the line style
    procedure SpaceHeader(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
    // Verti = height of the empty space : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    procedure SpacePage(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
    // Verti = height of the empty space : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    procedure SpaceFooter(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
    // Verti = height of the empty space : numeric value in the measurement unit (msMM or msInch)
    // ColNum = column reference, default between left and right margins
    // BkColorNum = background color reference, if > -1, replaces the column background color if any
    function LineSpace(SpSup, SpInt, SpInf: single): integer;
    // SpSup = space between lines, top : numeric value in the measurement unit (msMM or msInch)
    // SpInt = space between lines, internal if wrapping : numeric value in the measurement unit (msMM or msInch)
    // SpInf = space between lines, botom : numeric value in the measurement unit (msMM or msInch)
    procedure BeginGroup(PageJump: Boolean = False);
    // PageJump = True >> forces new page before the group
    //          = False >> does not create a new page if the whole group can stand on the same page as the preceding text
    procedure EndGroup(PageJump: Boolean = False);
    // PageJump = True >> forces new page after the group
    //          = False >> lets continue on the same page after the group
    procedure ColorColChange(ColNum: integer; ColColor: TfpgColor);
    // Changes the background color of a column
    // ColNum = column reference
    // ColColor = new background color for the column
    procedure FrameMargins(AStyle: integer);
    // draw a frame at the page margins
    // AStyle = reference of the line style of the frame
    procedure FrameHeader(AStyle: integer);
    // draw a frame at the limits of the header
    // AStyle = reference of the line style of the frame
    procedure FramePage(AStyle: integer);
    // draw a frame at the page limits : left and right margins, header bottom and footer top
    // AStyle = reference of the line style of the frame
    procedure FrameFooter(AStyle: integer);
    // draw a frame at the limits of the footer
    // AStyle = reference of the line style of the frame
    procedure LinePage(XBegin, YBegin, XEnd, YEnd: single; AStyle: integer);
    // draw a line at absolute position
    // XBegin = horizontal position of starting point in numeric value in the measurement unit (msMM or msInch)
    // YBegin = vertical position of starting point in numeric value in the measurement unit (msMM or msInch)
    // XEnd = horizontal position of ending point in numeric value in the measurement unit (msMM or msInch)
    // YEnd = vertical position of ending point in numeric value in the measurement unit (msMM or msInch)
    // AStyle = reference of the line style of the line
    procedure SurfPage(XLimits, YLimits: array of single; AColor: TfpgColor);
    // draw a coloured surface inside the defined limit points
    // XLimits = list of horizontal positions of limit points
    // YLimits = list of vertical positions of limit points
    // AColor = colour to be painted within the limits
    procedure ImageHeader(Horiz, Verti: single; ImgFileName: string; ColNum: integer = 0; Scale: integer = 1);
    // draw a bmp or jpg image at the defined position
    // Horiz = horizontal position in numeric value in the measurement unit (msMM or msInch)
    // Verti = vertical position in numeric value in the measurement unit (msMM or msInch)
    // ImgFileName = name of the image file
    // ColNum = column reference, default between left and right margins
    // Scale =  1 for full size
    //          2 for 1/2 size
    //          3 for 1/3 size
    //          4 for 1/4 size
    procedure ImagePage(Horiz, Verti: single; ImgFileName: string; ColNum: integer = 0; Scale: integer = 1);
    // draw a bmp or jpg image at the defined position
    // Horiz = horizontal position in numeric value in the measurement unit (msMM or msInch)
    // Verti = vertical position in numeric value in the measurement unit (msMM or msInch)
    // ImgFileName = name of the image file
    // ColNum = column reference, default between left and right margins
    // Scale =  1 for full size
    //          2 for 1/2 size
    //          3 for 1/3 size
    //          4 for 1/4 size
    procedure ImageFooter(Horiz, Verti: single; ImgFileName: string; ColNum: integer = 0; Scale: integer = 1);
    // draw a bmp or jpg image at the defined position
    // Horiz = horizontal position in numeric value in the measurement unit (msMM or msInch)
    // Verti = vertical position in numeric value in the measurement unit (msMM or msInch)
    // ImgFileName = name of the image file
    // ColNum = column reference, default between left and right margins
    // Scale =  1 for full size
    //          2 for 1/2 size
    //          3 for 1/3 size
    //          4 for 1/4 size
    procedure PrintPdf(Layout: TPageLayout = lSingle; Zoom: string = '100'; Prefer: Boolean = True);
    property Language: char read FVersion write FVersion;
    property Visualiser: Boolean read FVisualization write FVisualization;
    property NumSection: integer read FNmSection write FNmSection;
    property NumPage: integer read FNmPage write FNmPage;
    property NumPageSection: integer read FNmPageSect write FNmPageSect;
    property PaperHeight: integer read GetPaperHeight;
    property PagerWidth: integer read GetPaperWidth;
    property DefaultFile: string read FDefaultFile write FDefaultFile;
    property CurrentColor: integer read FCurrentColor write FCurrentColor;
    property SectionTitle: string read GetSectionTitle write SetSectionTitle;
  end;

  // classes for interface with PDF generation

  TPdfElement = class
  end;

  TPdfTexte = class(TPdfElement)
  private
    FPage: integer;
    FFont: integer;
    FSize: string;
    FPosX: single;
    FPosY: single;
    FWidth: single;
    FText: string;
    FColor: TfpgColor;
  public
    property PageId: integer read FPage write FPage;
    property FontName: integer read FFont write FFont;
    property FontSize: string read FSize write FSize;
    property TextPosX: single read FPosX write FPosX;
    property TextPosY: single read FPosY write FPosY;
    property TextWidt: single read FWidth write FWidth;
    property Writting: string read FText write FText;
    property Couleur: TfpgColor read FColor write FColor;
  end;

  TPdfRect = class(TPdfElement)
  private
    FPage: integer;
    FThick: single;
    FLeft: single;
    FBottom: single;
    FHeight: single;
    FWidth: single;
    FColor: TfpgColor;
    FFill: Boolean;
    FStroke: Boolean;
    FLineStyle: TfpgLineStyle;
  protected
  public
    property PageId: integer read FPage write FPage;
    property RectThickness: single read FThick write FThick;
    property RectLeft: single read FLeft write FLeft;
    property RectBottom: single read FBottom write FBottom;
    property RectHeight: single read FHeight write FHeight;
    property RectWidth: single read FWidth write FWidth;
    property RectColor: TfpgColor read FColor write FColor;
    property RectFill: Boolean read FFill write FFill;
    property RectStroke: Boolean read FStroke write FStroke;
    property RectLineStyle: TfpgLineStyle read FLineStyle write FLineStyle;
  end;

  TPdfLine = class(TPdfElement)
  private
    FPage: integer;
    FThick: single;
    FBeginX: single;
    FBeginY: single;
    FEndX: single;
    FEndY: single;
    FColor: TfpgColor;
    FStyle: TfpgLineStyle;
  protected
  public
    property PageId: integer read FPage write FPage;
    property LineThikness: single read FThick write FThick;
    property LineBeginX: single read FBeginX write FBeginX;
    property LineBeginY: single read FBeginY write FBeginY;
    property LineEndX: single read FEndX write FEndX;
    property LineEndY: single read FEndY write FEndY;
    property LineColor: TfpgColor read FColor write FColor;
    property LineStyle: TfpgLineStyle read FStyle write FStyle;
  end;

  TPdfSurf = class(TPdfElement)
  private
    FPage: integer;
    FPoints: T_Points;
    FColor: TfpgColor;
  protected
  public
    property PageId: integer read FPage write FPage;
    property Points: T_Points read FPoints;
    property SurfColor: TfpgColor read FColor write FColor;
  end;

  TPdfImg = class(TPdfElement)
  private
    FPage: integer;
    FNumber: integer;
    FLeft: single;
    FBottom: single;
    FWidth: integer;
    FHeight: integer;
  protected
  public
    property PageId: integer read FPage write FPage;
    property ImgNumber: integer read FNumber write FNumber;
    property ImgLeft: single read FLeft write FLeft;
    property ImgBottom: single read FBottom write FBottom;
    property ImgWidth: integer read FWidth write FWidth;
    property ImgHeight: integer read FHeight write FHeight;
  end;

var
  Infos: record
    Titre: string;
    Auteur: string;
  end;

  PdfPage: TList;
  PdfTexte: TPdfTexte;
  PdfRect: TPdfRect;
  PdfLine: TPdfLine;
  PdfSurf: TPdfSurf;
  PdfImg: TPdfImg;

const
  PPI        = 72;
  FontDefaut = 0;
  ColDefaut  = -2;
  lnCurrent  = -1;
  lnEnd      = -2;
  //  cnSuite= -1;
  cnLeft     = -2;
  cnCenter   = -3;
  cnRight    = -4;

implementation

uses
  fpg_constants,
  U_Visu;

const
  InchToMM = 25.4;

function T_Report.Dim2Pixels(Value: single): single;
begin
  if FMeasureUnit = msMM then
    Result := Value * PPI / InchToMM
  else
    Result := Value * PPI;
end;

function T_Report.Pixels2Dim(Value: single): single;
begin
  if FMeasureUnit = msMM then
    Result := Value * InchToMM / PPI
  else
    Result := Value / PPI;
end;

function T_Report.AddLineBreaks(const Txt: TfpgString; AMaxLineWidth: integer; AFnt: TfpgFont): string;
var
  i, n, ls: integer;
  sub: string;
  lw, tw: integer;
begin
  Result := '';
  ls     := Length(Txt);
  lw     := 0;
  i      := 1;
  while i <= ls do
  begin
    if (Txt[i] in txtWordDelims) then       // read the delimeter only
    begin
      sub := Txt[i];
      Inc(i);
    end
    else                                // read the whole word
    begin
      n := PosSetEx(txtWordDelims, Txt, i);
      if n > 0 then
      begin
        sub := Copy(Txt, i, n - i);
        i   := n;
      end
      else
      begin
        sub := Copy(Txt, i, MaxInt);
        i   := ls + 1;
      end;
    end;
    tw := AFnt.TextWidth(sub);            // wrap if needed
    if (lw + tw > aMaxLineWidth) and (lw > 0) then
    begin
      lw     := tw;
      Result := TrimRight(Result) + sLineBreak;
    end
    else
      Inc(lw, tw);
    Result := Result + sub;
  end;
end;

function T_Report.TxtHeight(AWid: integer; const ATxt: TfpgString; AFnt: TfpgFont; ALSpace: integer = 2): integer;
var
  Cpt: integer;
  Wraplst: TStringList;
begin
  Wraplst      := TStringList.Create;
  Wraplst.Text := ATxt;
  for Cpt := 0 to Pred(Wraplst.Count) do
    Wraplst[Cpt] := AddLineBreaks(Wraplst[Cpt], AWid, AFnt);
  Wraplst.Text := Wraplst.Text;
  Result := (AFnt.Height * Wraplst.Count) + (ALSpace * Pred(Wraplst.Count));
  WrapLst.Free;
end;

function T_Report.Convert2Alpha(Valeur: integer): string;
var
  Cpt: byte;
begin
  Result := '';
  Cpt    := 0;
  repeat
    if Valeur > 26 then
    begin
      Valeur := Valeur - 26;
      Inc(Cpt);
      Result := Chr(Cpt + 64);
    end
    else
    begin
      Result := Chr(Valeur + 64);
      Valeur := 0;
    end;
  until Valeur < 1;
end;

function T_Report.GetPaperHeight: integer;
begin
  Result := FPaper.H;
end;

function T_Report.GetPaperWidth: integer;
begin
  Result := FPaper.W;
end;

procedure T_Report.Bv_VisuPaint(Sender: TObject);
begin
  PrintPage(NumPage);
end;

procedure T_Report.PrepareFormat;
var
  TempH, TempW: integer;
  TempT, TempL, TempR, TempB: single;
begin
  with FPaper do
  begin
    case FPaperType of
      A4:
      begin
        H := 842;
        W := 595;
        with Printable do
        begin
          T := 10;
          L := 11;
          R := 586;
          B := 822;
        end;
      end;
      Letter:
      begin
        H := 792;
        W := 612;
        with Printable do
        begin
          T := 13;
          L := 13;
          R := 599;
          B := 780;
        end;
      end;
      Legal:
      begin
        H := 1008;
        W := 612;
        with Printable do
        begin
          T := 13;
          L := 13;
          R := 599;
          B := 996;
        end;
      end;
      Executive:
      begin
        H := 756;
        W := 522;
        with Printable do
        begin
          T := 14;
          L := 13;
          R := 508;
          B := 744;
        end;
      end;
      Comm10:
      begin
        H := 684;
        W := 297;
        with Printable do
        begin
          T := 13;
          L := 13;
          R := 284;
          B := 672;
        end;
      end;
      Monarch:
      begin
        H := 540;
        W := 279;
        with Printable do
        begin
          T := 13;
          L := 13;
          R := 266;
          B := 528;
        end;
      end;
      DL:
      begin
        H := 624;
        W := 312;
        with Printable do
        begin
          T := 14;
          L := 13;
          R := 297;
          B := 611;
        end;
      end;
      C5:
      begin
        H := 649;
        W := 459;
        with Printable do
        begin
          T := 13;
          L := 13;
          R := 446;
          B := 637;
        end;
      end;
      B5:
      begin
        H := 708;
        W := 499;
        with Printable do
        begin
          T := 14;
          L := 13;
          R := 485;
          B := 696;
        end;
      end;
    end;
    if FOrientation = oLandscape then
    begin
      TempH := H;
      TempW := W;
      H     := TempW;
      W     := TempH;
      with Printable do
      begin
        TempT := T;
        TempL := L;
        TempR := R;
        TempB := B;
        T     := TempL;
        L     := TempT;
        R     := TempB;
        B     := TempR;
      end;
    end;
  end;
end;

procedure T_Report.CreateVisu;
begin
  F_Visu := TF_Visu.Create(nil, self);
  with F_Visu do
  begin
    Bv_Visu.SetPosition((F_Visu.Width - FPaper.W - F_Visu.PreviewMargin) div 2,
        ((F_Visu.Height + 50 + F_Visu.PreviewMargin - FPaper.H) div 2), FPaper.W, FPaper.H);
    Bv_Visu.OnPaint := @Bv_VisuPaint;
    RecalcScrollbars;
  end;
end;

procedure T_Report.PrintPage(PageNumero: integer);
var
  CptSect, CptPage, CptCmd: integer;
  ThePage: T_Page;
  Cmd: T_Command;
begin
  CptSect := 0;
  repeat
    Inc(CptSect);
    CptPage := 0;
    with T_Section(Sections[Pred(CptSect)]) do
      repeat
        Inc(CptPage);
        ThePage := T_Page(Pages.Items[Pred(CptPage)]);
      until (ThePage.PagesTot = PageNumero) or (CptPage = Pages.Count);
  until (ThePage.PagesTot = PageNumero) or (CptSect = Sections.Count);
  NumPage        := PageNumero;
  NumSection     := CptSect;
  NumPageSection := ThePage.PagesSect;
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    if CmdHeader.Count > 0 then
      for CptCmd := 0 to Pred(CmdHeader.Count) do
      begin
        Cmd := T_Command(CmdHeader.Items[CptCmd]);
        if Cmd is T_WriteText then
          with Cmd as T_WriteText do
            WriteText(GetPosX, GetPosY, GetColumn, GetText, GetFont, GetBackColor, GetBorder, GetLineSpace, GetFlags, ZHeader);
        if Cmd is T_Number then
          with Cmd as T_Number do
            WriteNumber(GetPosX, GetPosY, GetColumn, GetTextNum, GetTextTot, GetFont, GetBackColor, GetBorder, GetLineSpace,
              GetFlags, GetTotal, GetAlpha, zHeader, GetTypeNum);
        if Cmd is T_Space then
          with Cmd as T_Space do
            InsertSpace(GetPosY, GetColumn, GetHeight, GetBackColor, zHeader);
        if Cmd is T_Line then
          with Cmd as T_Line do
            DrawALine(GetPosX, GetPosY, GetEndX, GetEndY, GetStyle);
        if Cmd is T_Image then
          with Cmd as T_Image do
            PaintImage(GetPosX, GetPosY, GetColumn, GetImage, zHeader);
      end;
    if GetCmdPage(NumPageSection).Count > 0 then
      for CptCmd := 0 to Pred(GetCmdPage(NumPageSection).Count) do
      begin
        Cmd := T_Command(GetCmdPage(NumPageSection).Items[CptCmd]);
        if Cmd is T_WriteText then
          with Cmd as T_WriteText do
            WriteText(GetPosX, GetPosY, GetColumn, GetText, GetFont, GetBackColor, GetBorder, GetLineSpace, GetFlags, ZPage);
        if Cmd is T_Space then
          with Cmd as T_Space do
            InsertSpace(GetPosY, GetColumn, GetHeight, GetBackColor, zPage);
        if Cmd is T_Line then
          with Cmd as T_Line do
            DrawALine(GetPosX, GetPosY, GetEndX, GetEndY, GetStyle);
        if Cmd is T_Surface then
          with Cmd as T_Surface do
            PaintSurface(GetPoints, GetColor);
        if Cmd is T_Image then
          with Cmd as T_Image do
            PaintImage(GetPosX, GetPosY, GetColumn, GetImage, zPage);
      end;
    if CmdFooter.Count > 0 then
      for CptCmd := 0 to Pred(CmdFooter.Count) do
      begin
        Cmd := T_Command(CmdFooter.Items[CptCmd]);
        if Cmd is T_WriteText then
          with Cmd as T_WriteText do
            WriteText(GetPosX, GetPosY, GetColumn, GetText, GetFont, GetBackColor, GetBorder, GetLineSpace, GetFlags, ZFooter);
        if Cmd is T_Number then
          with Cmd as T_Number do
            WriteNumber(GetPosX, GetPosY, GetColumn, GetTextNum, GetTextTot, GetFont, GetBackColor, GetBorder, GetLineSpace,
              GetFlags, GetTotal, GetAlpha, zFooter, GetTypeNum);
        if Cmd is T_Space then
          with Cmd as T_Space do
            InsertSpace(GetPosY, GetColumn, GetHeight, GetBackColor, zFooter);
        if Cmd is T_Line then
          with Cmd as T_Line do
            DrawALine(GetPosX, GetPosY, GetEndX, GetEndY, GetStyle);
        if Cmd is T_Image then
          with Cmd as T_Image do
            PaintImage(GetPosX, GetPosY, GetColumn, GetImage, zFooter);
      end;
    if CmdFrames.Count > 0 then
      for CptCmd := 0 to Pred(CmdFrames.Count) do
      begin
        Cmd := T_Command(CmdFrames.Items[CptCmd]);
        if Cmd is T_Frame then
          with Cmd as T_Frame do
            DrawAFrame(GetStyle, GetLeft, GetRight, GetTop, GetBottom, GetZone);
      end;
  end;
end;

procedure T_Report.ShiftFooterLines(Shift: single);
var
  Cpt: integer;
  Cmd: T_Command;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    if CmdFooter.Count > 0 then
    begin
      for Cpt := 0 to Pred(CmdFooter.Count) do
      begin
        Cmd := T_Command(CmdFooter.Items[Cpt]);
        if Cmd is T_WriteText then
          with Cmd as T_WriteText do
            SetPosY(GetPosY - Shift);
        if Cmd is T_Number then
          with Cmd as T_Number do
            SetPosY(GetPosY - Shift);
        if Cmd is T_Space then
          with Cmd as T_Space do
            SetPosY(GetPosY - Shift);
      end;
    end;
  end;
end;

procedure T_Report.ShiftPageLines(Shift: single);
var
  Cpt: integer;
  Cmd: T_Command;
begin
  with VWriteLine do
  begin
    for Cpt := 0 to Pred(Commands.Count) do
    begin
      Cmd := T_Command(Commands.Items[Cpt]);
      if Cmd is T_WriteText then
        with Cmd as T_WriteText do
          SetPosY(GetPosY - Shift);
    end;
  end;
end;

procedure T_Report.ShiftGroup(Shift: single);
var
  Cpt: integer;
  Cmd: T_Command;
begin
  with VGroup do
  begin
    for Cpt := 0 to Pred(Commands.Count) do
    begin
      Cmd := T_Command(Commands.Items[Cpt]);
      if Cmd is T_WriteText then
        with Cmd as T_WriteText do
          SetPosY(GetPosY - Shift);
    end;
  end;
end;

function T_Report.WriteText(PosX, PosY: single; Column, Text, FontNum, BkColorNum, BordNum, SpLine: integer; TxtFlags: TfpgTextFlags; Zone: TZone): single;
var
  PosH, PosV, LnSpInt, LnSpSup, LnSpInf, ThickLine: single;
  HTxt, HeighTxt, Half, ColorLine, Cpt: integer;
  EndOfLine, UseCurFont: Boolean;
  Fnt: TfpgFont;
  StyleLine: TfpgLineStyle;
  Wraplst: TStringList;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    EndOfLine := False;
    if FPreparation = ppPrepare then
    begin
      if FCurrentFont <> FontNum then
      begin
        FCurrentFont := FontNum;
        UseCurFont   := False;
      end
      else
        UseCurFont   := True;
    end;
    Fnt := T_Font(Fonts[FontNum]).GetFont;
    if LineSpaces.Count = 0 then
      LineSpace(0, 0, 0);
    if FCurrentLineSpace <> SpLine then
      FCurrentLineSpace := SpLine;
    with T_LineSpace(LineSpaces[FCurrentLineSpace]) do
    begin
      LnSpSup := GetSup;
      LnSpInt := GetInt;
      LnSpInf := GetInf;
    end;
    if Column = -2 then
      Column   := DefaultCol;
    if Column > -1 then
      HeighTxt := TxtHeight(Round(T_Column(Columns[Column]).GetTextWidth), Texts[Text], Fnt, Round(LnSpInt)) + Round(LnSpSup + LnSpInf)
    else
      HeighTxt := TxtHeight(Paper.W, Texts[Text], Fnt, Round(LnSpInt)) + Round(LnSpSup + LnSpInf);
    if (Column > -1) and (BordNum > -1) then
      Half     := Round(T_LineStyle(LineStyles[T_Border(Borders[BordNum]).GetStyle]).GetThick) div 2
    else
      Half     := 0;
    case FPreparation of
      ppPrepare:
      begin
        if NbPages = 0 then
          Page;
        if Column > -1 then
        begin
          HTxt := VWriteLine.LineHeight;
          if HTxt < HeighTxt then
            HTxt := HeighTxt;
        end
        else if HTxt < Fnt.Height then
          HTxt := Fnt.Height;
        case Zone of
          zHeader:
            FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
          zPage:
            FPosRef.Y     := FCurrentMargin.T + FHeaderHeight + FPageHeight;
          zFooter:
          begin
            FPosRef.Y     := FCurrentMargin.B - HTxt;
            FFooterHeight := FFooterHeight + HTxt;
            ShiftFooterLines(HTxt);
          end;
        end;
        if PosY = lnCurrent then
          PosV      := FPosRef.Y + LnSpSup
        else
        begin
          EndOfLine := True;
          if PosY = lnEnd then
          begin
            PosV := FPosRef.Y + LnSpSup;
            case Zone of
              zHeader:
                FPosRef.Y := FPosRef.Y + HTxt;
              zPage:
                if FPosRef.Y + HTxt > FCurrentMargin.B - FFooterHeight then
                  if FGroup then
                  begin
                    if VGroup.GroupeHeight + HTxt < FCurrentMargin.B - FCurrentMargin.T - FHeaderHeight - FFooterHeight then
                    begin
                      Page;
                      if VGroup.Commands.Count > 0 then
                      begin
                        FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
                        ShiftGroup(T_WriteText(VGroup.Commands[0]).GetPosY - FPosRef.Y);
                        FPosRef.Y := FPosRef.Y + VGroup.GroupeHeight + Succ(Half);
                        if VWriteLine.Commands.Count > 0 then
                          ShiftPageLines(T_WriteText(VWriteLine.Commands[0]).GetPosY - FPosRef.Y);
                        PosV      := FPosRef.Y + LnSpSup;
                        FPosRef.Y := FPosRef.Y + HTxt + Succ(Half);
                      end
                      else
                      begin
                        if VWriteLine.Commands.Count > 0 then
                          ShiftPageLines(T_WriteText(VWriteLine.Commands[0]).GetPosY - FPosRef.Y);
                        PosV      := FPosRef.Y + LnSpSup;
                        FPosRef.Y := FPosRef.Y + HTxt + Succ(Half);
                      end;
                    end
                    else
                    begin
                      LoadCmdGroupToPage;
                      //                    VGroup.Commands.Clear;
                      Page;
                      FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
                      if VWriteLine.Commands.Count > 0 then
                        ShiftPageLines(T_WriteText(VWriteLine.Commands[0]).GetPosY - FPosRef.Y);
                      PosV      := FPosRef.Y + LnSpSup;
                      FPosRef.Y := FPosRef.Y + HTxt + Succ(Half);
                    end;
                  end
                  else
                  begin
                    Page;
                    FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
                    if VWriteLine.Commands.Count > 0 then
                      ShiftPageLines(T_WriteText(VWriteLine.Commands[0]).GetPosY - FPosRef.Y);
                    PosV      := FPosRef.Y + LnSpSup;
                    FPosRef.Y := FPosRef.Y + HTxt + Succ(Half);
                  end
                else
                  FPosRef.Y := FPosRef.Y + HTxt;
            end;
            if BordNum > -1 then
              with T_Border(Borders[BordNum]) do
                if bfBottom in GetFlags then
                  FPosRef.Y := FPosRef.Y + 1;
          end
          else
          begin
            PosV      := PosY;
            FPosRef.Y := PosV + LnSpInf;
          end;
          case Zone of
            zHeader:
              FHeaderHeight := FPosRef.Y - FCurrentMargin.T;
            zPage:
              FPageHeight   := FPosRef.Y - FHeaderHeight - FCurrentMargin.T;
          end;
        end;
        //if PosX= cnSuite
        //then
        //PosH:= FPosRef.X
        //else
        if Column = -1 then
          if PosX > 0 then
            PosH := PosX
          else
          begin
            PosH := T_Column(Columns[ColDefaut]).GetTextPos;
            if (txtRight in TxtFlags) then
              PosH := PosH + T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[Text]) - T_Column(Columns[ColDefaut]).ColMargin;
            if (txtHCenter in TxtFlags) then
              PosH := PosH + (T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[Text])) / 2;
          end
        else if PosX > 0 then
        begin
          if (PosX < T_Column(Columns[Column]).GetTextPos) or (PosX > (T_Column(Columns[Column]).GetTextPos + T_Column(Columns[Column]).GetTextWidth)) then
            PosH := T_Column(Columns[Column]).GetTextPos
          else
            PosH := PosX;
        end
        else
        begin
          PosH := T_Column(Columns[Column]).GetTextPos;
          if (txtRight in TxtFlags) then
            PosH := PosH + T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[Text]) - T_Column(Columns[Column]).ColMargin;
          if (txtHCenter in TxtFlags) then
            PosH := PosH + (T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[Text])) / 2;
        end;
        FPosRef.X := PosH + Fnt.TextWidth(Texts[Text] + ' ');
        VWriteLine.LoadText(PosH, PosV, Column, Text, FontNum, HTxt, BkColorNum, BordNum, SpLine, UseCurFont, TxtFlags);
        Result := Pixels2Dim(FPosRef.Y);
        if EndOfLine then
        begin
          HTxt := 0;
          LineEnd(Zone);
        end;
      end;
      ppVisualize:
      begin
        with FCanvas do
        begin
          Font := T_Font(Fonts[FontNum]).GetFont;
          SetTextColor(T_Font(Fonts[FontNum]).GetColor);
          if Column > -1 then
            with T_Column(Columns[Column]) do
            begin
              if BkColorNum > -1 then
                SetColor(T_BackColor(BackColors[BkColorNum]).GetColor)
              else
                SetColor(GetColor);
              FillRectangle(Round(ColPos), Round(PosY - LnSpSup), Round(ColWidth), HeighTxt);
              if BordNum > -1 then
                with T_Border(Borders[BordNum]) do
                begin
                  SetLineStyle(Round(T_LineStyle(LineStyles[GetStyle]).GetThick), T_LineStyle(LineStyles[GetStyle]).GetStyle);
                  SetColor(T_LineStyle(LineStyles[GetStyle]).GetColor);
                  if bfLeft in GetFlags then
                    DrawLine(Round(ColPos) + Half, Round(PosY - LnSpSup), Round(ColPos) + Half, Round(PosY - LnSpSup) + HeighTxt);
                  if bfRight in GetFlags then
                    DrawLine(Round(ColPos + ColWidth) - Succ(Half), Round(PosY - LnSpSup), Round(ColPos + ColWidth) - Succ(Half), Round(PosY - LnSpSup) + HeighTxt);
                  if bfTop in GetFlags then
                    DrawLine(Round(ColPos), Round(PosY - LnSpSup) + Half, Round(ColPos + ColWidth), Round(PosY - LnSpSup) + Half);
                  if bfBottom in GetFlags then
                    DrawLine(Round(ColPos), Round(PosY - LnSpSup) + HeighTxt - Half, Round(ColPos + ColWidth), Round(PosY - LnSpSup) + HeighTxt - Half);
                end;
              DrawText(Round(GetTextPos), Round(PosY), Round(GetTextWidth), 0, Texts[Text], TxtFlags, Round(LnSpInt));
            end
          else
            DrawText(Round(PosX), Round(PosY) - Fnt.Ascent, Round(Paper.W - PosX), 0, Texts[Text], TxtFlags);
        end;
      end;
      ppPdfFile:
      begin
        if Column > -1 then
          with T_Column(Columns[Column]) do
          begin
            if (GetColor <> clWhite) or (BkColorNum > -1) then
            begin
              PdfRect := TPdfRect.Create;
              with PdfRect do
              begin
                PageId  := NumPage;
                FLeft   := ColPos;
                FBottom := Paper.H - PosY + LnSpSup - HeighTxt;
                FHeight := HeighTxt;
                FWidth  := ColWidth;
                if BkColorNum > -1 then
                  FColor := T_BackColor(BackColors[BkColorNum]).GetColor
                else
                  FColor := GetColor;
                FFill := True;
                FStroke := False;
              end;
              PdfPage.Add(PdfRect);
            end;
            if BordNum > -1 then
              with T_Border(Borders[BordNum]) do
              begin
                StyleLine := T_LineStyle(LineStyles[GetStyle]).GetStyle;
                ColorLine := T_LineStyle(LineStyles[GetStyle]).GetColor;
                ThickLine := T_LineStyle(LineStyles[GetStyle]).GetThick;
                if bfLeft in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfRight in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos + ColWidth;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfTop in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfBottom in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup - HeighTxt;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
              end;
            if Fnt.TextWidth(Texts[Text]) < GetTextWidth then
            begin
              PdfTexte := TPdfTexte.Create;
              with PdfTexte do
              begin
                PageId   := NumPage;
                FFont    := FontNum;
                FSize    := T_Font(Fonts[FontNum]).GetSize;
                FColor   := T_Font(Fonts[FontNum]).GetColor;
                TextPosX := GetTextPos;
                if (txtRight in TxtFlags) then
                  TextPosX := ColPos + ColWidth - ColMargin - Fnt.TextWidth(Texts[Text]);
                if (txtHCenter in TxtFlags) then
                  TextPosX := GetTextPos + (ColWidth - Fnt.TextWidth(Texts[Text])) / 2;
                TextPosY := Paper.H - PosY - Fnt.Ascent;
                TextWidt := ColWidth;
                Writting := Texts[Text];
              end;
              PdfPage.Add(PdfTexte);
            end
            else
            begin
              Wraplst      := TStringList.Create;
              Wraplst.Text := Texts[Text];
              for Cpt := 0 to Pred(Wraplst.Count) do
                Wraplst[Cpt] := AddLineBreaks(Wraplst[Cpt], Round(GetTextWidth), Fnt);
              Wraplst.Text := Wraplst.Text;
              for Cpt        := 0 to Pred(Wraplst.Count) do
              begin
                PdfTexte     := TPdfTexte.Create;
                with PdfTexte do
                begin
                  PageId   := NumPage;
                  FFont    := FontNum;
                  FSize    := T_Font(Fonts[FontNum]).GetSize;
                  FColor   := T_Font(Fonts[FontNum]).GetColor;
                  TextPosX := GetTextPos;
                  if (txtRight in TxtFlags) then
                    TextPosX := ColPos + ColWidth - ColMargin - Fnt.TextWidth(Wraplst[Cpt]);
                  if (txtHCenter in TxtFlags) then
                    TextPosX := GetTextPos + (ColWidth - Fnt.TextWidth(Wraplst[Cpt])) / 2;
                  TextPosY := Paper.H - PosY - Fnt.Ascent - (Fnt.Height + LnSpInt) * Cpt;
                  TextWidt := ColWidth;
                  Writting := Wraplst[Cpt];
                end;
                PdfPage.Add(PdfTexte);
              end;
              WrapLst.Free;
            end;
          end
        else if Fnt.TextWidth(Texts[Text]) < Paper.W - PosX then
        begin
          PdfTexte := TPdfTexte.Create;
          with PdfTexte do
          begin
            PageId := NumPage;
            FFont  := FontNum;
            FSize  := T_Font(Fonts[FontNum]).GetSize;
            FColor := T_Font(Fonts[FontNum]).GetColor;
            FPosX  := PosX;
            FPosY  := Paper.H - PosY;
            FWidth := Paper.W;
            FText  := Texts[Text];
          end;
          PdfPage.Add(PdfTexte);
        end
        else
        begin
          Wraplst      := TStringList.Create;
          Wraplst.Text := Texts[Text];
          for Cpt := 0 to Pred(Wraplst.Count) do
            Wraplst[Cpt] := AddLineBreaks(Wraplst[Cpt], Round(Paper.W - PosX), Fnt);
          Wraplst.Text := Wraplst.Text;
          for Cpt        := 0 to Pred(Wraplst.Count) do
          begin
            PdfTexte     := TPdfTexte.Create;
            with PdfTexte do
            begin
              PageId := NumPage;
              FFont  := FontNum;
              FSize  := T_Font(Fonts[FontNum]).GetSize;
              FColor := T_Font(Fonts[FontNum]).GetColor;
              FPosX  := PosX;
              FPosY  := Paper.H - PosY - Fnt.Ascent - (Fnt.Height + LnSpInt) * Cpt;
              FWidth := Paper.W;
              FText  := Wraplst[Cpt];
            end;
            PdfPage.Add(PdfTexte);
          end;
          WrapLst.Free;
        end;
      end;
    end;
  end;
end;

function T_Report.WriteNumber(PosX, PosY: single; Column, TextNum, TextTot, FontNum, BkColorNum, BordNum, SpLine: integer; TxtFlags: TfpgTextFlags; Total, Alpha: Boolean; Zone: TZone; SPNum: TSectPageNum): single;

  function BuildChaine: string;
  var
    NumAlpha: string;
  begin
    case SPNum of
      PageNum:
      begin
        if Total then
          Result := Texts[TextNum] + ' ' + IntToStr(NumPage) + ' ' + Texts[TextTot] + ' ' + IntToStr(T_Section(Sections[Pred(Sections.Count)]).TotPages)
        else
          Result := Texts[TextNum] + ' ' + IntToStr(NumPage);
      end;
      SectNum:
      begin
        if Alpha then
          NumAlpha := Convert2Alpha(NumSection)
        else
          NumAlpha := IntToStr(NumSection);
        if Total then
          Result   := Texts[TextNum] + ' ' + NumAlpha + ' ' + Texts[TextTot] + ' ' + IntToStr(Sections.Count)
        else
          Result   := Texts[TextNum] + ' ' + NumAlpha;
      end;
      PSectNum:
      begin
        if Alpha then
          NumAlpha := Convert2Alpha(NumPageSection)
        else
          NumAlpha := IntToStr(NumPageSection);
        if Total then
          Result   := Texts[TextNum] + ' ' + NumAlpha + ' ' + Texts[TextTot] + ' ' + IntToStr(T_Section(Sections[Pred(NumSection)]).NbPages)
        else
          Result   := Texts[TextNum] + ' ' + NumAlpha;
      end;
    end;
  end;

var
  PosH, PosV, LnSpInt, LnSpSup, LnSpInf, ThickLine: single;
  HTxt, HeighTxt, Half, ColorLine: integer;
  EndOfLine, UseCurFont: Boolean;
  Fnt: TfpgFont;
  StyleLine: TfpgLineStyle;
  Chaine: string;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    EndOfLine := False;
    if FPreparation = ppPrepare then
    begin
      if FCurrentFont <> FontNum then
      begin
        FCurrentFont := FontNum;
        UseCurFont   := False;
      end
      else
        UseCurFont   := True;
    end;
    Fnt := T_Font(Fonts[FontNum]).GetFont;
    if LineSpaces.Count = 0 then
      LineSpace(0, 0, 0);
    if FCurrentLineSpace <> SpLine then
      FCurrentLineSpace := SpLine;
    with T_LineSpace(LineSpaces[FCurrentLineSpace]) do
    begin
      LnSpSup := GetSup;
      LnSpInt := GetInt;
      LnSpInf := GetInf;
    end;
    if Column = -2 then
      Column   := DefaultCol;
    if Column > -1 then
      HeighTxt := TxtHeight(Round(T_Column(Columns[Column]).GetTextWidth), Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0', Fnt, Round(LnSpInt)) + Round(LnSpSup + LnSpInf)
    else
      HeighTxt := TxtHeight(Paper.W, Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0', Fnt, Round(LnSpInt)) + Round(LnSpSup + LnSpInf);
    if (Column > -1) and (BordNum > -1) then
      Half     := Round(T_LineStyle(LineStyles[T_Border(Borders[BordNum]).GetStyle]).GetThick) div 2;
    case FPreparation of
      ppPrepare:
      begin
        if NbPages = 0 then
          Page;
        if Column > -1 then
        begin
          HTxt := VWriteLine.LineHeight;
          if HTxt < HeighTxt then
            HTxt := HeighTxt;
        end
        else if HTxt < Fnt.Height then
          HTxt := Fnt.Height;
        case Zone of
          zHeader:
            FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
          zFooter:
          begin
            FPosRef.Y     := FCurrentMargin.B - HTxt;
            FFooterHeight := FFooterHeight + HTxt;
            ShiftFooterLines(HTxt);
          end;
        end;
        if PosY = lnCurrent then
          PosV      := FPosRef.Y + LnSpSup
        else
        begin
          EndOfLine := True;
          if PosY = lnEnd then
          begin
            PosV := FPosRef.Y + LnSpSup;
            case Zone of
              zHeader:
                FPosRef.Y := FPosRef.Y + HTxt;
            end;
            if BordNum > -1 then
              with T_Border(Borders[BordNum]) do
                if bfBottom in GetFlags then
                  FPosRef.Y := FPosRef.Y + 1;
          end
          else
          begin
            PosV      := PosY;
            FPosRef.Y := PosV + LnSpInf;
          end;
          case Zone of
            zHeader:
              FHeaderHeight := FPosRef.Y - FCurrentMargin.T;
          end;
        end;
        if Column = -1 then
          if PosX > 0 then
            PosH := PosX
          else
          begin
            PosH := T_Column(Columns[ColDefaut]).GetTextPos - T_Column(Columns[0]).ColMargin;
            if (txtRight in TxtFlags) then
              if Total then
                PosH := PosH + T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0 ') - T_Column(Columns[ColDefaut]).ColMargin
              else
                PosH := PosH + T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ') - T_Column(Columns[ColDefaut]).ColMargin;
            if (txtHCenter in TxtFlags) then
              if Total then
                PosH := PosH + (T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0 ')) / 2
              else
                PosH := PosH + (T_Column(Columns[ColDefaut]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ')) / 2;
          end
        else if PosX > 0 then
          if (PosX < T_Column(Columns[Column]).GetTextPos) or (PosX > (T_Column(Columns[Column]).GetTextPos + T_Column(Columns[Column]).GetTextWidth)) then
            PosH := T_Column(Columns[Column]).GetTextPos
          else
            PosH := PosX
        else
        begin
          PosH := T_Column(Columns[Column]).GetTextPos - T_Column(Columns[Column]).ColMargin;
          if (txtRight in TxtFlags) then
            if Total then
              PosH := PosH + T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0 ') - T_Column(Columns[Column]).ColMargin
            else
              PosH := PosH + T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ') - T_Column(Columns[Column]).ColMargin;
          if (txtHCenter in TxtFlags) then
            if Total then
              PosH := PosH + (T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0 ')) / 2
            else
              PosH := PosH + (T_Column(Columns[Column]).ColWidth - Fnt.TextWidth(Texts[TextNum] + ' 0 ')) / 2;
        end;
        FPosRef.X := PosH + Fnt.TextWidth(Texts[TextNum] + ' 0 ' + Texts[TextTot] + ' 0 ');
        VWriteLine.LoadNumber(PosH, PosV, Column, TextNum, TextTot, FontNum, HTxt, BkColorNum, BordNum, SpLine, UseCurFont, TxtFlags, Total, Alpha, SPNum);
        Result := Pixels2Dim(FPosRef.Y);
        if EndOfLine then
        begin
          HTxt := 0;
          LineEnd(Zone);
        end;
      end;
      ppVisualize:
      begin
        with FCanvas do
        begin
          Chaine := BuildChaine;
          Font   := T_Font(Fonts[FontNum]).GetFont;
          SetTextColor(T_Font(Fonts[FontNum]).GetColor);
          if Column > -1 then
            with T_Column(Columns[Column]) do
            begin
              if BkColorNum > -1 then
                SetColor(T_BackColor(BackColors[BkColorNum]).GetColor)
              else
                SetColor(GetColor);
              FillRectangle(Round(ColPos), Round(PosY - LnSpSup), Round(ColWidth), HeighTxt);
              if BordNum > -1 then
                with T_Border(Borders[BordNum]) do
                begin
                  SetLineStyle(Round(T_LineStyle(LineStyles[GetStyle]).GetThick), T_LineStyle(LineStyles[GetStyle]).GetStyle);
                  SetColor(T_LineStyle(LineStyles[GetStyle]).GetColor);
                  if bfLeft in GetFlags then
                    DrawLine(Round(ColPos) + Half, Round(PosY - LnSpSup), Round(ColPos) + Half, Round(PosY - LnSpSup) + HeighTxt);
                  if bfRight in GetFlags then
                    DrawLine(Round(ColPos + ColWidth) - Half, Round(PosY - LnSpSup), Round(ColPos + ColWidth) - Half, Round(PosY - LnSpSup) + HeighTxt);
                  if bfTop in GetFlags then
                    DrawLine(Round(ColPos), Round(PosY - LnSpSup) + Half, Round(ColPos + ColWidth), Round(PosY - LnSpSup) + Half);
                  if bfBottom in GetFlags then
                    DrawLine(Round(ColPos), Round(PosY - LnSpSup) + HeighTxt - Succ(Half), Round(ColPos + ColWidth), Round(PosY - LnSpSup) + HeighTxt - Succ(Half));
                end;
              DrawText(Round(GetTextPos), Round(PosY), Round(GetTextWidth), 0, Chaine, TxtFlags, Round(LnSpInt));
            end
          else
            DrawText(Round(PosX), Round(PosY), Chaine, TxtFlags);
        end;
      end;
      ppPdfFile:
      begin
        Chaine := BuildChaine;
        if Column > -1 then
          with T_Column(Columns[Column]) do
          begin
            if (GetColor <> clWhite) or (BkColorNum > -1) then
            begin
              PdfRect := TPdfRect.Create;
              with PdfRect do
              begin
                PageId  := NumPage;
                FLeft   := ColPos;
                FBottom := Paper.H - PosY + LnSpSup - HeighTxt;
                FHeight := HeighTxt;
                FWidth  := ColWidth;
                if BkColorNum > -1 then
                  FColor := T_BackColor(BackColors[BkColorNum]).GetColor
                else
                  FColor := GetColor;
                FFill := True;
                FStroke := False;
              end;
              PdfPage.Add(PdfRect);
            end;
            if BordNum > -1 then
              with T_Border(Borders[BordNum]) do
              begin
                StyleLine := T_LineStyle(LineStyles[GetStyle]).GetStyle;
                ColorLine := T_LineStyle(LineStyles[GetStyle]).GetColor;
                ThickLine := T_LineStyle(LineStyles[GetStyle]).GetThick;
                if bfLeft in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfRight in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos + ColWidth;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfTop in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
                if bfBottom in GetFlags then
                begin
                  PdfLine := TPdfLine.Create;
                  with PdfLine do
                  begin
                    PageId  := NumPage;
                    FBeginX := ColPos;
                    FBeginY := Paper.H - PosY + LnSpSup - HeighTxt;
                    FEndX   := ColPos + ColWidth;
                    FEndY   := Paper.H - PosY + LnSpSup - HeighTxt;
                    FStyle  := StyleLine;
                    FColor  := ColorLine;
                    FThick  := ThickLine;
                  end;
                  PdfPage.Add(PdfLine);
                end;
              end;
            PdfTexte := TPdfTexte.Create;
            with PdfTexte do
            begin
              PageId   := NumPage;
              FFont    := FontNum;
              FSize    := T_Font(Fonts[FontNum]).GetSize;
              FColor   := T_Font(Fonts[FontNum]).GetColor;
              TextPosX := GetTextPos;
              if (txtRight in TxtFlags) then
                TextPosX := ColPos + ColWidth - ColMargin - Fnt.TextWidth(Chaine);
              if (txtHCenter in TxtFlags) then
                TextPosX := GetTextPos + (ColWidth - Fnt.TextWidth(Chaine)) / 2;
              TextPosY := Paper.H - PosY - Fnt.Ascent;
              TextWidt := ColWidth;
              Writting := Chaine;
            end;
            PdfPage.Add(PdfTexte);
          end
        else
        begin
          PdfTexte := TPdfTexte.Create;
          with PdfTexte do
          begin
            PageId := NumPage;
            FFont  := FontNum;
            FSize  := T_Font(Fonts[FontNum]).GetSize;
            FColor := T_Font(Fonts[FontNum]).GetColor;
            FPosX  := PosX;
            FPosY  := PosY - Fnt.Ascent;
            FWidth := Paper.W;
            FText  := Chaine;
          end;
          PdfPage.Add(PdfTexte);
        end;
      end;
    end;
  end;
end;

function T_Report.InsertSpace(PosY: single; Column: integer; SpaceHeight: single; BkColorNum: integer; Zone: TZone): single;
var
  PosV: single;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    if PosY > -1 then
      PosV   := PosY
    else
      PosV   := FPosRef.Y;
    if Column = -2 then
      Column := DefaultCol;
    case FPreparation of
      ppPrepare:
      begin
        case Zone of
          zHeader:
          begin
            FPosRef.Y     := FCurrentMargin.T + FHeaderHeight;
            FPosRef.Y     := FPosRef.Y + SpaceHeight;
            FHeaderHeight := FPosRef.Y - FCurrentMargin.T;
            LoadSpaceHeader(PosV, Column, SpaceHeight, BkColorNum);
          end;
          zPage:
          begin
            FPosRef.Y := FCurrentMargin.T + FHeaderHeight + FPageHeight;
            if FPosRef.Y + SpaceHeight > FCurrentMargin.B - FFooterHeight then
            begin
              FPosRef.Y := FCurrentMargin.T + FHeaderHeight;
              Page;
            end
            else
              FPosRef.Y := FPosRef.Y + SpaceHeight;
            FPageHeight := FPosRef.Y - FHeaderHeight - FCurrentMargin.T;
            LoadSpacePage(PosV, Column, SpaceHeight, BkColorNum);
          end;
          zFooter:
          begin
            FPosRef.Y     := FCurrentMargin.B - SpaceHeight;
            FFooterHeight := FFooterHeight + SpaceHeight;
            PosV          := FPosRef.Y;
            ShiftFooterLines(SpaceHeight);
            LoadSpaceFooter(PosV, Column, SpaceHeight, BkColorNum);
          end;
        end;
        if FGroup then
          LoadSpaceGroup(SpaceHeight);
        Result := Pixels2Dim(FPosRef.Y);
        LineEnd(Zone);
      end;
      ppVisualize:
      begin
        with FCanvas, T_Column(Columns[Column]) do
        begin
          if BkColorNum > -1 then
            SetColor(T_BackColor(BackColors[BkColorNum]).GetColor)
          else
            SetColor(GetColor);
          FillRectangle(Round(ColPos), Round(PosV), Round(ColWidth), Round(SpaceHeight));
        end;
      end;
      ppPdfFile:
      begin
        if Column > -1 then
          with T_Column(Columns[Column]) do
          begin
            if (GetColor <> clWhite) or (BkColorNum > -1) then
            begin
              PdfRect := TPdfRect.Create;
              with PdfRect do
              begin
                PageId  := NumPage;
                FLeft   := ColPos;
                FBottom := Paper.H - PosY - SpaceHeight;
                FHeight := SpaceHeight;
                FWidth  := ColWidth;
                if BkColorNum > -1 then
                  FColor := T_BackColor(BackColors[BkColorNum]).GetColor
                else
                  FColor := GetColor;
                FFill := True;
                FStroke := False;
              end;
              PdfPage.Add(PdfRect);
            end;
          end; { with }
      end; { ppPdfFile label }
    end; { case }
  end; { with }
end;

procedure T_Report.LineEnd(Zone: TZone);
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case Zone of
      zHeader:
        LoadCmdHeader;
      zPage:
        if FGroup then
          LoadCmdGroup
        else
          LoadCmdPage;
      zFooter:
        LoadCmdFooter;
    end;
  end;
end;

procedure T_Report.DrawAFrame(StyLine, XLeft, XRight, YTop,YBottom: integer; Zone: TZone);
var
  Half, MarginL, MarginR, MarginT, MarginB, HeaderH, FooterH: integer;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case FPreparation of
      ppPrepare:
        with FCurrentMargin do
        begin
          MarginL:= Round(L);
          MarginR:= Round(R);
          MarginT:= Round(T);
          MarginB:= Round(B);
          HeaderH:= Round(FHeaderHeight);
          FooterH:= Round(FFooterHeight);
          case Zone of
            zHeader:
              LoadFrame(StyLine, MarginL, MarginR, MarginT, MarginT + HeaderH, zHeader);
            zPage:
              LoadFrame(StyLine, MarginL, MarginR, MarginT + HeaderH, MarginB - FooterH, zPage);
            zFooter:
              LoadFrame(StyLine, MarginL, MarginR, MarginB - FooterH, MarginB, zFooter);
            zMargins:
              LoadFrame(StyLine, MarginL, MarginR, MarginT, MarginB, zMargins);
          end;
        end;
      ppVisualize:
      begin
        with FCanvas do
        begin
          with T_LineStyle(LineStyles[StyLine]) do
          begin
            SetLineStyle(Round(GetThick), GetStyle);
            Half := Round(GetThick) div 2;
            SetColor(GetColor);
          end;
          DrawLine(XLeft + Half, YTop, XLeft + Half, YBottom);      // left
          DrawLine(XRight - Half, YTop, XRight - half, YBottom);    // right
          DrawLine(XLeft, YTop + Half, XRight, YTop + Half);        // top
          DrawLine(XLeft, YBottom - Half, XRight, YBottom - Half);  // bottom
        end; { with FCanvas }
      end;
      ppPdfFile:
      begin
        PdfRect := TPdfRect.Create;
        with PdfRect do
        begin
          PageId := NumPage;
          with T_LineStyle(LineStyles[StyLine]) do
          begin
            FThick     := GetThick;
            FColor     := GetColor;
            FLineStyle := GetStyle;
          end;
          FLeft:= XLeft;
          FBottom:= Paper.H - YBottom;
          FHeight:= YBottom - YTop;
          FWidth:= XRight - XLeft;
          FFill   := False;
          FStroke := True;
          PdfPage.Add(PdfRect);
        end; { with PdfRect }
      end;
    end; { case FPreparation }
  end; { with T_Section... }
end;

procedure T_Report.DrawALine(XBegin, YBegin, XEnd, YEnd: single; StyLine: integer);
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case FPreparation of
      ppPrepare:
        LoadLine(XBegin, YBegin, ColDefaut, XEnd, YEnd, StyLine);
      ppVisualize:
        with FCanvas do
        begin
          with T_LineStyle(LineStyles[StyLine]) do
          begin
            SetLineStyle(Round(GetThick), GetStyle);
            SetColor(GetColor);
          end;
          DrawLine(Round(XBegin), Round(YBegin), Round(XEnd), Round(YEnd));
        end;
      ppPdfFile:
      begin
        PdfLine := TPdfLine.Create;
        with PdfLine do
        begin
          PageId  := NumPage;
          FBeginX := XBegin;
          FBeginY := Paper.H - YBegin;
          FEndX   := XEnd;
          FEndY   := Paper.H - YEnd;
          FStyle  := T_LineStyle(LineStyles[StyLine]).GetStyle;
          ;
          FColor  := T_LineStyle(LineStyles[StyLine]).GetColor;
          FThick  := T_LineStyle(LineStyles[StyLine]).GetThick;
        end;
        PdfPage.Add(PdfLine);
      end;
    end; { case FPreparation }
  end; { with T_Section... }
end;

procedure T_Report.DrawAHorizLine(XBegin, YBegin: single; Column: integer; XEnd: single; StyLine: integer; Zone: TZone);
var
  PosV: single;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case FPreparation of
      ppPrepare:
      begin
        case Zone of
          zHeader:
          begin
            FPosRef.Y     := FCurrentMargin.T + FHeaderHeight;
            PosV          := FPosRef.Y + XBegin;
            FPosRef.Y     := FPosRef.Y + XBegin + YBegin + T_LineStyle(LineStyles[StyLine]).GetThick;
            FHeaderHeight := FPosRef.Y - FCurrentMargin.T;
            with T_Column(Columns[Column]) do
              LoadLineHorizHeader(ColPos, PosV, Column, ColPos + ColWidth, PosV, StyLine);
          end;
          zPage:
          begin
            FPosRef.Y   := FCurrentMargin.T + FHeaderHeight + FPageHeight;
            PosV        := FPosRef.Y + XBegin;
            FPosRef.Y   := FPosRef.Y + XBegin + YBegin + T_LineStyle(LineStyles[StyLine]).GetThick;
            FPageHeight := FPosRef.Y - FHeaderHeight - FCurrentMargin.T;
            with T_Column(Columns[Column]) do
              LoadLineHorizPage(ColPos, PosV, Column, ColPos + ColWidth, PosV, StyLine);
          end;
          zFooter:
          begin
            FPosRef.Y     := FCurrentMargin.B - XBegin;
            PosV          := FPosRef.Y;
            FPosRef.Y     := FPosRef.Y - YBegin - T_LineStyle(LineStyles[StyLine]).GetThick;
            FFooterHeight := FFooterHeight + XBegin + YBegin + T_LineStyle(LineStyles[StyLine]).GetThick;
            ShiftFooterLines(XBegin + YBegin + T_LineStyle(LineStyles[StyLine]).GetThick);
            with T_Column(Columns[Column]) do
              LoadLineHorizFooter(ColPos, PosV, Column, ColPos + ColWidth, PosV, StyLine);
          end;
        end;
        if FGroup then
          LoadLineHorizGroupe(T_LineStyle(LineStyles[StyLine]).GetThick);
      end;
      ppVisualize:
      begin
        with FCanvas do
        begin
          with T_LineStyle(LineStyles[StyLine]) do
          begin
            SetLineStyle(Round(GetThick), GetStyle);
            SetColor(GetColor);
          end;
          DrawLine(Round(XBegin), Round(YBegin), Round(XEnd), Round(YBegin));
        end;
      end;
      ppPdfFile:
      begin
        PdfLine := TPdfLine.Create;
        with PdfLine do
        begin
          PageId  := NumPage;
          FBeginX := XBegin;
          FBeginY := Paper.H - YBegin;
          FEndX   := XEnd;
          FEndY   := Paper.H - YBegin;
          FStyle  := T_LineStyle(LineStyles[StyLine]).GetStyle;
          ;
          FColor  := T_LineStyle(LineStyles[StyLine]).GetColor;
          FThick  := T_LineStyle(LineStyles[StyLine]).GetThick;
        end;
        PdfPage.Add(PdfLine);
      end;
    end; { case FPreparation }
  end; { with T_Section... }
end;

procedure T_Report.PaintSurface(Points: T_Points; Couleur: TfpgColor);
var
  OldColor: TfpgColor;
  Cpt: integer;
  Pts: array of TPoint;
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case FPreparation of
      ppPrepare:
        LoadSurf(Points, Couleur);
      ppVisualize:
      begin
        OldColor := FCanvas.Color;
        FCanvas.SetColor(Couleur);
        SetLength(Pts, Length(Points));
        for Cpt := 0 to Pred(Length(Pts)) do
        begin
          Pts[Cpt].X := Round(Points[Cpt].X);
          Pts[Cpt].Y := Round(Points[Cpt].Y);
        end;
        FCanvas.DrawPolygon(Pts);
        FCanvas.SetColor(OldColor);
      end;
      ppPdfFile:
      begin
        PdfSurf := TPdfSurf.Create;
        SetLength(PdfSurf.FPoints, Length(Points));
        for Cpt := 0 to Pred(Length(Points)) do
        begin
          PdfSurf.FPoints[Cpt].X := Points[Cpt].X;
          PdfSurf.FPoints[Cpt].Y := Paper.H - Points[Cpt].Y;
        end;
        with PdfSurf do
        begin
          PageId := NumPage;
          //SetLength(FPoints,Length(Points));
          //for Cpt:= 0 to Pred(Length(Points)) do   // weird behaviour: points gets length= 0 inside the with clause !
          //  begin
          //  FPoints[Cpt].X:= Points[Cpt].X;
          //  FPoints[Cpt].Y:= Paper.H-Points[Cpt].Y;
          //  end;
          FColor := Couleur;
        end;
        PdfPage.Add(PdfSurf);
      end;
    end; { case FPreparation }
  end; { with T_Section... }
end;

procedure T_Report.PaintImage(PosX, PosY: single; Column, ImgNum: integer; Zone: TZone);
begin
  with T_Section(Sections[Pred(NumSection)]) do
  begin
    case FPreparation of
      ppPrepare:
      begin
        if Column > -1 then
          PosX := T_Column(Columns[Column]).ColPos + PosX;
        case Zone of
          zHeader:
          begin
            PosY := FCurrentMargin.T + PosY;
            LoadImgHeader(PosX, PosY, Column, ImgNum);
          end;
          zPage:
          begin
            PosY := FCurrentMargin.T + FHeaderHeight + PosY;
            LoadImgPage(PosX, PosY, Column, ImgNum);
          end;
          zFooter:
          begin
            PosY := FCurrentMargin.B - FFooterHeight + PosY;
            LoadImgFooter(PosX, PosY, Column, ImgNum);
          end;
        end;
      end;
      ppVisualize:
        FCanvas.DrawImage(Round(PosX), Round(PosY), TfpgImage(Images[ImgNum]));
      ppPdfFile:
      begin
        PdfImg := TPdfImg.Create;
        with PdfImg do
        begin
          PageId    := NumPage;
          ImgNumber := ImgNum;
          ImgLeft   := PosX;
          ImgBottom := Paper.H - PosY - TfpgImage(Images[ImgNum]).Height;
          ImgWidth  := TfpgImage(Images[ImgNum]).Width;
          ImgHeight := TfpgImage(Images[ImgNum]).Height;
        end;
        PdfPage.Add(PdfImg);
      end;
    end; { case FPreparation }
  end; { with T_Section... }
end;

function T_Report.GetSectionTitle: string;
begin
  Result := T_Section(Sections[Pred(Sections.Count)]).Title;
end;

procedure T_Report.SetSectionTitle(ATitle: string);
begin
  T_Section(Sections[Pred(Sections.Count)]).Title := ATitle;
end;

{ public methods }

constructor T_Report.Create;
begin
  inherited Create;
  OldSeparator := DecimalSeparator;
  DecimalSeparator := '.';
  Sections   := TList.Create;
  Fonts      := TList.Create;
  Columns    := TList.Create;
  LineSpaces := TList.Create;
  BackColors := TList.Create;
  LineStyles := TList.Create;
  Borders    := TList.Create;
  Images     := TList.Create;
  ImageNames := TStringList.Create;
  Texts      := TStringList.Create;
  VWriteLine := T_WriteLine.Create;
  PdfPage    := TList.Create;
  Outline    := False;
end;

destructor T_Report.Destroy;
var
  Cpt: integer;
begin
  if Sections.Count > 0 then
    for Cpt := 0 to Pred(Sections.Count) do
      T_Section(Sections[Cpt]).Free;
  Sections.Free;
  if Fonts.Count > 0 then
    for Cpt := 0 to Pred(Fonts.Count) do
      T_Font(Fonts[Cpt]).Free;
  Fonts.Free;
  if Columns.Count > 0 then
    for Cpt := 0 to Pred(Columns.Count) do
      T_Column(Columns[Cpt]).Free;
  Columns.Free;
  if LineSpaces.Count > 0 then
    for Cpt := 0 to Pred(LineSpaces.Count) do
      T_LineSpace(LineSpaces[Cpt]).Free;
  LineSpaces.Free;
  if BackColors.Count > 0 then
    for Cpt := 0 to Pred(BackColors.Count) do
      T_BackColor(BackColors[Cpt]).Free;
  BackColors.Free;
  if LineStyles.Count > 0 then
    for Cpt := 0 to Pred(LineStyles.Count) do
      T_LineStyle(LineStyles[Cpt]).Free;
  LineStyles.Free;
  if Borders.Count > 0 then
    for Cpt := 0 to Pred(Borders.Count) do
      T_Border(Borders[Cpt]).Free;
  Borders.Free;
  if Images.Count > 0 then
    for Cpt := 0 to Pred(Images.Count) do
      TfpgImage(Images[Cpt]).Free;
  Images.Free;
  ImageNames.Free;
  Texts.Free;
  VWriteLine.Free;
  if PdfPage.Count > 0 then
  begin
    for Cpt := 0 to Pred(PdfPage.Count) do
    begin
      if TPdfElement(PdfPage[Cpt]) is TPdfTexte then
        TPdfTexte(PdfPage[Cpt]).Free
      else if TPdfElement(PdfPage[Cpt]) is TPdfRect then
        TPdfRect(PdfPage[Cpt]).Free
      else if TPdfElement(PdfPage[Cpt]) is TPdfLine then
        TPdfLine(PdfPage[Cpt]).Free
      else if TPdfElement(PdfPage[Cpt]) is TPdfSurf then
        TPdfSurf(PdfPage[Cpt]).Free
      else if TPdfElement(PdfPage[Cpt]) is TPdfImg then
        TPdfImg(PdfPage[Cpt]).Free;
    end;
  end;
  PdfPage.Free;
  DecimalSeparator := OldSeparator;
  inherited;
end;

procedure T_Report.BeginWrite(IniOrientation: TOrient = oPortrait; IniPaperType: TPaperType = A4; IniMeasure: TMeasureUnit = msMM; IniVersion: char = 'F'; IniVisu: Boolean = True);
begin
  FVersion       := IniVersion;
  FOrientation   := IniOrientation;
  FPaperType     := IniPaperType;
  FMeasureUnit   := IniMeasure;
  FPreparation   := ppPrepare;
  FVisualization := IniVisu;
  PrepareFormat;
  if IniVisu then
    CreateVisu;
  FCurrentFont := -1;
  FCurrentLineSpace := -1;
  FGroup := False;
  with FPaper do
    VColumn := T_Column.Create(Printable.L, Printable.R - Printable.L, 0, clWhite);
  Columns.Add(VColumn);
end;

procedure T_Report.EndWrite;
var
  Cpt: integer;
begin
  FPreparation := ppPdfFile;
  if Sections.Count > 0 then
  begin
    for Cpt := 1 to Sections.Count do
    begin
      NumSection := Cpt;
      if T_Section(Sections[Pred(NumSection)]).TotPages > 0 then
      begin
        NumPageSection := 1;
        NumPage        := 1;
      end;
    end
  end
  else
    Exit;
  for Cpt := 1 to T_Section(Sections[Pred(NumSection)]).TotPages do
    PrintPage(Cpt);
  if FVisualization then
  begin
    FPreparation := ppVisualize;
    try
      WriteDocument;
      F_Visu.ShowModal;
    finally
      F_Visu.Free;
    end;
  end;
end;

procedure T_Report.WriteDocument;
begin
  if FVisualization then
    FCanvas := Bv_Visu.Canvas;
end;

procedure T_Report.PagePreview;
begin
  FVisualization := not FVisualization;
  if FVisualization then
    FCanvas := Bv_Visu.Canvas;
end;

procedure T_Report.Section(MgLeft, MgRight, MgTop, MgBottom: single; BackPos: single; IniOrientation: TOrient = oPortrait);
var
  CMargin: single;
begin
  case FPreparation of
    ppPrepare:
    begin
      FOrientation := IniOrientation;
      PrepareFormat;
      with FCurrentMargin, FPaper do
      begin
        if Dim2Pixels(MgLeft) > Printable.L then
          L := Dim2Pixels(MgLeft)
        else
          L := Printable.L;
        if (W - Dim2Pixels(MgRight)) < Printable.R then
          R := W - Dim2Pixels(MgRight)
        else
          R := Printable.R;
        if Dim2Pixels(MgTop) > Printable.T then
          T := Dim2Pixels(MgTop)
        else
          T := Printable.T;
        if (H - Dim2Pixels(MgBottom)) < Printable.B then
          B := H - Dim2Pixels(MgBottom)
        else
          B := Printable.B;
      end;
      FPosRef.X := FCurrentMargin.L;
      FHeaderHeight := 0;
      FPageHeight   := 0;
      FFooterHeight := 0;
      NumSection    := NumSection + 1;
      VSection      := T_Section.Create(FPaper, FCurrentMargin, NumSection);
      Sections.Add(VSection);
      CMargin       := Dim2Pixels(BackPos);
      VColumn       := T_Column.Create(FCurrentMargin.L, FCurrentMargin.R - FCurrentMargin.L, CMargin, clWhite);
      T_Section(Sections[Pred(Sections.Count)]).DefaultCol := Columns.Add(VColumn);
    end;
  end;
end;

procedure T_Report.Page;
begin
  if FPreparation = ppPrepare then
  begin
    NumPage     := NumPage + 1;
    T_Section(Sections[Pred(Sections.Count)]).LoadPage(NumPage);
    FPosRef.Y   := FCurrentMargin.T + FHeaderHeight;
    FPageHeight := 0;
  end;
end;

function T_Report.BackColor(FdColor: TfpgColor): integer;
begin
  VBackColor := T_BackColor.Create(FdColor);
  Result     := BackColors.Add(VBackColor);
end;

function T_Report.Font(FtNom: string; FtColor: TfpgColor): integer;
begin
  VFont  := T_Font.Create(FtNom, FtColor);
  Result := Fonts.Add(VFont);
end;

function T_Report.LineStyle(StThick: single; StColor: TfpgColor; StStyle: TfpgLineStyle): integer;
begin
  VLineStyle := T_LineStyle.Create(StThick, StColor, StStyle);
  Result     := LineStyles.Add(VLineStyle);
end;

function T_Report.Border(BdFlags: TBorderFlags; BdStyle: integer): integer;
begin
  VBorder := T_Border.Create(BdFlags, BdStyle);
  Result  := Borders.Add(VBorder);
end;

 //function T_Report.Border(BdFlags: TBorderFlags; StFlags: array of Integer): Integer;
 //begin
 //VBorder:= T_Border.Create(BdFlags,BdStyle);
 //Result:= Borders.Add(VBorder);
 //end;

function T_Report.Column(ClnPos, ClnWidth: single; ClnMargin: single = 0; ClnColor: TfpgColor = clWhite): integer;
var
  CPos, CWidth, CMargin: single;
begin
  CPos    := Dim2Pixels(ClnPos);
  CWidth  := Dim2Pixels(ClnWidth);
  CMargin := Dim2Pixels(ClnMargin);
  VColumn := T_Column.Create(CPos, CWidth, CMargin, ClnColor);
  Result  := Columns.Add(VColumn);
end;

procedure T_Report.WriteHeader(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefText: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [txtWrap];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefText := Texts.IndexOf(Text);
  if RefText = -1 then
    RefText := Texts.Add(Text);
  WriteText(Horiz, Verti, ColNum, RefText, FontNum, BkColorNum, BordNum, LineSpNum, Flags, zHeader);
end;

function T_Report.WritePage(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1): single;
var
  RefText: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [txtWrap];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefText := Texts.IndexOf(Text);
  if RefText = -1 then
    RefText := Texts.Add(Text);
  Result := WriteText(Horiz, Verti, ColNum, RefText, FontNum, BkColorNum, BordNum, LineSpNum, Flags, ZPage);
end;

procedure T_Report.WriteFooter(Horiz, Verti: single; Text: string; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefText: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [txtWrap];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefText := Texts.IndexOf(Text);
  if RefText = -1 then
    RefText := Texts.Add(Text);
  WriteText(Horiz, Verti, ColNum, RefText, FontNum, BkColorNum, BordNum, LineSpNum, Flags, zFooter);
end;

procedure T_Report.NumSectionHeader(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
  LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TexteSect);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TexteSect);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, Alpha, zHeader, SectNum);
end;

procedure T_Report.NumSectionFooter(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
  LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TexteSect);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TexteSect);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, Alpha, zFooter, SectNum);
end;

procedure T_Report.NumPageHeader(Horiz, Verti: single; TextePage: string = ''; TextTot: string = ''; Total: Boolean = False; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TextePage);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TextePage);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, False, zHeader, PageNum);
end;

procedure T_Report.NumPageFooter(Horiz, Verti: single; TextePage: string = ''; TextTot: string = ''; Total: Boolean = False; ColNum: integer = 0; FontNum: integer = 0; LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TextePage);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TextePage);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, False, zFooter, PageNum);
end;

procedure T_Report.NumPageSectionHeader(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
  LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TexteSect);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TexteSect);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, Alpha, zHeader, PSectNum);
end;

procedure T_Report.NumPageSectionFooter(Horiz, Verti: single; TexteSect: string = ''; TextTot: string = ''; Total: Boolean = False; Alpha: Boolean = False; ColNum: integer = 0; FontNum: integer = 0;
  LineSpNum: integer = 0; BkColorNum: integer = -1; BordNum: integer = -1);
var
  RefTextPage, RefTextTot: integer;
  Flags: TfpgTextFlags;
begin
  Flags := [];
  if Horiz < 0 then
  begin
    if Horiz = cnLeft then
      Include(Flags, txtLeft);
    if Horiz = cnCenter then
      Include(Flags, txtHCenter);
    if Horiz = cnRight then
      Include(Flags, txtRight);
  end
  else
    Horiz := Dim2Pixels(Horiz);
  if Verti > 0 then
    Verti := Dim2Pixels(Verti);
  RefTextPage := Texts.IndexOf(TexteSect);
  if RefTextPage = -1 then
    RefTextPage := Texts.Add(TexteSect);
  RefTextTot := Texts.IndexOf(TextTot);
  if RefTextTot = -1 then
    RefTextTot := Texts.Add(TextTot);
  WriteNumber(Horiz, Verti, ColNum, RefTextPage, RefTextTot, FontNum, BkColorNum, BordNum, LineSpNum, Flags, Total, Alpha, zFooter, PSectNum);
end;

procedure T_Report.HorizLineHeader(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
begin
  DrawAHorizLine(Dim2Pixels(SpBefore), Dim2Pixels(SpAfter), ColNum, -1, StyleNum, zHeader);
end;

procedure T_Report.HorizLinePage(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
begin
  DrawAHorizLine(Dim2Pixels(SpBefore), Dim2Pixels(SpAfter), ColNum, -1, StyleNum, zPage);
end;

procedure T_Report.HorizLineFooter(SpBefore, SpAfter: single; ColNum: integer = 0; StyleNum: integer = 0);
begin
  DrawAHorizLine(Dim2Pixels(SpBefore), Dim2Pixels(SpAfter), ColNum, -1, StyleNum, zFooter);
end;

procedure T_Report.SpaceHeader(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
begin
  InsertSpace(-1, ColNum, Dim2Pixels(Verti), BkColorNum, zHeader);
end;

procedure T_Report.SpacePage(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
begin
  InsertSpace(-1, ColNum, Dim2Pixels(Verti), BkColorNum, zPage);
end;

procedure T_Report.SpaceFooter(Verti: single; ColNum: integer = 0; BkColorNum: integer = -1);
begin
  InsertSpace(-1, ColNum, Dim2Pixels(Verti), BkColorNum, zFooter);
end;

function T_Report.LineSpace(SpSup, SpInt, SpInf: single): integer;
var
  Sup, Int, Inf: integer;
begin
  if SpSup > 0 then
    Sup := Round(Dim2Pixels(SpSup))
  else
    Sup := 0;
  if SpInt > 0 then
    Int := Round(Dim2Pixels(SpInt))
  else
    Int := 0;
  if SpInf > 0 then
    Inf := Round(Dim2Pixels(SpInf))
  else
    Inf := 0;
  VLineSpace := T_LineSpace.Create(Sup, Int, Inf);
  Result := LineSpaces.Add(VLineSpace);
end;

procedure T_Report.BeginGroup(PageJump: Boolean = False);
begin
  VGroup := T_Group.Create;
  FGroup := True;
  if PageJump then
    Page;
end;

procedure T_Report.EndGroup(PageJump: Boolean = False);
begin
  T_Section(Sections[Pred(Sections.Count)]).LoadCmdGroupToPage;
  FGroup := False;
  VGroup.Free;
  if PageJump then
    Page;
end;

procedure T_Report.ColorColChange(ColNum: integer; ColColor: TfpgColor);
begin
  T_Column(Columns[ColNum]).SetColColor(ColColor);
end;

procedure T_Report.FrameMargins(AStyle: integer);
begin
  DrawAFrame(AStyle, 0, 0, 0, 0, zMargins);
end;

procedure T_Report.FrameHeader(AStyle: integer);
begin
  DrawAFrame(AStyle, 0, 0, 0, 0, zHeader);
end;

procedure T_Report.FramePage(AStyle: integer);
begin
  DrawAFrame(AStyle, 0, 0, 0, 0, zPage);
end;

procedure T_Report.FrameFooter(AStyle: integer);
begin
  DrawAFrame(AStyle, 0, 0, 0, 0, zFooter);
end;

procedure T_Report.LinePage(XBegin, YBegin, XEnd, YEnd: single; AStyle: integer);
begin
  DrawALine(Dim2Pixels(XBegin), Dim2Pixels(YBegin), Dim2Pixels(XEnd), Dim2Pixels(YEnd), AStyle);
end;

procedure T_Report.SurfPage(XLimits, YLimits: array of single; AColor: TfpgColor);
var
  Size, Cpt: integer;
  Ends: array of TRefPos;
begin
  if Length(XLimits) < Length(YLimits) then
    Size        := Length(XLimits)
  else if Length(XLimits) > Length(YLimits) then
    Size        := Length(YLimits)
  else
    Size        := Length(XLimits);
  SetLength(Ends, Size);
  for Cpt       := 0 to Pred(Size) do
  begin
    Ends[Cpt].X := Dim2Pixels(XLimits[Cpt]);
    Ends[Cpt].Y := Dim2Pixels(YLimits[Cpt]);
  end;
  PaintSurface(Ends, AColor);
end;

procedure T_Report.ImageHeader(Horiz, Verti: single; ImgFileName: string; ColNum, Scale: integer);
var
  RefImage: integer;
  Image: TfpgImage;
begin
  Horiz := Dim2Pixels(Horiz);
  Verti := Dim2Pixels(Verti);
  if FileExists(ImgFileName) then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      if Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'bmp' then
      begin
        Image := LoadImage_BMP(ImgFileName);
        Scale := 1;
      end;
      if (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'jpg') or (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 4) = 'jpeg') then
        Image := LoadImage_JPG(ImgFileName, Scale);
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zHeader);
  end
  else if fpgImages.GetImage(ImgFileName) <> nil then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      Image    := fpgImages.GetImage(ImgFileName);
      Scale    := 1;
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zPage);
  end
  else
    { TODO: localize this message }
    ShowMessage('Image ' + ImgFileName + ' is missing');
end;

procedure T_Report.ImagePage(Horiz, Verti: single; ImgFileName: string; ColNum, Scale: integer);
var
  RefImage: integer;
  Image, TempImage: TfpgImage;
begin
  Horiz := Dim2Pixels(Horiz);
  Verti := Dim2Pixels(Verti);
  if FileExists(ImgFileName) then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      if Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'bmp' then
      begin
        Image := LoadImage_BMP(ImgFileName);
        Scale := 1;
      end;
      if (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'jpg') or (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 4) = 'jpeg') then
        Image := LoadImage_JPG(ImgFileName, Scale);
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zPage);
  end
  else if fpgImages.GetImage(ImgFileName) <> nil then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      { This returns a refence to an existing image }
      TempImage := fpgImages.GetImage(ImgFileName);
      Scale := 1;
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      { Clone the image because we don't want to free fpGUI registered images,
        because they could be used by other widgets or areas of the application. }
      Image := TempImage.ImageFromSource;
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zPage);
  end
  else
    { TODO: localize this message }
    ShowMessage('Image ' + ImgFileName + ' is missing');
end;

procedure T_Report.ImageFooter(Horiz, Verti: single; ImgFileName: string; ColNum, Scale: integer);
var
  RefImage: integer;
  Image: TfpgImage;
begin
  Horiz := Dim2Pixels(Horiz);
  Verti := Dim2Pixels(Verti);
  if FileExists(ImgFileName) then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      if Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'bmp' then
      begin
        Image := LoadImage_BMP(ImgFileName);
        Scale := 1;
      end;
      if (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 3) = 'jpg') or (Copy(ImgFileName, Succ(Pos('.', ImgFileName)), 4) = 'jpeg') then
        Image := LoadImage_JPG(ImgFileName, Scale);
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zFooter);
  end
  else if fpgImages.GetImage(ImgFileName) <> nil then
  begin
    RefImage := ImageNames.IndexOf(IntToStr(Scale) + ImgFileName);
    if RefImage = -1 then
    begin
      Image    := fpgImages.GetImage(ImgFileName);
      Scale    := 1;
      RefImage := ImageNames.Add(IntToStr(Scale) + ImgFileName);
      Images.Add(Image);
    end;
    PaintImage(Horiz, Verti, ColNum, RefImage, zPage);
  end
  else
    { TODO: localize this message }
    ShowMessage('Image ' + ImgFileName + ' is missing');
end;

procedure T_Report.PrintPdf(Layout: TPageLayout; Zoom: string; Prefer: Boolean);
var
  Fd_SavePdf: TfpgFileDialog;
  PdfFile: string;
  PdfFileStream: TFileStream;
begin
  if T_Section(Sections[Pred(Sections.Count)]).TotPages = 0 then
  begin
    ShowMessage('There is no file to print');
    Exit;
  end;

  Fd_SavePdf          := TfpgFileDialog.Create(nil);
  Fd_SavePdf.InitialDir := fpgExtractFilePath(ParamStr(0));
  Fd_SavePdf.Filter   := 'PDF Documents |*.pdf';
  Fd_SavePdf.FileName := DefaultFile;
  try
    if Fd_SavePdf.RunSaveFile then
    begin
      PdfFile := Fd_SavePdf.FileName;
      if Lowercase(fpgExtractFileExt(PdfFile)) <> '.pdf' then
        PdfFile := PdfFile + '.pdf';
      Document := TPdfDocument.CreateDocument(Layout, Zoom, Prefer);
      with Document do
      begin
        PdfFileStream := TFileStream.Create(PdfFile, fmCreate);
        WriteDocument(PdfFileStream);
        PdfFileStream.Free;
        Free;
      end;
      { TODO: Create a cross-platform fpgViewFile() method or something }
      fpgOpenURL(PdfFile);
    end;
  finally
    Fd_SavePdf.Free;
  end;
end;

end.