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
|
{
<< Impressions >> U_Pdf.pas
Copyright (C) 2010 - JM.Levecque - <jmarc.levecque@jmlesite.fr>
This library is a free software coming as a add-on to fpGUI toolkit
See the copyright included in the fpGUI distribution for details about redistribution
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 produces the pdf file
}
unit U_Pdf;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StrUtils,
fpg_main, fpg_base, fpg_dialogs;
type
TPdfObjet = class(TObject)
private
protected
public
constructor Create; virtual;
destructor Destroy; override;
end;
TPdfBoolean = class(TPdfObjet)
private
FValue: Boolean;
protected
procedure WriteBoolean(const AFlux: TStream);
public
constructor CreateBoolean(const AValue: Boolean);
destructor Destroy; override;
end;
TPdfInteger = class(TPdfObjet)
private
FValue: Integer;
protected
procedure WriteInteger(const AFlux: TStream);
procedure IncrementeInteger;
property Value: Integer read FValue write FValue;
public
constructor CreateInteger(const AValue: Integer);
destructor Destroy; override;
end;
TPdfReference = class(TPdfObjet)
private
FValue: Integer;
protected
procedure WriteReference(const AFlux: TStream);
public
constructor CreateReference(const AValue: Integer);
destructor Destroy; override;
end;
TPdfName = class(TPdfObjet)
private
FValue: string;
protected
procedure WriteName(const AFlux: TStream);
public
constructor CreateName(const AValue: string);
destructor Destroy; override;
end;
TPdfString = class(TPdfObjet)
private
FValue: string;
protected
procedure WriteString(const AFlux: TStream);
public
constructor CreateString(const AValue: string);
destructor Destroy; override;
end;
TPdfArray = class(TPdfObjet)
private
FArray: TList;
protected
procedure WriteArray(const AFlux: TStream);
procedure AddItem(const AValue: TPdfObjet);
public
constructor CreateArray;
destructor Destroy; override;
end;
TPdfStream = class(TPdfObjet)
private
FStream: TList;
protected
procedure WriteStream(const AFlux: TStream);
procedure AddItem(const AValue: TPdfObjet);
public
constructor CreateStream;
destructor Destroy; override;
end;
TPdfFonte = class(TPdfObjet)
private
FTxtFont: Integer;
FTxtSize: string;
protected
procedure WriteFonte(const AFlux: TStream);
function WriteFonteStream(const FFlux: TMemoryStream; const AFlux: TStream): Int64;
public
constructor CreateFonte(const AFont: Integer; const ASize: string);
destructor Destroy; override;
end;
TPdfText = class(TPdfObjet)
private
FTxtPosX: Single;
FTxtPosY: Single;
FTxtText: TPdfString;
protected
procedure WriteText(const AFlux: TStream);
public
constructor CreateText(const APosX,APosY: Single; const AText: string);
destructor Destroy; override;
end;
TPdfLigne = class(TPdfObjet)
private
FEpais: Single;
FStaX: Single;
FStaY: Single;
FEndX: Single;
FEndY: Single;
protected
procedure WriteLigne(const AFlux: TStream);
public
constructor CreateLigne(const AEpais,AStaX,AStaY,AEndX,AEndY: Single);
destructor Destroy; override;
end;
TPdfRectangle = class(TPdfObjet)
private
FEpais: Single;
FRecX: Single;
FRecY: Single;
FRecW: Single;
FRecH: Single;
FFill: Boolean;
FStroke: Boolean;
protected
procedure WriteRectangle(const AFlux: TStream);
public
constructor CreateRectangle(const AEpais,APosX,APosY,AWidth,AHeight: Single; const AFill,AStroke: Boolean);
destructor Destroy; override;
end;
TRefPos= record
X: Single;
Y: Single;
end;
T_Points = array of TRefPos;
TPdfSurface = class(TPdfObjet)
private
FPoints: T_Points;
protected
procedure WriteSurface(const AFlux: TStream);
public
constructor CreateSurface(const APoints: T_Points);
destructor Destroy; override;
end;
TPdfImage = class(TPdfObjet)
private
FNumber: Integer;
FLeft: Single;
FBottom: Single;
FWidth: Integer;
FHeight: Integer;
protected
function WriteImageStream(const ANumber: Integer; AFlux: TStream): Int64;
procedure WriteImage(const AFlux: TStream);
public
constructor CreateImage(const ALeft,ABottom: Single; AWidth,AHeight,ANumber: Integer);
destructor Destroy; override;
end;
TPdfLineStyle = class(TPdfObjet)
private
FDash: TfpgLineStyle;
FPhase: Integer;
protected
procedure WriteLineStyle(const AFlux: TStream);
public
constructor CreateLineStyle(ADash: TfpgLineStyle; APhase: Integer);
destructor Destroy; override;
end;
TPdfColor = class(TPdfObjet)
private
FRed: string;
FGreen: string;
FBlue: string;
FStroke: Boolean;
protected
procedure WriteColor(const AFlux: TStream);
public
constructor CreateColor(const AStroke: Boolean; AColor: TfpgColor);
destructor Destroy; override;
end;
TPdfDicElement = class(TObject)
private
FKey: TPdfName;
FValue: TPdfObjet;
protected
procedure WriteDicElement(const AFlux: TStream);
public
constructor CreateDicElement(const AKey: string; const AValue: TPdfObjet);
destructor Destroy; override;
end;
TPdfDictionary = class(TPdfObjet)
private
FElement: TList; // list of TPdfDicElement
protected
procedure AddElement(const AKey: string; const AValue: TPdfObjet);
function ElementParCle(const AValue: string): Integer;
procedure WriteDictionary(const AObjet: Integer; const AFlux: TStream);
public
constructor CreateDictionary;
destructor Destroy; override;
end;
TPdfXRef = class(TObject)
private
FOffset: Integer;
FObjet: TPdfDictionary;
FStream: TPdfStream;
protected
procedure WriteXRef(const AFlux: TStream);
public
constructor CreateXRef;
destructor Destroy; override;
property Offset: Integer read FOffset write FOffset;
end;
TPageLayout= (lSingle,lTwo,lContinuous);
TPdfDocument = class(TObject)
private
FPreferences: Boolean;
FPageLayout: TPageLayout;
FZoomValue: string;
FXRefObjets: TList; // list of TPdfXRef
protected
function ElementParNom(const AValue: string): Integer;
procedure WriteXRefTable(const AFlux: TStream);
procedure WriteObjet(const AObjet: Integer; const AFlux: TStream);
procedure CreateRefTable;
procedure CreateTrailer;
function CreateCatalog: Integer;
procedure CreateInfo;
procedure CreatePreferences;
function CreatePages(Parent: Integer): Integer;
function CreatePage(Parent,Haut,Larg,PageNum: Integer): Integer;
function CreateOutlines: Integer;
function CreateOutline(Parent,SectNo,PageNo: Integer; SectTitre: string): Integer;
procedure CreateStdFont(NomFonte: string; NumFonte: Integer);
function LoadFont(NomFonte: string): string;
procedure CreateTtfFont(const NumFonte: Integer);
procedure CreateTp1Font(const NumFonte: Integer);
procedure CreateFontDescriptor(const NumFonte: Integer);
procedure CreateFontWidth;
procedure CreateFontFile(const NumFonte: Integer);
procedure CreateImage(ImgWidth,ImgHeight,NumImg: Integer);
function CreateContents: Integer;
procedure CreateStream(NumeroPage,PageNum: Integer);
public
constructor CreateDocument(const ALayout: TPageLayout= lSingle; const AZoom: string= '100'; const APreferences: Boolean= True);
destructor Destroy; override;
procedure WriteDocument(const AFlux: TStream);
property PageLayout: TPageLayout read FPageLayout write FPageLayout default lSingle;
end;
TFontDef = record
FType: string;
FName: string;
FAscent: string;
FDescent: string;
FCapHeight: string;
FFlags: string;
FFontBBox: string;
FItalicAngle: string;
FStemV: string;
FMissingWidth: string;
FEncoding: string;
FFile: string;
FOriginalSize: string;
FDiffs: widestring;
FCharWidth: widestring;
end;
const
CRLF= #13#10;
PDF_VERSION= '%PDF-1.3';
PDF_FILE_END= '%%EOF';
PDF_MAX_GEN_NUM= 65535;
PDF_UNICODE_HEADER = 'FEFF001B%s001B';
PDF_LANG_STRING = 'fr';
var
Document: TPdfDocument;
OldDecSeparator: Char;
Outline: Boolean;
FontDirectory: string;
implementation
uses
U_Report, U_Command;
var
Trailer: TPdfDictionary;
CurrentColor: string;
CurrentWidth: string;
Catalogue: Integer;
FontDef: TFontDef;
Flux: TMemoryStream;
FontFiles: array of string;
// utility functions
function InsertEscape(const AValue: string): string;
var
Chaine: string;
begin
Result:= '';
Chaine:= AValue;
if Pos('\',Chaine)> 0
then
Chaine:= AnsiReplaceStr(Chaine,'\','\\');
if Pos('(',Chaine)> 0
then
Chaine:= AnsiReplaceStr(Chaine,'(','\(');
if Pos(')',Chaine)> 0
then
Chaine:= AnsiReplaceStr(Chaine,')','\)');
Result:= Chaine;
//while Pos('\',Chaine)> 0 do
// begin
// Result:= Result+Copy(Chaine,1,Pred(Pos('\',Chaine)))+'\\';
// Chaine:= Copy(Chaine,Succ(Pos('\',Chaine)),Length(Chaine)-Pos('\',Chaine));
// end;
//Chaine:= Result+Chaine;
//Result:= '';
//while Pos('(',Chaine)> 0 do
// begin
// Result:= Result+Copy(Chaine,1,Pred(Pos('(',Chaine)))+'\(';
// Chaine:= Copy(Chaine,Succ(Pos('(',Chaine)),Length(Chaine)-Pos('(',Chaine));
// end;
//Chaine:= Result+Chaine;
//Result:= '';
//while Pos(')',Chaine)> 0 do
// begin
// Result:= Result+Copy(Chaine,1,Pred(Pos(')',Chaine)))+'\)';
// Chaine:= Copy(Chaine,Succ(Pos(')',Chaine)),Length(Chaine)-Pos(')',Chaine));
// end;
//Result:= Result+Chaine;
end;
procedure WriteChaine(const Valeur: string; AFlux: TStream);
begin
AFlux.Write(PChar(Valeur)^,Length(Valeur));
end;
function IntToChaine(const Valeur: Integer; const Long: Integer): string;
var
Chaine: string;
Cpt: Integer;
begin
Result:= '';
Chaine:= IntToStr(Valeur);
if Length(Chaine)< Long
then
for Cpt:= Succ(Length(Chaine)) to Long do
Result:= Result+'0';
Result:= Result+Chaine;
end;
function DateToPdfDate(const ADate: TDateTime): string;
begin
Result:= FormatDateTime('"D:"yyyymmddhhnnss',ADate);
end;
function ExtractBaseFontName(const AValue: string): string;
var
FontName,Chaine1,Chaine2: string;
begin
FontName:= Copy(AValue,1,Pred(Pos('-',AValue)));
if Pos(':',AValue)> 0
then
begin
Chaine1:= Copy(AValue,Succ(Pos(':',AValue)),Length(AValue)-Pos(':',AValue));
Chaine1:= Uppercase(Chaine1[1])+Copy(Chaine1,2,Pred(Length(Chaine1)));
if Pos(':',Chaine1)> 0
then
begin
Chaine2:= Copy(Chaine1,Succ(Pos(':',Chaine1)),Length(Chaine1)-Pos(':',Chaine1));
Chaine2:= Uppercase(Chaine2[1])+Copy(Chaine2,2,Pred(Length(Chaine2)));
Chaine1:= Copy(Chaine1,1,Pred(Pos(':',Chaine1)));
Chaine1:= Uppercase(Chaine1[1])+Copy(Chaine1,2,Pred(Length(Chaine1)));
Chaine1:= Chaine1+Chaine2;
end;
Chaine1:= '-'+Chaine1;
end;
Result:= FontName+Chaine1;
end;
// object methods
constructor TPdfObjet.Create;
begin
// to be implemented by descendents
end;
destructor TPdfObjet.Destroy;
begin
inherited;
end;
procedure TPdfBoolean.WriteBoolean(const AFlux: TStream);
begin
if FValue
then
WriteChaine('true',AFlux)
else
WriteChaine('false',AFlux);
end;
constructor TPdfBoolean.CreateBoolean(const AValue: Boolean);
begin
inherited Create;
FValue:= AValue;
end;
destructor TPdfBoolean.Destroy;
begin
inherited;
end;
procedure TPdfInteger.WriteInteger(const AFlux: TStream);
begin
WriteChaine(IntToStr(FValue), AFlux);
end;
procedure TPdfInteger.IncrementeInteger;
begin
FValue:= FValue+1;
end;
constructor TPdfInteger.CreateInteger(const AValue: Integer);
begin
inherited Create;
FValue:= AValue;
end;
destructor TPdfInteger.Destroy;
begin
inherited;
end;
procedure TPdfReference.WriteReference(const AFlux: TStream);
begin
WriteChaine(IntToStr(FValue)+' 0 R',AFlux);
end;
constructor TPdfReference.CreateReference(const AValue: Integer);
begin
inherited Create;
FValue:= AValue;
end;
destructor TPdfReference.Destroy;
begin
inherited;
end;
procedure TPdfName.WriteName(const AFlux: TStream);
begin
if FValue<> ''
then
if Pos('Length1',FValue)> 0
then
WriteChaine('/Length1',AFlux)
else
WriteChaine('/'+FValue,AFlux);
end;
constructor TPdfName.CreateName(const AValue: string);
begin
inherited Create;
FValue:= AValue;
end;
destructor TPdfName.Destroy;
begin
inherited;
end;
procedure TPdfString.WriteString(const AFlux: TStream);
begin
WriteChaine('('+Utf8ToAnsi(FValue)+')',AFlux);
end;
constructor TPdfString.CreateString(const AValue: string);
begin
inherited Create;
FValue:= AValue;
if (Pos('(',FValue)> 0) or (Pos(')',FValue)> 0) or (Pos('\',FValue)> 0)
then
FValue:= InsertEscape(FValue);
end;
destructor TPdfString.Destroy;
begin
inherited;
end;
procedure TPdfArray.WriteArray(const AFlux: TStream);
var
Cpt: Integer;
begin
WriteChaine('[',AFlux);
for Cpt:= 0 to Pred(FArray.Count) do
begin
if Cpt> 0
then
WriteChaine(' ',AFlux);
if TPdfObjet(FArray[Cpt]) is TPdfInteger
then
TPdfInteger(FArray[Cpt]).WriteInteger(AFlux);
if TPdfObjet(FArray[Cpt]) is TPdfReference
then
TPdfReference(FArray[Cpt]).WriteReference(AFlux);
if TPdfObjet(FArray[Cpt]) is TPdfName
then
TPdfName(FArray[Cpt]).WriteName(AFlux);
end;
WriteChaine(']',AFlux);
end;
procedure TPdfArray.AddItem(const AValue: TPdfObjet);
begin
FArray.Add(AValue);
end;
constructor TPdfArray.CreateArray;
begin
inherited Create;
FArray:= TList.Create;
end;
destructor TPdfArray.Destroy;
var
Cpt: Integer;
begin
if FArray.Count> 0
then
for Cpt:= 0 to Pred(FArray.Count) do
if TPdfObjet(FArray[Cpt]) is TPdfInteger
then
TPdfInteger(FArray[Cpt]).Free
else
if TPdfObjet(FArray[Cpt]) is TPdfReference
then
TPdfReference(FArray[Cpt]).Free
else
if TPdfObjet(FArray[Cpt]) is TPdfName
then
TPdfName(FArray[Cpt]).Free;
FArray.Free;
inherited;
end;
procedure TPdfStream.WriteStream(const AFlux: TStream);
var
Cpt: Integer;
begin
for Cpt:= 0 to Pred(FStream.Count) do
begin
if TPdfObjet(FStream[Cpt]) is TPdfFonte
then
TPdfFonte(FStream[Cpt]).WriteFonte(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfColor
then
TPdfColor(FStream[Cpt]).WriteColor(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfText
then
TPdfText(FStream[Cpt]).WriteText(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfRectangle
then
TPdfRectangle(FStream[Cpt]).WriteRectangle(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfLigne
then
TPdfLigne(FStream[Cpt]).WriteLigne(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfLineStyle
then
TPdfLineStyle(FStream[Cpt]).WriteLineStyle(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfSurface
then
TPdfSurface(FStream[Cpt]).WriteSurface(AFlux);
if TPdfObjet(FStream[Cpt]) is TPdfImage
then
TPdfImage(FStream[Cpt]).WriteImage(AFlux);
end;
end;
procedure TPdfStream.AddItem(const AValue: TPdfObjet);
begin
FStream.Add(AValue);
end;
constructor TPdfStream.CreateStream;
begin
inherited Create;
FStream:= TList.Create;
end;
destructor TPdfStream.Destroy;
var
Cpt: Integer;
begin
if FStream.Count> 0
then
for Cpt:= 0 to Pred(FStream.Count) do
if TPdfObjet(FStream[Cpt]) is TPdfFonte
then
TPdfFonte(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfColor
then
TPdfColor(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfText
then
TPdfText(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfRectangle
then
TPdfRectangle(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfLigne
then
TPdfLigne(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfLineStyle
then
TPdfLineStyle(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfSurface
then
TPdfSurface(FStream[Cpt]).Free
else
if TPdfObjet(FStream[Cpt]) is TPdfImage
then
TPdfImage(FStream[Cpt]).Free;
FStream.Free;
inherited;
end;
procedure TPdfFonte.WriteFonte(const AFlux: TStream);
begin
WriteChaine('/F'+IntToStr(FTxtFont)+' '+FTxtSize+' Tf'+CRLF,AFlux);
end;
function TPdfFonte.WriteFonteStream(const FFlux: TMemoryStream; const AFlux: TStream): Int64;
var
BeginFlux,EndFlux: Int64;
begin
WriteChaine(CRLF+'stream'+CRLF,AFlux);
BeginFlux:= AFlux.Position;
FFlux.SaveToStream(AFlux);
EndFlux:= AFlux.Position;
Result:= EndFlux-BeginFlux;
WriteChaine(CRLF,AFlux);
WriteChaine('endstream',AFlux);
end;
constructor TPdfFonte.CreateFonte(const AFont: Integer; const ASize: string);
begin
inherited Create;
FTxtFont:= AFont;
FTxtSize:= ASize;
end;
destructor TPdfFonte.Destroy;
begin
inherited;
end;
procedure TPdfText.WriteText(const AFlux: TStream);
begin
WriteChaine('BT'+CRLF,AFlux);
WriteChaine(FormatFloat('0.##',FTxtPosX)+' '+FormatFloat('0.##',FTxtPosY)+' Td'+CRLF,AFlux);
TPdfString(FTxtText).WriteString(AFlux);
WriteChaine(' Tj'+CRLF,AFlux);
WriteChaine('ET'+CRLF,AFlux);
end;
constructor TPdfText.CreateText(const APosX,APosY: Single; const AText: string);
begin
inherited Create;
FTxtPosX:= APosX;
FTxtPosY:= APosY;
FTxtText:= TPdfString.CreateString(AText);
end;
destructor TPdfText.Destroy;
begin
FTxtText.Free;
inherited;
end;
procedure TPdfLigne.WriteLigne(const AFlux: TStream);
begin
if (FormatFloat('0.##',FEpais)+' w')<> CurrentWidth
then
begin
WriteChaine('1 J'+CRLF,AFlux);
WriteChaine(FormatFloat('0.##',FEpais)+' w'+CRLF,AFlux);
CurrentWidth:= FormatFloat('0.##',FEpais)+' w';
end;
WriteChaine(FormatFloat('0.##',FStaX)+' '+FormatFloat('0.##',FStaY)+' m'+CRLF,AFlux);
WriteChaine(FormatFloat('0.##',FEndX)+' '+FormatFloat('0.##',FEndY)+' l'+CRLF,AFlux);
WriteChaine('S'+CRLF,AFlux);
end;
constructor TPdfLigne.CreateLigne(const AEpais,AStaX,AStaY,AEndX,AEndY: Single);
begin
inherited Create;
FEpais:= AEpais;
FStaX:= AStaX;
FStaY:= AStaY;
FEndX:= AEndX;
FEndY:= AEndY;
end;
destructor TPdfLigne.Destroy;
begin
inherited;
end;
procedure TPdfRectangle.WriteRectangle(const AFlux: TStream);
begin
if FStroke
then
if (FormatFloat('0.##',FEpais)+' w')<> CurrentWidth
then
begin
WriteChaine('1 J'+CRLF,AFlux);
WriteChaine(FormatFloat('0.##',FEpais)+' w'+CRLF,AFlux);
CurrentWidth:= FormatFloat('0.##',FEpais)+' w';
end;
WriteChaine(FormatFloat('0.##',FRecX)+' '+FormatFloat('0.##',FRecY)+' '+FormatFloat('0.##',FRecW)+' '+FormatFloat('0.##',FRecH)+' re'+CRLF,AFlux);
if FStroke
then
WriteChaine('S'+CRLF,AFlux);
if FFill
then
WriteChaine('f'+CRLF,AFlux);
end;
constructor TPdfRectangle.CreateRectangle(const AEpais,APosX,APosY,AWidth,AHeight: Single; const AFill,AStroke: Boolean);
begin
inherited Create;
FEpais:= AEpais;
FRecX:= APosX;
FRecY:= APosY;
FRecW:= AWidth;
FRecH:= AHeight;
FFill:= AFill;
FStroke:= AStroke;
end;
destructor TPdfRectangle.Destroy;
begin
inherited;
end;
procedure TPdfSurface.WriteSurface(const AFlux: TStream);
var
Cpt: Integer;
begin
WriteChaine(FormatFloat('0.##',FPoints[0].X)+' '+FormatFloat('0.##',FPoints[0].Y)+' m'+CRLF,AFlux);
for Cpt:= 1 to Pred(Length(FPoints)) do
WriteChaine(FormatFloat('0.##',FPoints[Cpt].X)+' '+FormatFloat('0.##',FPoints[Cpt].Y)+' l'+CRLF,AFlux);
WriteChaine('h'+CRLF,AFlux);
WriteChaine('f'+CRLF,AFlux);
end;
constructor TPdfSurface.CreateSurface(const APoints: T_Points);
begin
inherited Create;
FPoints:= APoints;
end;
destructor TPdfSurface.Destroy;
begin
inherited;
end;
function TPdfImage.WriteImageStream(const ANumber: Integer; AFlux: TStream): Int64;
var
CptW,CptH: Integer;
BeginFlux,EndFlux: Int64;
begin
WriteChaine(CRLF+'stream'+CRLF,AFlux);
BeginFlux:= AFlux.Position;
for CptH:= 0 to Pred(TfpgImage(Images[ANumber]).Height) do
for CptW:= 0 to Pred(TfpgImage(Images[ANumber]).Width) do
begin
AFlux.WriteByte(fpgGetRed(TfpgImage(Images[ANumber]).Colors[CptW,CptH]));
AFlux.WriteByte(fpgGetGreen(TfpgImage(Images[ANumber]).Colors[CptW,CptH]));
AFlux.WriteByte(fpgGetBlue(TfpgImage(Images[ANumber]).Colors[CptW,CptH]));
end;
EndFlux:= AFlux.Position;
Result:= EndFlux-BeginFlux;
WriteChaine(CRLF,AFlux);
WriteChaine('endstream',AFlux);
end;
procedure TPdfImage.WriteImage(const AFlux: TStream);
begin
WriteChaine('q'+CRLF,AFlux);
WriteChaine(IntToStr(FWidth)+' 0 0 '+IntToStr(FHeight)+' '+FormatFloat('0.##',FLeft)+' '
+FormatFloat('0.##',FBottom)+' cm'+CRLF,AFlux);
WriteChaine('/I'+IntToStr(FNumber)+' Do '+CRLF,AFlux);
WriteChaine('Q'+CRLF,AFlux);
end;
constructor TPdfImage.CreateImage(const ALeft,ABottom: Single; AWidth,AHeight,ANumber: Integer);
begin
inherited Create;
FNumber:= ANumber;
FLeft:= ALeft;
FBottom:= ABottom;
FWidth:= AWidth;
FHeight:= AHeight;
end;
destructor TPdfImage.Destroy;
begin
inherited;
end;
procedure TPdfLineStyle.WriteLineStyle(const AFlux: TStream);
begin
WriteChaine('[',AFlux);
case FDash of
lsDash:
WriteChaine('5 5',AFlux);
lsDot:
WriteChaine('2 2',AFlux);
lsDashDot:
WriteChaine('5 2 2 2',AFlux);
lsDashDotDot:
WriteChaine('5 2 2 2 2 2',AFlux);
end;
WriteChaine('] '+IntToStr(FPhase)+' d'+CRLF,AFlux);
end;
constructor TPdfLineStyle.CreateLineStyle(ADash: TfpgLineStyle; APhase: Integer);
begin
inherited Create;
FDash:= ADash;
FPhase:= APhase;
end;
destructor TPdfLineStyle.Destroy;
begin
inherited;
end;
procedure TPdfColor.WriteColor(const AFlux: TStream);
begin
if FStroke
then
begin
if (FRed+' '+FGreen+' '+FBlue+' rg')<> CurrentColor
then
begin
WriteChaine(FRed+' '+FGreen+' '+FBlue+' rg'+CRLF,AFlux);
CurrentColor:= FRed+' '+FGreen+' '+FBlue+' rg';
end;
end
else
if (FRed+' '+FGreen+' '+FBlue+' RG')<> CurrentColor
then
begin
WriteChaine(FRed+' '+FGreen+' '+FBlue+' RG'+CRLF,AFlux);
CurrentColor:= FRed+' '+FGreen+' '+FBlue+' RG';
end;
end;
constructor TPdfColor.CreateColor(const AStroke: Boolean; AColor: TfpgColor);
begin
inherited Create;
FBlue := FormatFloat('0.##', fpgGetBlue(AColor)/256);
FGreen := FormatFloat('0.##', fpgGetGreen(AColor)/256);
FRed := FormatFloat('0.##', fpgGetRed(AColor)/256);
FStroke := AStroke;
end;
destructor TPdfColor.Destroy;
begin
inherited
end;
procedure TPdfDicElement.WriteDicElement(const AFlux: TStream);
begin
FKey.WriteName(AFlux);
WriteChaine(' ',AFlux);
if FValue is TPdfBoolean
then
TPdfBoolean(FValue).WriteBoolean(AFlux);
if FValue is TPdfInteger
then
TPdfInteger(FValue).WriteInteger(AFlux);
if FValue is TPdfReference
then
TPdfReference(FValue).WriteReference(AFlux);
if FValue is TPdfName
then
TPdfName(FValue).WriteName(AFlux);
if FValue is TPdfString
then
TPdfString(FValue).WriteString(AFlux);
if FValue is TPdfArray
then
TPdfArray(FValue).WriteArray(AFlux);
if FValue is TPdfDictionary
then
TPdfDictionary(FValue).WriteDictionary(-1,AFlux);
WriteChaine(CRLF,AFlux);
end;
constructor TPdfDicElement.CreateDicElement(const AKey: string; const AValue: TPdfObjet);
begin
inherited Create;
FKey:= TPdfName.CreateName(AKey);
FValue:= AValue;
end;
destructor TPdfDicElement.Destroy;
begin
FKey.Free;
if FValue is TPdfBoolean
then
TPdfBoolean(FValue).Free
else
if FValue is TPdfDictionary
then
TPdfDictionary(FValue).Free
else
if FValue is TPdfInteger
then
TPdfInteger(FValue).Free
else
if FValue is TPdfName
then
TPdfName(FValue).Free
else
if FValue is TPdfReference
then
TPdfReference(FValue).Free
else
if FValue is TPdfString
then
TPdfString(FValue).Free
else
if FValue is TPdfArray
then
TPdfArray(FValue).Free;
inherited;
end;
procedure TPdfDictionary.AddElement(const AKey: string; const AValue: TPdfObjet);
var
DicElement: TPdfDicElement;
begin
DicElement:= TPdfDicElement.CreateDicElement(AKey,AValue);
FElement.Add(DicElement);
end;
function TPdfDictionary.ElementParCle(const AValue: string): Integer;
var
Cpt: Integer;
begin
Result:= -1;
for Cpt:= 0 to Pred(FElement.Count) do
if TPdfDicElement(FElement[Cpt]).FKey.FValue= AValue
then
begin
Result:= Cpt;
Exit;
end;
end;
procedure TPdfDictionary.WriteDictionary(const AObjet: Integer; const AFlux: TStream);
var
Long: TPdfInteger;
Cpt,NumImg,NumFnt: Integer;
Value: string;
begin
if TPdfName(TPdfDicElement(FElement[0]).FKey).FValue= ''
then
TPdfDicElement(FElement[0]).WriteDicElement(AFlux) // write a charwidth array of a font
else
begin
WriteChaine('<<'+CRLF,AFlux);
if FElement.Count> 0
then
for Cpt:= 0 to Pred(FElement.Count) do
TPdfDicElement(FElement[Cpt]).WriteDicElement(AFlux);
NumImg:= -1;
NumFnt:= -1;
if FElement.Count> 0
then
for Cpt:= 0 to Pred(FElement.Count) do
if AObjet> -1
then
begin
if (TPdfName(TPdfDicElement(FElement[Cpt]).FKey).FValue= 'Name')
then
if (TPdfObjet(TPdfDicElement(FElement[Cpt]).FValue) is TPdfName)
and (TPdfName(TPdfDicElement(FElement[Cpt]).FValue).FValue[1]= 'I')
then
begin
NumImg:= StrToInt(Copy(TPdfName(TPdfDicElement(FElement[Cpt]).FValue).FValue,2,Length(TPdfName(TPdfDicElement(FElement[Cpt]).FValue).FValue)-1));
Flux:= TMemoryStream.Create;
Flux.Position:= 0;
// write image stream length in xobject dictionary
Long:= TPdfInteger.CreateInteger(TPdfImage(TPdfXRef(Document.FXRefObjets[AObjet]).FObjet).WriteImageStream(NumImg,Flux));
TPdfDictionary(TPdfXRef(Document.FXRefObjets[AObjet]).FObjet).AddElement('Length',Long);
TPdfDicElement(FElement[Pred(FElement.Count)]).WriteDicElement(AFlux);
Flux.Free;
WriteChaine('>>',AFlux);
// write image stream in xobject dictionary
TPdfImage(TPdfXRef(Document.FXRefObjets[AObjet]).FObjet).WriteImageStream(NumImg,AFlux);
end;
if Pos('Length1',TPdfName(TPdfDicElement(FElement[Cpt]).FKey).FValue)> 0
then
begin
Flux:= TMemoryStream.Create;
Value:= TPdfName(TPdfDicElement(FElement[Cpt]).FKey).FValue;
NumFnt:= StrToInt(Copy(Value,Succ(Pos(' ',Value)),Length(Value)-Pos(' ',Value)));
Flux.LoadFromFile(FontFiles[NumFnt]);
// write fontfile stream length in xobject dictionary
Long:= TPdfInteger.CreateInteger(Flux.Size);
TPdfDictionary(TPdfXRef(Document.FXRefObjets[AObjet]).FObjet).AddElement('Length',Long);
TPdfDicElement(FElement[Pred(FElement.Count)]).WriteDicElement(AFlux);
WriteChaine('>>',AFlux);
// write fontfile stream in xobject dictionary
TPdfFonte(TPdfXRef(Document.FXRefObjets[NumFnt]).FObjet).WriteFonteStream(Flux,AFlux);
Flux.Free;
end;
end;
if (NumImg= -1) and (NumFnt= -1)
then
WriteChaine('>>',AFlux);
end;
end;
constructor TPdfDictionary.CreateDictionary;
begin
inherited Create;
FElement:= TList.Create;
end;
destructor TPdfDictionary.Destroy;
var
Cpt: integer;
begin
if FElement.Count> 0
then
for Cpt:= 0 to Pred(FElement.Count) do
TPdfDicElement(FElement[Cpt]).Free;
FElement.Free;
inherited;
end;
procedure TPdfXRef.WriteXRef(const AFlux: TStream);
begin
WriteChaine(IntToChaine(FOffset,10)+' '+IntToChaine(0,5)+' n'+CRLF,AFlux);
end;
constructor TPdfXRef.CreateXRef;
begin
inherited Create;
FOffset:= 0;
FObjet:= TpdfDictionary.CreateDictionary;
FStream:= nil;
end;
destructor TPdfXRef.Destroy;
begin
FObjet.Free;
FStream.Free;
inherited;
end;
function TPdfDocument.ElementParNom(const AValue: string): Integer;
var
Cpt: Integer;
begin
for Cpt:= 1 to Pred(FXRefObjets.Count) do
if TPdfName(TPdfDicElement(TPdfDictionary(TPdfXRef(FXRefObjets[Cpt]).FObjet).FElement[0]).FValue).FValue= AValue
then
Result:= Cpt;
end;
procedure TPdfDocument.WriteXRefTable(const AFlux: TStream);
var
Cpt: Integer;
begin
if FXRefObjets.Count> 1
then
for Cpt:= 1 to Pred(FXRefObjets.Count) do
TPdfXRef(FXRefObjets[Cpt]).WriteXRef(AFlux);
end;
procedure TPdfDocument.WriteObjet(const AObjet: Integer; const AFlux: TStream);
var
Long: TPdfInteger;
Flux: TMemoryStream;
begin
WriteChaine(IntToStr(AObjet)+' 0 obj'+CRLF,AFlux);
if TPdfXRef(FXRefObjets[AObjet]).FStream= nil
then
TPdfDictionary(TPdfXRef(FXRefObjets[AObjet]).FObjet).WriteDictionary(AObjet,AFlux)
else
begin
Flux:= TMemoryStream.Create;
Flux.Position:= 0;
CurrentColor:= '';
CurrentWidth:= '';
TPdfXRef(FXRefObjets[AObjet]).FStream.WriteStream(Flux);
// write stream length element in contents dictionary
Long:= TPdfInteger.CreateInteger(Flux.Size);
TPdfDictionary(TPdfXRef(FXRefObjets[AObjet]).FObjet).AddElement('Length',Long);
Flux.Free;
TPdfXRef(FXRefObjets[AObjet]).FObjet.WriteDictionary(-1,AFlux);
// write stream in contents dictionary
CurrentColor:= '';
CurrentWidth:= '';
WriteChaine(CRLF+'stream'+CRLF,AFlux);
TPdfXRef(FXRefObjets[AObjet]).FStream.WriteStream(AFlux);
WriteChaine('endstream',AFlux);
end;
WriteChaine(CRLF+'endobj'+CRLF+CRLF,AFlux);
end;
procedure TPdfDocument.CreateRefTable;
var
XRefObjet: TPdfXRef;
begin
FXRefObjets:= TList.Create;
// add first xref entry
XRefObjet:= TPdfXRef.CreateXRef;
FXRefObjets.Add(XRefObjet);
end;
procedure TPdfDocument.CreateTrailer;
var
XRefObjets: TPdfInteger;
begin
Trailer:= TPdfDictionary.CreateDictionary;
// add size trailer element
XRefObjets:= TPdfInteger.CreateInteger(FXRefObjets.Count);
Trailer.AddElement('Size',XRefObjets);
end;
function TPdfDocument.CreateCatalog: Integer;
var
Catalog: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Table: TPdfArray;
begin
// add xref entry
Catalog:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Catalog);
// add root trailer element
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Trailer.AddElement('Root',XRefObjets);
// add type element to catalog dictionary
Nom:= TPdfName.CreateName('Catalog');
Catalog.FObjet.AddElement('Type',Nom);
// add pagelayout element to catalog dictionary
case FPageLayout of
lSingle:
Nom:= TPdfName.CreateName('SinglePage');
lTwo:
Nom:= TPdfName.CreateName('TwoColumnLeft');
lContinuous:
Nom:= TPdfName.CreateName('OneColumn');
end;
Catalog.FObjet.AddElement('PageLayout',Nom);
// add openaction element to catalog dictionary
Table:= TPdfArray.CreateArray;
Catalog.FObjet.AddElement('OpenAction',Table);
Result:= Pred(FXRefObjets.Count);
end;
procedure TPdfDocument.CreateInfo;
var
Info: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfString;
begin
// add xref entry
Info:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Info);
// add info trailer element
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Trailer.AddElement('Info',XRefObjets);
TPdfInteger(TPdfDicElement(Trailer.FElement[Trailer.ElementParCle('Size')]).FValue).FValue:= FXRefObjets.Count;
// add title element to info dictionary
Nom:= TPdfString.CreateString(Infos.Titre);
Info.FObjet.AddElement('Title',Nom);
// add author element to info dictionary
Nom:= TPdfString.CreateString(Infos.Auteur);
Info.FObjet.AddElement('Author',Nom);
// add creator element to info dictionary
Nom:= TPdfString.CreateString(ApplicationName);
Info.FObjet.AddElement('Creator',Nom);
// add producer element to info dictionary
Nom:= TPdfString.CreateString('fpGUI/FPC');
Info.FObjet.AddElement('Producer',Nom);
// add creationdate element to info dictionary
Nom:= TPdfString.CreateString(DateToPdfDate(Now));
Info.FObjet.AddElement('CreationDate',Nom);
end;
procedure TPdfDocument.CreatePreferences;
var
Viewer: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Preference: TPdfBoolean;
begin
// add xref entry
Viewer:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Viewer);
// add type element to preferences dictionary
Nom:= TPdfName.CreateName('ViewerPreferences');
Viewer.FObjet.AddElement('Type',Nom);
// add preference element to preferences dictionary
Preference:= TPdfBoolean.CreateBoolean(True);
Viewer.FObjet.AddElement('FitWindow',Preference);
// add preferences reference to catalog dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[ElementParNom('Catalog')]).FObjet).AddElement('ViewerPreferences',XRefObjets)
end;
function TPdfDocument.CreatePages(Parent: Integer): Integer;
var
Pages: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Dictionaire: TPdfDictionary;
Table: TPdfArray;
Count: TPdfInteger;
begin
// add xref entry
Pages:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Pages);
// add type element to pages dictionary
Nom:= TPdfName.CreateName('Pages');
Pages.FObjet.AddElement('Type',Nom);
// add parent reference to pages dictionary if pages is not the root of the page tree
if Parent> 0
then
begin
XRefObjets:= TPdfReference.CreateReference(Parent);
Pages.FObjet.AddElement('Parent',XRefObjets);
// increment count in parent pages dictionary
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[Parent]).FObjet);
TPdfInteger(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Count')]).FValue).IncrementeInteger;
// add kid reference in parent pages dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Kids')]).FValue).AddItem(XRefObjets);
end
else
begin
// add pages reference to catalog dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[ElementParNom('Catalog')]).FObjet).AddElement('Pages',XRefObjets)
end;
// add kids element to pages dictionary
Table:= TPdfArray.CreateArray;
Pages.FObjet.AddElement('Kids',Table);
// add count element to pages dictionary
Count:= TPdfInteger.CreateInteger(0);
Pages.FObjet.AddElement('Count',Count);
Result:= Pred(FXRefObjets.Count);
end;
function TPdfDocument.CreatePage(Parent,Haut,Larg,PageNum: Integer): Integer;
var
Page: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Dictionaire: TPdfDictionary;
Table: TPdfArray;
Coord: TPdfInteger;
Cpt: Integer;
begin
// add xref entry
Page:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Page);
// add type element to page dictionary
Nom:= TPdfName.CreateName('Page');
Page.FObjet.AddElement('Type',Nom);
// add parent reference to page dictionary
XRefObjets:= TPdfReference.CreateReference(Parent);
Page.FObjet.AddElement('Parent',XRefObjets);
// increment count in parent pages dictionary
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[Parent]).FObjet);
TPdfInteger(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Count')]).FValue).IncrementeInteger;
// add kid reference in parent pages dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Kids')]).FValue).AddItem(XRefObjets);
// add mediabox element to page dictionary
Table:= TPdfArray.CreateArray;
Page.FObjet.AddElement('MediaBox',Table);
// add coordinates in page mediabox
Dictionaire:= TPdfDictionary(TPdfXRef(Page).FObjet);
Coord:= TPdfInteger.CreateInteger(0);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('MediaBox')]).FValue).AddItem(Coord);
Coord:= TPdfInteger.CreateInteger(0);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('MediaBox')]).FValue).AddItem(Coord);
Coord:= TPdfInteger.CreateInteger(Larg);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('MediaBox')]).FValue).AddItem(Coord);
Coord:= TPdfInteger.CreateInteger(Haut);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('MediaBox')]).FValue).AddItem(Coord);
// add resources element to page dictionary
Dictionaire:= TPdfDictionary.CreateDictionary;
Page.FObjet.AddElement('Resources',Dictionaire);
// add procset element in resources element to page dictionary
Table:= TPdfArray.CreateArray;
TPdfDictionary(TPdfDicElement(Page.FObjet.FElement[Pred(Page.FObjet.FElement.Count)]).FValue).AddElement('ProcSet',Table);
// add font element in resources element to page dictionary
if Fonts.Count> 0
then
begin
Dictionaire:= TPdfDictionary.CreateDictionary;
TPdfDictionary(TPdfDicElement(Page.FObjet.FElement[Pred(Page.FObjet.FElement.Count)]).FValue).AddElement('Font',Dictionaire);
end;
for Cpt:= 0 to Pred(PdfPage.Count) do
if TPdfElement(PdfPage[Cpt]) is TPdfImg
then
if TPdfImg(PdfPage[Cpt]).PageId= PageNum
then
begin
// add xobject element in resources element to page dictionary
Dictionaire:= TPdfDictionary.CreateDictionary;
TPdfDictionary(TPdfDicElement(Page.FObjet.FElement[Pred(Page.FObjet.FElement.Count)]).FValue).AddElement('XObject',Dictionaire);
Break;
end;
// add pdf element in procset array to page dictionary
Dictionaire:= TPdfDictionary(TPdfDicElement(Page.FObjet.FElement[Pred(Page.FObjet.FElement.Count)]).FValue);
Nom:= TPdfName.CreateName('PDF');
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('ProcSet')]).FValue).AddItem(Nom);
// add text element in procset array to page dictionary
Nom:= TPdfName.CreateName('Text');
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('ProcSet')]).FValue).AddItem(Nom);
// add image element in procset array to page dictionary
Nom:= TPdfName.CreateName('ImageC');
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('ProcSet')]).FValue).AddItem(Nom);
Result:= Pred(FXRefObjets.Count);
end;
function TPdfDocument.CreateOutlines: Integer;
var
Outlines: TPdfXRef;
Nom: TPdfName;
Count: TPdfInteger;
begin
// add xref entry
Outlines:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Outlines);
// add type element to outlines dictionary
Nom:= TPdfName.CreateName('Outlines');
Outlines.FObjet.AddElement('Type',Nom);
// add count element to outlines dictionary
Count:= TPdfInteger.CreateInteger(0);
Outlines.FObjet.AddElement('Count',Count);
Result:= Pred(FXRefObjets.Count);
end;
function TPdfDocument.CreateOutline(Parent,SectNo,PageNo: Integer; SectTitre: string): Integer;
var
Outline: TPdfXRef;
XRefObjets: TPdfReference;
Titre: TPdfString;
Count: TPdfInteger;
Table: TPdfArray;
begin
// add xref entry
Outline:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Outline);
// add title element to outline dictionary
if PageNo> -1
then
if SectTitre<> ''
then
Titre:= TPdfString.CreateString(SectTitre+' Page '+IntToStr(PageNo))
else
Titre:= TPdfString.CreateString('Section '+IntToStr(SectNo)+' Page '+IntToStr(PageNo))
else
if SectTitre<> ''
then
Titre:= TPdfString.CreateString(SectTitre)
else
Titre:= TPdfString.CreateString('Section '+IntToStr(SectNo));
Outline.FObjet.AddElement('Title',Titre);
// add parent reference to outline dictionary
XRefObjets:= TPdfReference.CreateReference(Parent);
Outline.FObjet.AddElement('Parent',XRefObjets);
// add count element to outline dictionary
Count:= TPdfInteger.CreateInteger(0);
Outline.FObjet.AddElement('Count',Count);
// add dest element to outline dictionary
Table:= TPdfArray.CreateArray;
Outline.FObjet.AddElement('Dest',Table);
Result:= Pred(FXRefObjets.Count);
end;
procedure TPdfDocument.CreateStdFont(NomFonte: string; NumFonte: Integer);
var
Fontes: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Dictionaire: TPdfDictionary;
Cpt: Integer;
begin
if Pos('Italic',NomFonte)> 0
then
NomFonte:= Copy(NomFonte,1,Pred(Pos('Italic',NomFonte)))+'Oblique';
// AnsiReplaceText(NomFonte,'Italic','Oblique');
// add xref entry
Fontes:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Fontes);
// add type element to font dictionary
Nom:= TPdfName.CreateName('Font');
Fontes.FObjet.AddElement('Type',Nom);
// add subtype element to font dictionary
Nom:= TPdfName.CreateName('Type1');
Fontes.FObjet.AddElement('Subtype',Nom);
// add encoding element to font dictionary
Nom:= TPdfName.CreateName('WinAnsiEncoding');
Fontes.FObjet.AddElement('Encoding',Nom);
// add firstchar element to font dictionary
Nom:= TPdfName.CreateName('32');
//Nom:= TPdfName.CreateName('0');
Fontes.FObjet.AddElement('FirstChar',Nom);
// add lastchar element to font dictionary
Nom:= TPdfName.CreateName('255');
Fontes.FObjet.AddElement('LastChar',Nom);
// add basefont element to font dictionary
Nom:= TPdfName.CreateName(NomFonte);
Fontes.FObjet.AddElement('BaseFont',Nom);
// add name element to font dictionary
Nom:= TPdfName.CreateName('F'+IntToStr(NumFonte));
Fontes.FObjet.AddElement('Name',Nom);
// add font reference to all page dictionary
for Cpt:= 1 to Pred(FXRefObjets.Count) do
begin
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[Cpt]).FObjet);
if Dictionaire.FElement.Count> 0
then
if TPdfName(TPdfDicElement(Dictionaire.FElement[0]).FValue).FValue= 'Page'
then
begin
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Resources')]).FValue);
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Font')]).FValue);
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Dictionaire.AddElement(TPdfName(Nom).FValue,XRefObjets);
end;
end;
SetLength(FontFiles,Succ(Length(FontFiles)));
FontFiles[NumFonte]:= '';
end;
function TPdfDocument.LoadFont(NomFonte: string): string;
var
FileTxt: TextFile;
Ligne: widestring;
begin
if FileExists(FontDirectory+NomFonte+'.fnt')
then
begin
AssignFile(FileTxt,FontDirectory+NomFonte+'.fnt');
Reset(FileTxt);
while not Eof(FileTxt) do
begin
Readln(FileTxt,Ligne);
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'FontType'
then
FontDef.FType:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'FontName'
then
FontDef.FName:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'Ascent'
then
FontDef.FAscent:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'Descent'
then
FontDef.FDescent:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'CapHeight'
then
FontDef.FCapHeight:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'Flags'
then
FontDef.FFlags:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'FontBBox'
then
FontDef.FFontBBox:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'ItalicAngle'
then
FontDef.FItalicAngle:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'StemV'
then
FontDef.FStemV:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'MissingWidth'
then
FontDef.FMissingWidth:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'Encoding'
then
FontDef.FEncoding:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'FontFile'
then
FontDef.FFile:= FontDirectory+Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'OriginalSize'
then
FontDef.FOriginalSize:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'Diffs'
then
FontDef.FDiffs:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
if Copy(Ligne,1,Pred(Pos('=',Ligne)))= 'CharWidth'
then
FontDef.FCharWidth:= Copy(Ligne,Succ(Pos('=',Ligne)),Length(Ligne)-Pos('=',Ligne));
end;
Result:= FontDef.FType;
end
else
ShowMessage('Font file '+NomFonte+'.fnt not found');
end;
procedure TPdfDocument.CreateTtfFont(const NumFonte: Integer);
var
Fontes: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Dictionaire: TPdfDictionary;
Value: TPdfInteger;
Cpt: Integer;
begin
// add xref entry
Fontes:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Fontes);
// add type element to font dictionary
Nom:= TPdfName.CreateName('Font');
Fontes.FObjet.AddElement('Type',Nom);
// add subtype element to font dictionary
Nom:= TPdfName.CreateName(FontDef.FType);
Fontes.FObjet.AddElement('Subtype',Nom);
// add encoding element to font dictionary
Nom:= TPdfName.CreateName('WinAnsiEncoding');
Fontes.FObjet.AddElement('Encoding',Nom);
// add firstchar element to font dictionary
Value:= TPdfInteger.CreateInteger(32);
Fontes.FObjet.AddElement('FirstChar',Value);
// add lastchar element to font dictionary
Value:= TPdfInteger.CreateInteger(255);
Fontes.FObjet.AddElement('LastChar',Value);
// add basefont element to font dictionary
Nom:= TPdfName.CreateName(FontDef.FName);
Fontes.FObjet.AddElement('BaseFont',Nom);
// add name element to font dictionary
Nom:= TPdfName.CreateName('F'+IntToStr(NumFonte));
Fontes.FObjet.AddElement('Name',Nom);
// add font reference to all page dictionary
for Cpt:= 1 to Pred(FXRefObjets.Count) do
begin
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[Cpt]).FObjet);
if Dictionaire.FElement.Count> 0
then
if TPdfName(TPdfDicElement(Dictionaire.FElement[0]).FValue).FValue= 'Page'
then
begin
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Resources')]).FValue);
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Font')]).FValue);
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Dictionaire.AddElement(TPdfName(Nom).FValue,XRefObjets);
end;
end;
CreateFontDescriptor(NumFonte);
// add fontdescriptor reference to font dictionary
XRefObjets:= TPdfReference.CreateReference(FXRefObjets.Count-2);
Fontes.FObjet.AddElement('FontDescriptor',XRefObjets);
CreateFontWidth;
// add fontwidth reference to font dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Fontes.FObjet.AddElement('Widths',XRefObjets);
SetLength(FontFiles,Succ(Length(FontFiles)));
FontFiles[NumFonte]:= FontDef.FFile;
end;
procedure TPdfDocument.CreateTp1Font(const NumFonte: Integer);
begin
end;
procedure TPdfDocument.CreateFontDescriptor(const NumFonte: Integer);
var
FtDesc: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Value: TPdfInteger;
Table: TPdfArray;
Dictionaire: TPdfDictionary;
begin
// add xref entry
FtDesc:= TPdfXRef.CreateXRef;
FXRefObjets.Add(FtDesc);
// add type element to fontdescriptor dictionary
Nom:= TPdfName.CreateName('FontDescriptor');
FtDesc.FObjet.AddElement('Type',Nom);
// add fontname element to fontdescriptor dictionary
Nom:= TPdfName.CreateName(FontDef.FName);
FtDesc.FObjet.AddElement('FontName',Nom);
// add ascent element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FAscent));
FtDesc.FObjet.AddElement('Ascent',Value);
// add descent element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FDescent));
FtDesc.FObjet.AddElement('Descent',Value);
// add capheight element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FCapHeight));
FtDesc.FObjet.AddElement('CapHeight',Value);
// add flags element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FFlags));
FtDesc.FObjet.AddElement('Flags',Value);
// add fontbbox element to fontdescriptor dictionary
Table:= TPdfArray.CreateArray;
FtDesc.FObjet.AddElement('FontBBox',Table);
// add coordinates in page fontbbox
while Pos(' ',FontDef.FFontBBox)> 0 do
begin
Dictionaire:= TPdfDictionary(TPdfXRef(FtDesc).FObjet);
Value:= TPdfInteger.CreateInteger(StrToInt(Copy(FontDef.FFontBBox,1,Pred(Pos(' ',FontDef.FFontBBox)))));
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('FontBBox')]).FValue).AddItem(Value);
FontDef.FFontBBox:= Copy(FontDef.FFontBBox,Succ(Pos(' ',FontDef.FFontBBox)),Length(FontDef.FFontBBox)-Pos(' ',FontDef.FFontBBox));;
end;
// add italicangle element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FItalicAngle));
FtDesc.FObjet.AddElement('ItalicAngle',Value);
// add stemv element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FStemV));
FtDesc.FObjet.AddElement('StemV',Value);
// add missingwidth element to fontdescriptor dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FMissingWidth));
FtDesc.FObjet.AddElement('MissingWidth',Value);
CreateFontFile(NumFonte);
// add fontfilereference to fontdescriptor dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
FtDesc.FObjet.AddElement('FontFile2',XRefObjets);
end;
procedure TPdfDocument.CreateFontWidth;
var
FtDesc: TPdfXRef;
XRefObjets: TPdfReference;
Value: TPdfInteger;
Table: TPdfArray;
Dictionaire: TPdfDictionary;
begin
// add xref entry
FtDesc:= TPdfXRef.CreateXRef;
FXRefObjets.Add(FtDesc);
// add element to fontwidth dictionary
Table:= TPdfArray.CreateArray;
FtDesc.FObjet.AddElement('',Table);
// add width values in fontwidth array
while Pos(' ',FontDef.FCharWidth)> 0 do
begin
Dictionaire:= TPdfDictionary(TPdfXRef(FtDesc).FObjet);
Value:= TPdfInteger.CreateInteger(StrToInt(Copy(FontDef.FCharWidth,1,Pred(Pos(' ',FontDef.FCharWidth)))));
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('')]).FValue).AddItem(Value);
FontDef.FCharWidth:= Copy(FontDef.FCharWidth,Succ(Pos(' ',FontDef.FCharWidth)),Length(FontDef.FCharWidth)-Pos(' ',FontDef.FCharWidth));;
end;
end;
procedure TPdfDocument.CreateFontFile(const NumFonte: Integer);
var
FtDesc: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Value: TPdfInteger;
begin
// add xref entry
FtDesc:= TPdfXRef.CreateXRef;
FXRefObjets.Add(FtDesc);
// add filter element to fontfile dictionary
Nom:= TPdfName.CreateName('FlateDecode');
FtDesc.FObjet.AddElement('Filter',Nom);
// add length1 element to fontfile dictionary
Value:= TPdfInteger.CreateInteger(StrToInt(FontDef.FOriginalSize));
FtDesc.FObjet.AddElement('Length1 '+IntToStr(NumFonte),Value);
end;
procedure TPdfDocument.CreateImage(ImgWidth,ImgHeight,NumImg: Integer);
var
Images: TPdfXRef;
XRefObjets: TPdfReference;
Nom: TPdfName;
Dictionaire: TPdfDictionary;
Long: TPdfInteger;
Cpt: Integer;
begin
// add xref entry
Images:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Images);
// add type element to image dictionary
Nom:= TPdfName.CreateName('XObject');
Images.FObjet.AddElement('Type',Nom);
// add subtype element to image dictionary
Nom:= TPdfName.CreateName('Image');
Images.FObjet.AddElement('Subtype',Nom);
// add width element to image dictionary
Long:= TPdfInteger.CreateInteger(ImgWidth);
Images.FObjet.AddElement('Width',Long);
// add height element to image dictionary
Long:= TPdfInteger.CreateInteger(ImgHeight);
Images.FObjet.AddElement('Height',Long);
// add color space element to image dictionary
Nom:= TPdfName.CreateName('DeviceRGB');
Images.FObjet.AddElement('ColorSpace',Nom);
// add bits per component element to image dictionary
Long:= TPdfInteger.CreateInteger(8);
Images.FObjet.AddElement('BitsPerComponent',Long);
// add name element to image dictionary
Nom:= TPdfName.CreateName('I'+IntToStr(NumImg));
Images.FObjet.AddElement('Name',Nom);
// add image reference to page dictionary
for Cpt:= 1 to Pred(FXRefObjets.Count) do
begin
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[Cpt]).FObjet);
if Dictionaire.FElement.Count> 0
then
if TPdfName(TPdfDicElement(Dictionaire.FElement[0]).FValue).FValue= 'Page'
then
begin
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Resources')]).FValue);
if Dictionaire.ElementParCle('XObject')> -1
then
begin
Dictionaire:= TPdfDictionary(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('XObject')]).FValue);
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Dictionaire.AddElement(TPdfName(Nom).FValue,XRefObjets);
end;
end;
end;
end;
function TPdfDocument.CreateContents: Integer;
var
Contents: TPdfXRef;
XRefObjets: TPdfReference;
Stream: TPdfStream;
begin
// add xref entry
Contents:= TPdfXRef.CreateXRef;
FXRefObjets.Add(Contents);
Stream:= TPdfStream.CreateStream;
TPdfXRef(FXRefObjets[Pred(FXRefObjets.Count)]).FStream:= Stream;
// add contents reference to page dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[Pred(Pred(FXRefObjets.Count))]).FObjet).AddElement('Contents',XRefObjets);
Result:= Pred(FXRefObjets.Count);
end;
procedure TPdfDocument.CreateStream(NumeroPage,PageNum: Integer);
var
Cpt: Integer;
Txt: TPdfText;
Clr: TPdfColor;
Fnt: TPdfFonte;
Rct: TPdfRectangle;
Lin: TPdfLigne;
Srf: TPdfSurface;
Sty: TPdfLineStyle;
Img: TPdfImage;
begin
for Cpt:= 0 to Pred(PdfPage.Count) do
begin
if TPdfElement(PdfPage[Cpt]) is TPdfTexte
then
if TPdfTexte(PdfPage[Cpt]).PageId= NumeroPage
then
with TPdfTexte(PdfPage[Cpt]) do
begin
if FontName> -1
then
begin
Fnt:= TPdfFonte.CreateFonte(FontName,FontSize);
// adjust font size to display device
Fnt.FTxtSize:= IntToStr(Round((StrToInt(FontSize)*fpgApplication.Screen_dpi_y) div 72));
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Fnt);
Clr:= TPdfColor.CreateColor(True,Couleur);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Clr);
end;
Txt:= TPdfText.CreateText(TextPosX,TextPosY,Writting);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Txt);
end;
if TPdfElement(PdfPage[Cpt]) is TPdfRect
then
if TPdfRect(PdfPage[Cpt]).PageId= NumeroPage
then
with TPdfRect(PdfPage[Cpt]) do
begin
Clr:= TPdfColor.CreateColor(True,RectColor);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Clr);
if RectStroke
then
begin
Sty:= TPdfLineStyle.CreateLineStyle(RectLineStyle,0);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Sty);
end;
Rct:= TPdfRectangle.CreateRectangle(RectThickness,RectLeft,RectBottom,RectWidth,RectHeight,RectFill,RectStroke);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Rct);
end;
if TPdfElement(PdfPage[Cpt]) is TPdfLine
then
if TPdfLine(PdfPage[Cpt]).PageId= NumeroPage
then
with TPdfLine(PdfPage[Cpt]) do
begin
Clr:= TPdfColor.CreateColor(False,LineColor);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Clr);
Sty:= TPdfLineStyle.CreateLineStyle(LineStyle,0);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Sty);
Lin:= TPdfLigne.CreateLigne(LineThikness,LineBeginX,LineBeginY,LineEndX,LineEndY);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Lin);
end;
if TPdfElement(PdfPage[Cpt]) is TPdfSurf
then
if TPdfSurf(PdfPage[Cpt]).PageId= NumeroPage
then
with TPdfSurf(PdfPage[Cpt]) do
begin
Clr:= TPdfColor.CreateColor(True,SurfColor);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Clr);
Srf:= TPdfSurface.CreateSurface(Points);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Srf);
end;
if TPdfElement(PdfPage[Cpt]) is TPdfImg
then
if TPdfImg(PdfPage[Cpt]).PageId= NumeroPage
then
with TPdfImg(PdfPage[Cpt]) do
begin
Img:= TPdfImage.CreateImage(ImgLeft,ImgBottom,ImgWidth,ImgHeight,ImgNumber);
TPdfStream(TPdfXRef(FXRefObjets[PageNum]).FStream).AddItem(Img);
end;
end;
end;
constructor TPdfDocument.CreateDocument(const ALayout: TPageLayout; const AZoom: string; const APreferences: Boolean);
var
Cpt,CptSect,CptPage,NumFont,TreeRoot,ParentPage,PageNum,NumPage: Integer;
OutlineRoot,ParentOutline,PageOutline,NextOutline,NextSect,NewPage,PrevOutline,PrevSect: Integer;
Dictionaire: TPdfDictionary;
XRefObjets: TPdfReference;
Nom: TPdfName;
FontName,FtName: string;
begin
inherited Create;
FPreferences:= APreferences;
FPageLayout:= ALayout;
FZoomValue:= AZoom;
CreateRefTable;
CreateTrailer;
Catalogue:= CreateCatalog;
CreateInfo;
CreatePreferences;
ParentPage:= 0;
ParentOutline:= 0;
if Sections.Count> 1
then
begin
if Outline
then
begin
OutlineRoot:= CreateOutlines;
// add outline reference to catalog dictionary
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[Catalogue]).FObjet).AddElement('Outlines',XRefObjets);
// add useoutline element to catalog dictionary
Nom:= TPdfName.CreateName('UseOutlines');
TPdfDictionary(TPdfXRef(FXRefObjets[Catalogue]).FObjet).AddElement('PageMode',Nom);
end;
TreeRoot:= CreatePages(ParentPage);
end;
NumPage:= 0; // page number identical to the call to PrintPage
for CptSect:= 0 to Pred(Sections.Count) do
begin
if Sections.Count> 1
then
begin
if Outline
then
begin
ParentOutline:= CreateOutline(OutlineRoot,Succ(CptSect),-1,T_Section(Sections[CptSect]).Title);
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[OutlineRoot]).FObjet);
TPdfInteger(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Count')]).FValue).IncrementeInteger;
if CptSect= 0
then
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[OutlineRoot]).FObjet).AddElement('First',XRefObjets);
NextSect:= ParentOutline;
PrevSect:= Pred(FXRefObjets.Count);
end
else
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[NextSect]).FObjet).AddElement('Next',XRefObjets);
XRefObjets:= TPdfReference.CreateReference(PrevSect);
TPdfDictionary(TPdfXRef(FXRefObjets[ParentOutline]).FObjet).AddElement('Prev',XRefObjets);
NextSect:= ParentOutline;
if CptSect< Pred(Sections.Count)
then
PrevSect:= Pred(FXRefObjets.Count);
end;
if CptSect= Pred(Sections.Count)
then
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[OutlineRoot]).FObjet).AddElement('Last',XRefObjets);
end;
end;
ParentPage:= CreatePages(TreeRoot);
end
else
ParentPage:= CreatePages(ParentPage);
for CptPage:= 0 to Pred(T_Section(Sections[CptSect]).Pages.Count) do
begin
with T_Section(Sections[CptSect]) do
NewPage:= CreatePage(ParentPage,Paper.H,Paper.W,Succ(NumPage));
// add zoom factor to catalog dictionary
if (CptSect= 0) and (CptPage= 0)
then
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[ElementParNom('Catalog')]).FObjet);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('OpenAction')]).FValue).AddItem(XRefObjets);
Nom:= TPdfName.CreateName('XYZ null null '+FormatFloat('0.##',StrToInt(FZoomValue)/100));
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('OpenAction')]).FValue).AddItem(Nom);
end;
Inc(NumPage);
PageNum:= CreateContents; // pagenum = object number in the pdf file
CreateStream(NumPage,PageNum);
if (Sections.Count> 1) and Outline
then
begin
PageOutline:= CreateOutline(ParentOutline,Succ(CptSect),Succ(Cptpage),T_Section(Sections[CptSect]).Title);
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[ParentOutline]).FObjet);
TPdfInteger(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Count')]).FValue).IncrementeInteger;
// add page reference to outline destination
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[PageOutline]).FObjet);
XRefObjets:= TPdfReference.CreateReference(NewPage);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Dest')]).FValue).AddItem(XRefObjets);
// add display control name to outline destination
Nom:= TPdfName.CreateName('Fit');
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Dest')]).FValue).AddItem(Nom);
if CptPage= 0
then
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[ParentOutline]).FObjet).AddElement('First',XRefObjets);
NextOutline:= PageOutline;
PrevOutline:= Pred(FXRefObjets.Count);
// add page reference to parent outline destination
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[ParentOutline]).FObjet);
XRefObjets:= TPdfReference.CreateReference(NewPage);
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Dest')]).FValue).AddItem(XRefObjets);
// add display control name to outline destination
Nom:= TPdfName.CreateName('Fit');
TPdfArray(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Dest')]).FValue).AddItem(Nom);
end
else
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[NextOutline]).FObjet).AddElement('Next',XRefObjets);
XRefObjets:= TPdfReference.CreateReference(PrevOutline);
TPdfDictionary(TPdfXRef(FXRefObjets[PageOutline]).FObjet).AddElement('Prev',XRefObjets);
NextOutline:= PageOutline;
if CptPage< Pred(T_Section(Sections[CptSect]).Pages.Count)
then
PrevOutline:= Pred(FXRefObjets.Count);
end;
if CptPage= Pred(T_Section(Sections[CptSect]).Pages.Count)
then
begin
XRefObjets:= TPdfReference.CreateReference(Pred(FXRefObjets.Count));
TPdfDictionary(TPdfXRef(FXRefObjets[ParentOutline]).FObjet).AddElement('Last',XRefObjets);
end;
end;
end;
end;
if Sections.Count> 1
then
begin
// update count in root parent pages dictionary
Dictionaire:= TPdfDictionary(TPdfXRef(FXRefObjets[TreeRoot]).FObjet);
TPdfInteger(TPdfDicElement(Dictionaire.FElement[Dictionaire.ElementParCle('Count')]).FValue).Value:= T_Section(Sections[CptSect]).TotPages;
end;
if FontDirectory= ''
then
FontDirectory:= ExtractFilePath(Paramstr(0));
// select the font type
NumFont:= 0;
if Fonts.Count> 0
then
for Cpt:= 0 to Pred(Fonts.Count) do
begin
FontName:= ExtractBaseFontName(T_Font(Fonts[Cpt]).GetFont.FontDesc);
if Pos('-',FontName)> 0
then
FtName:= Copy(FontName,1,Pred(Pos('-',FontName)))
else
FtName:= FontName;
if (Lowercase(FtName)= 'courier') or (Lowercase(FtName)= 'helvetica') or (Lowercase(FtName)= 'times')
then
begin
FontName:= Uppercase(FontName[1])+Copy(FontName,2,Pred(Length(FontName)));
CreateStdFont(FontName,NumFont);
end
else
if LoadFont(FontName)= 'TrueType'
then
CreateTtfFont(NumFont)
else
CreateTp1Font(NumFont); // not implemented yet
Inc(NumFont);
end;
if Images.Count> 0
then
for Cpt:= 0 to Pred(Images.Count) do
CreateImage(TfpgImage(Images[Cpt]).Width,TfpgImage(Images[Cpt]).Height,Cpt);
TPdfInteger(TPdfDicElement(Trailer.FElement[Trailer.ElementParCle('Size')]).FValue).FValue:= FXRefObjets.Count;
end;
destructor TPdfDocument.Destroy;
var
Cpt: Integer;
begin
Trailer.Free;
if FXRefObjets.Count> 0
then
for Cpt:= 0 to Pred(FXRefObjets.Count) do
TPdfXRef(FXRefObjets[Cpt]).Free;
FXRefObjets.Free;
inherited;
end;
procedure TPdfDocument.WriteDocument(const AFlux: TStream);
var
Cpt,XRefPos: Integer;
begin
AFlux.Position:= 0;
WriteChaine(PDF_VERSION+CRLF,AFlux);
// write numbered indirect objects
for Cpt:= 1 to Pred(FXRefObjets.Count) do
begin
XRefPos:= AFlux.Position;
WriteObjet(Cpt,AFlux);
TPdfXRef(FXRefObjets[Cpt]).Offset:= XRefPos;
end;
XRefPos:= AFlux.Position;
// write xref table
WriteChaine('xref'+CRLF+'0 '+IntToStr(FXRefObjets.Count)+CRLF,AFlux);
with TPdfXRef(FXRefObjets[0]) do
WriteChaine(IntToChaine(Offset,10)+' '+IntToChaine(PDF_MAX_GEN_NUM,5)+' f'+CRLF,AFlux);
WriteXRefTable(AFlux);
// write trailer
WriteChaine('trailer'+CRLF,AFlux);
Trailer.WriteDictionary(-1,AFlux);
// write offset of last xref table
WriteChaine(CRLF+'startxref'+CRLF+IntToStr(XRefPos)+CRLF,AFlux);
WriteChaine(PDF_FILE_END,AFlux);
end;
end.
|