/
PDFium.Frame.pas
1493 lines (1368 loc) · 37.1 KB
/
PDFium.Frame.pas
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
unit PDFium.Frame;
{
PDF viewer using libPDFium.dll (c)2017-2018 by Execute SARL
http://www.execute.fr
https://github.com/tothpaul/PDFiumReader
2017-09-09 v1.0
2017-09-10 v1.1 better scrolling (less redraw)
2018-11-30 v2.0 switched from PDFium.DLL to libPDFium.DLL
}
interface
{.$DEFINE TRACK_CURSOR}
{.$DEFINE TRACK_EVENTS}
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes, System.Math,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
Execute.libPDFium, Vcl.ExtCtrls;
const
PAGE_MARGIN = 5; // pixels
type
TPDFPrintOptions = record
PageNumber: Integer; // Index dans Pages[]
PageCount: Integer;
Pages: TArray<Integer>;
PageType: Integer; // 0 = Tout, 1 = Impaires, 2 = Paires
PageSize: Integer; // 0 = Ajuster, 1 = Taille réelle, 2 = Auto, 3 = Echelle
PageScale: Single; // Mise à l'échelle de la page
Orientation: Integer; // 0 = Auto, 1 = Portrait, 2 = Paysage
Rotation: Integer; // 1 si l'apperçu est horizontal (il faut faire une rotation à l'impression)
PaperWidth: Integer; // 1/10 de mm
PaperHeight: Integer; // 1/10 de mm
GrayScale: Boolean;
Center: Boolean;
Annotations: Boolean;
// Impression multipage
PagePerSheet: Integer;
PageOrder: Integer; // 0 = Horizontal, 1 = Horizontal inversé, 2 = Vertical, 3 = Vertical Inversé
PageContour: Boolean;
procedure AddPages(Start, Stop: Integer);
function CurrentPage: Integer;
end;
TZoomMode = (
zmCustom,
zmActualSize,
zmPageLevel,
zmPageWidth
);
TPDFiumFrame = class(TFrame)
private
{ Déclarations privées }
type
TPDFPage = class
Frame : TPDFiumFrame;
Index : Integer;
Handle : IPDFPage;
Top : Double;
Rect : TRect;
Text : IPDFText;
NoText : Boolean;
Visible : Integer;
SelStart : Integer;
SelStop : Integer;
Selection : TArray<TRectD>;
HasWidgets: Boolean;
Bitmap : IPDFBitmap;
procedure CheckWidgets;
function HasText: Boolean;
function CharIndex(x, y, distance: Integer): Integer;
function CharCount: Integer;
function Search(const AText: string; AStart: Integer): IPDFSearchText;
function Select(Start: Integer): Boolean;
function SelectTo(Stop: Integer): Boolean;
function ClearSelection: Boolean;
function GetSelectRange(var ARect: TRect): Boolean;
procedure Paint(DC: HDC);
procedure DrawSelection(DC, BMP: HDC; const Blend: TBlendFunction; const Client: TRect);
end;
private
FPDF : IPDFium;
FError : Integer;
FPageCount: Integer;
FPageSize : TArray<TPointsSizeF>;
FTotalSize: TPointsSize;
FPages : TList;
FReload : Boolean;
FPageIndex: Integer;
FZoom : Single;
FZoomMode : TZoomMode;
FStatus : TLabel;
FCurPage : TPDFPage;
FSelPage : TPDFPage;
FSelStart : Integer;
FSelBmp : TBitmap;
FInvalide : Boolean;
FOnPaint : TNotifyEvent;
FSearchText: string;
FSearchPage: Integer;
FSearch : IPDFSearchText;
{$IFDEF TRACK_CURSOR}
FCharIndex: Integer;
FCharBox : TRectD;
{$ENDIF}
procedure OnLoad;
procedure SetPageCount(Value: Integer);
procedure SetScrollSize;
procedure SetZoom(Value: Single);
procedure SetZoomMode(Value: TZoomMode);
procedure AdjustZoom;
procedure ClearPages;
function GetPage(PageIndex: Integer): TPDFPage;
function GetPageAt(const p: TPoint): TPDFPage;
procedure LoadVisiblePages;
procedure WMEraseBkGnd(var Msg: TMessage); message WM_ERASEBKGND;
procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
procedure WMHScroll(var Message: TWMHScroll); message WM_HSCROLL;
procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
protected
procedure PaintWindow(DC: HDC); override;
procedure Resize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
MousePos: TPoint): Boolean; override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
function GetPageTop(Index: Integer): Integer;
function GetPageNumber: Integer;
procedure SetPageNumber(Value: Integer);
function GetPageY(PageIndex: Integer): Integer;
function PageToScreen(Value: Single): Integer; inline;
function ScaleToScreen: Single;
procedure ShowSelection;
procedure ComputePageRect(APage: TPDFPage);
public
{ Déclarations publiques }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Invalidate; override;
procedure CloseDocument();
procedure LoadFromMemory(APointer: Pointer; ASize: Integer);
procedure LoadFromStream(AStream: TStream);
procedure LoadFromFile(const AFileName: string);
procedure ClearSelection;
function PageLevelZoom: Single;
function PageWidthZoom: Single;
procedure NextPage;
procedure PrevPage;
procedure GoPage(Index: Integer);
procedure Print;
procedure PrintPreview(Canvas: TCanvas; const ARect: TRect; const Options: TPDFPrintOptions; ScaleX, ScaleY: Single; Printer: Boolean);
function PageWidth(PageNumber: Integer): Double;
function PageHeight(PageNumber: Integer): Double;
function IsLandscape(PageNumber: Integer): Boolean;
function GetSelectionText: string;
function GetProperty(const Name: AnsiString): string;
function GetProperties: string;
function Search(const AText: string; ANext: Boolean): Boolean;
property PageIndex: Integer read FPageIndex;
property PageCount: Integer read FPageCount;
property PageNumber: Integer read GetPageNumber write SetPageNumber;
property Zoom: Single read FZoom write SetZoom;
property ZoomMode: TZoomMode read FZoomMode write SetZoomMode;
property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
end;
implementation
{$R *.dfm}
uses PDFium.PrintDlg;
resourcestring
sUnableToLoadPDFium = 'Unable to load libPDFium.dll';
procedure TPDFPrintOptions.AddPages(Start, Stop: Integer);
var
Count: Integer;
Len: Integer;
Index: Integer;
begin
if Stop > PageCount then
Stop := PageCount;
if Start < 0 then
Start := 1;
Count := Stop - Start + 1;
if Count < 0 then
Exit;
Len := Length(Pages);
SetLength(Pages, Len + Count);
for Index := 0 to Count - 1 do
begin
if (PageType = 0) or ((PageType = 1) = Odd(Start)) then
begin
Pages[Len] := Start;
Inc(Len);
end;
Inc(Start);
end;
SetLength(Pages, Len);
end;
function TPDFPrintOptions.CurrentPage: Integer;
begin
if (PageNumber > 0) and (PageNumber <= Length(Pages)) then
Result := Pages[PageNumber - 1]
else
Result := 0;
end;
{ TPDFiumFrame.TPDFPage }
function TPDFiumFrame.TPDFPage.HasText: Boolean;
begin
if (Text = nil) and (NoText = False) then
begin
Handle.GetText(Text);
NoText := Text = nil;
end;
Result := not NoText;
end;
procedure TPDFiumFrame.TPDFPage.Paint(DC: HDC);
var
R: TRect;
P: TPoint;
begin
if HasWidgets then
begin
if Bitmap = nil then
Handle.GetBitmap(Rect, Rect, 0, FPDF_ANNOT, Bitmap);
Bitmap.Draw(DC, Rect.Left, Rect.Top);
Exit;
end;
if (Rect.Left <> 0) or (Rect.Top <> 0) then
begin
R := TRect.Create(0, 0, Rect.Width, Rect.Height);
SetViewportOrgEx(DC, Rect.Left, Rect.Top, @P);
Handle.Render(DC, R, 0, FPDF_ANNOT);
SetViewportOrgEx(DC, P.X, P.Y, nil);
end else begin
Handle.Render(DC, Rect, 0, FPDF_ANNOT);
end;
end;
function TPDFiumFrame.TPDFPage.CharCount: Integer;
begin
if HasText then
Result := Text.CharCount
else
Result := 0;
end;
function TPDFiumFrame.TPDFPage.CharIndex(x, y, distance: Integer): Integer;
var
Pos: TPointsSize;
begin
if HasText = False then
Exit(-1);
Handle.DeviceToPage(Rect, x, y, Pos.cx, Pos.cy);
Result := Text.CharIndexAtPos(Pos, distance);
end;
procedure TPDFiumFrame.TPDFPage.CheckWidgets;
var
Count: Integer;
Index: Integer;
Annot: IPDFAnnotation;
begin
if Handle = nil then
Exit;
Count := Handle.GetAnnotationCount;
for Index := 0 to Count - 1 do
begin
if Handle.GetAnnotation(Index, Annot) = 0 then
begin
if Annot.GetSubtype = FPDF_ANNOT_WIDGET then
begin
HasWidgets := True;
Exit;
end;
end;
end;
end;
function TPDFiumFrame.TPDFPage.ClearSelection: Boolean;
begin
Result := Selection <> nil;
if Result then
begin
Selection := nil;
SelStart := 0;
SelStop := 0;
end;
end;
procedure TPDFiumFrame.TPDFPage.DrawSelection(DC, BMP: HDC; const Blend: TBlendFunction; const Client: TRect);
var
Index: Integer;
R: TRect;
begin
for Index := 0 to Length(Selection) - 1 do
begin
with Selection[Index] do
begin
Handle.PageToDevice(Rect, Left, Top, R.Left, R.Top);
Handle.PageToDevice(Rect, Right, Bottom, R.Right, R.Bottom);
end;
if Client.IntersectsWith(R) then
AlphaBlend(DC, R.Left, R.Top, R.Width, R.Height, BMP, 0, 0, 100, 50, Blend);
end;
end;
function TPDFiumFrame.TPDFPage.GetSelectRange(var ARect: TRect): Boolean;
var
Index: Integer;
R: TRect;
begin
Result := Length(Selection) > 0;
if Result and (Visible < 1) then
begin
Frame.ComputePageRect(Self);
end;
for Index := 0 to Length(Selection) - 1 do
begin
with Selection[Index] do
begin
Handle.PageToDevice(Rect, Left, Top, R.Left, R.Top);
Handle.PageToDevice(Rect, Right, Bottom, R.Right, R.Bottom);
end;
if ARect.Height = 0 then
begin
ARect := R;
end
else
begin
if R.Top < ARect.Top then
ARect.Top := R.Top;
if R.Bottom > ARect.Bottom then
ARect.Bottom := R.Bottom;
if R.Left < ARect.Left then
ARect.Left := R.Left;
if R.Right > ARect.Right then
ARect.Right := R.Right;
end;
end;
end;
function TPDFiumFrame.TPDFPage.Search(const AText: string;
AStart: Integer): IPDFSearchText;
begin
if not HasText then
Result := nil
else
Text.Search(TString(AText), 0, AStart, Result);
end;
function TPDFiumFrame.TPDFPage.Select(Start: Integer): Boolean;
begin
Result := Selection <> nil;
if (HasText = False) or (Start < 0) then
begin
Start := 0;
end;
SelStart := Start;
SelStop := SelStart;
Selection := nil;
end;
function TPDFiumFrame.TPDFPage.SelectTo(Stop: Integer): Boolean;
var
SelLen: Integer;
Start : Integer;
Count : Integer;
Index : Integer;
begin
if Stop < 0 then
Exit(False);
if Stop > SelStart then
Inc(Stop); // add one char
if Stop = SelStop then
Exit(False);
Result := True;
SelStop := Stop;
SelLen := SelStop - SelStart;
if SelLen = 0 then
Selection := nil
else begin
if SelLen > 0 then
Start := SelStart
else begin
Start := SelStop;
SelLen := -SelLen;
end;
Count := Text.GetRectCount(Start, SelLen);
SetLength(Selection, Count);
for Index := 0 to Count - 1 do
begin
Text.GetRect(Index, Selection[Index]);
end;
end;
end;
{ TPDFiumFrame }
constructor TPDFiumFrame.Create(AOwner: TComponent);
var
Err: Integer;
begin
{$IFDEF TRACK_EVENTS}
AllocConsole;
{$ENDIF}
inherited;
ControlStyle := ControlStyle + [csOpaque];
FZoom := 100;
FPageIndex := -1;
FSelBmp := TBitmap.Create;
FSelBmp.Canvas.Brush.Color := RGB(50, 142, 254);
FSelBmp.SetSize(100, 50);
FPages := TList.Create;
try
Err := PDF_Create(PDFIUM_VERSION, FPDF);
if Err <> 0 then
raise Exception.Create('Unable to load PDF');
except
FStatus := TLabel.Create(Self);
FStatus.Align := alClient;
FStatus.Parent := Self;
FStatus.Alignment := taCenter;
FStatus.Layout := tlCenter;
FStatus.Caption := sUnableToLoadPDFium;
end;
end;
destructor TPDFiumFrame.Destroy;
begin
CloseDocument();
FPages.Free;
FSelBmp.Free;
inherited;
end;
function TPDFiumFrame.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
MousePos: TPoint): Boolean;
begin
FReload := True;
VertScrollBar.Position := VertScrollBar.Position - WheelDelta;
Result := True;
end;
procedure TPDFiumFrame.LoadFromFile(const AFileName: string);
var
UTF8Name: UTF8String;
Password: string;
AnsiPwd : AnsiString;
begin
UTF8Name := UTF8String(AFileName);
ClearPages;
FError := FPDF.LoadFromFile(TUTF8String(UTF8Name), '');
while FError = FPDF_ERR_PASSWORD do
begin
if InputQuery('PDFium', 'Password', Password) = False then
Break;
AnsiPwd := AnsiString(Password);
FError := FPDF.LoadFromFile(TUTF8String(UTF8Name), PAnsiChar(AnsiPwd));
end;
OnLoad;
end;
procedure TPDFiumFrame.LoadFromMemory(APointer: Pointer; ASize: Integer);
begin
ClearPages;
FError := FPDF.LoadFromMemory(APointer, ASize, '');
OnLoad;
end;
procedure TPDFiumFrame.LoadFromStream(AStream: TStream);
var
Stream: TCustomMemoryStream;
begin
if AStream is TCustomMemoryStream then
Stream := TCustomMemoryStream(AStream)
else begin
Stream := TMemoryStream.Create;
Stream.CopyFrom(AStream, AStream.Size - AStream.Position);
end;
try
LoadFromMemory(Stream.Memory, Stream.Size);
finally
if Stream <> AStream then
Stream.Free;
end;
end;
procedure TPDFiumFrame.OnLoad;
begin
if FError = 0 then
SetPageCount(FPDF.GetPageCount)
else
SetPageCount(0);
FReload := True;
Invalidate;
end;
function TPDFiumFrame.ScaleToScreen: Single;
begin
Result := FZoom / 100 * Screen.PixelsPerInch / 72;
end;
function TPDFiumFrame.Search(const AText: string; ANext: Boolean): Boolean;
var
Start: Integer;
Len: Integer;
Page: TPDFPage;
begin
Result := False;
if AText = '' then
Exit;
if (AText <> FSearchText) or (FSearch = nil) then
begin
FSearchText := AText;
FSearchPage := PageIndex;
if ANext then
Start := 0
else
Start := -1;
FSearch := GetPage(FSearchPage).Search(AText, Start);
end;
if ANext then
begin
while (FSearch = nil) or (FSearch.FindNext = 0) do
begin
repeat
if FSearchPage = PageCount - 1 then
begin
FSearch := nil;
Exit;
end;
Inc(FSearchPage);
FSearch := GetPage(FSearchPage).Search(AText, 0);
until FSearch <> nil;
end;
end
else
begin
while (FSearch = nil) or (FSearch.FindPrev = 0) do
begin
repeat
if FSearchPage = 0 then
begin
FSearch := nil;
Exit;
end;
Dec(FSearchPage);
FSearch := GetPage(FSearchPage).Search(AText, -1);
until FSearch <> nil;
end;
end;
ClearSelection;
FSearch.GetPosition(Start, Len);
Page := GetPage(FSearchPage);
Page.Select(Start);
Page.SelectTo(Start + Len);
ShowSelection;
Invalidate;
Result := True;
end;
(*
var
Index: Integer;
Page: TPDFPage;
Start, Len: Integer;
begin
for Index := 0 to Pred(FPDF.GetPageCount) do
begin
Page := GetPage(Index);
if Page.HasText then
begin
AllocConsole;
WriteLn('$', IntToHex(Cardinal(Text), 8), ' ', ord(Text[1]));
Page.Text.Search(TString(Text), 0, 0, Result);
if (Result <> nil) then
begin
Result.GetPosition(Start, Len);
if (Result.FindNext = 0) then
begin
SetPageNumber(Index);
Result.GetPosition(Start, Len);
Page.Select(Start);
Page.SelectTo(Start + Len);
end;
end;
end;
end;
end;
*)
procedure TPDFiumFrame.SetPageCount(Value: Integer);
var
Index: Integer;
begin
FTotalSize.cx := 0;
FTotalSize.cy := 0;
FPageCount := Value;
if FPageCount > 0 then
begin
SetLength(FPageSize, FPageCount);
for Index := 0 to FPageCount - 1 do
begin
FPDF.GetPageSize(Index, FPageSize[Index]);
with FPageSize[Index] do
begin
if cx > FTotalSize.cx then
FTotalSize.cx := cx;
FTotalSize.cy := FTotalSize.cy + cy;
end;
end;
end;
HorzScrollBar.Position := 0;
VertScrollBar.Position := 0;
SetScrollSize;
end;
procedure TPDFiumFrame.SetPageNumber(Value: Integer);
begin
Dec(Value);
if (Value >= 0) and (Value < FPageCount) and (FPageIndex <> Value) then
begin
FPageIndex := Value;
FReload := True;
VertScrollBar.Position := GetPageY(FPageIndex);
end;
end;
procedure TPDFiumFrame.SetScrollSize;
var
Scale: Single;
begin
Scale := FZoom / 100 * Screen.PixelsPerInch / 72;
HorzScrollBar.Range := Round(FTotalSize.cx * Scale) + PAGE_MARGIN * 2;
VertScrollBar.Range := Round(FTotalSize.cy * Scale) + PAGE_MARGIN * (FPageCount + 1);
end;
procedure TPDFiumFrame.SetZoom(Value: Single);
begin
if Value < 0.65 then
Value := 0.65;
if Value > 6400 then
Value := 6400;
FZoom := Value;
SetScrollSize;
FReload := True;
Invalidate;
if Assigned(OnResize) then
OnResize(Self);
end;
procedure TPDFiumFrame.SetZoomMode(Value: TZoomMode);
begin
FZoomMode := Value;
AdjustZoom;
end;
procedure TPDFiumFrame.ShowSelection;
var
R: TRect;
Index: Integer;
Page: TPDFPage;
sPage: TPDFPage;
V0, V1: Integer;
dx, dy: Integer;
begin
FillChar(R, SizeOf(R), 0);
sPage := nil;
for Index := 0 to FPages.Count - 1 do
begin
Page := FPages[Index];
if Page.GetSelectRange(R) then
sPage := Page;
end;
if R.Height = 0 then
Exit;
V0 := VertScrollBar.Position;
V1 := V0 + ClientHeight;
dx := 0;
dy := 0;
if (R.Top < 0) or (V0 + R.Top > V1) then
begin
dy := -R.Top;
end;
V0 := HorzScrollBar.Position;
V1 := V0 + ClientWidth;
if (R.Left < 0) then
dx := -R.Left
else if (V0 + R.Right > V1) then
begin
dx := V1 - R.Right;
if dx > -R.Left then
dx := -R.Left;
end;
if (dx <> 0) or (dy <> 0) then
begin
SendMessage(Handle, WM_SETREDRAW, WPARAM(False), 0);
try
ScrollBy(dx, dy);
HorzScrollBar.Position := HorzScrollBar.Position - dx;
VertScrollBar.Position := VertScrollBar.Position - dy;
finally
SendMessage(Handle, WM_SETREDRAW, WPARAM(True), 0);
FReload := True;
inherited Invalidate;
end;
end;
end;
procedure TPDFiumFrame.AdjustZoom;
begin
case FZoomMode of
zmCustom : Exit;
zmActualSize: Zoom := 100;
zmPageLevel : Zoom := PageLevelZoom;
zmPageWidth : Zoom := PageWidthZoom;
end;
end;
procedure TPDFiumFrame.ClearPages;
var
Index: Integer;
begin
FCurPage := nil;
for Index := 0 to FPages.Count - 1 do
TPDFPage(FPages[Index]).Free;
FPages.Clear;
end;
procedure TPDFiumFrame.ClearSelection;
var
Clear: Boolean;
Index: Integer;
Page : TPDFPage;
begin
Clear := False;
for Index := 0 to FPages.Count - 1 do
begin
Page := FPages[Index];
if Page.ClearSelection then
Clear := True;
end;
if Clear then
Invalidate;
end;
procedure TPDFiumFrame.CloseDocument;
begin
if FPDF <> nil then
begin
FSearchText := '';
FSearch := nil;
ClearPages;
FPDF.CloseDocument;
if not (csDestroying in ComponentState) then
begin
SetPageCount(0);
Invalidate;
end;
end;
end;
procedure TPDFiumFrame.ComputePageRect(APage: TPDFPage);
var
Top: Double;
Marge: Integer;
Index: Integer;
Scale: Single;
Center: Integer;
begin
Top := 0;
Marge := PAGE_MARGIN;
for Index := 0 to APage.Index - 1 do
begin
Top := Top + FPageSize[Index].cy;
Inc(Marge, PAGE_MARGIN);
end;
Index := APage.Index;
Scale := ScaleToScreen;
Center := HorzScrollBar.Range;
if Center >= ClientWidth then
Center := PAGE_MARGIN + Center div 2
else
Center := ClientWidth div 2;
APage.Rect.Top := Round(Top * Scale) + Marge - VertScrollBar.Position;
APage.Rect.Left := Center - Round(FPageSize[Index].cx / 2 * Scale) - HorzScrollBar.Position;
APage.Rect.Width := Round(FPageSize[Index].cx * Scale);
APage.Rect.Height := Round(FPageSize[Index].cy * Scale);
end;
function TPDFiumFrame.GetPage(PageIndex: Integer): TPDFPage;
var
Index: Integer;
begin
for Index := 0 to FPages.Count - 1 do
begin
Result := FPages[Index];
if Result.Index = PageIndex then
Exit;
end;
Result := TPDFPage.Create;
Result.Frame := Self;
FPages.Add(Result);
Result.Index := PageIndex;
FPDF.GetPage(PageIndex, Result.Handle);
Result.CheckWidgets;
end;
function TPDFiumFrame.GetPageAt(const p: TPoint): TPDFPage;
var
Index: Integer;
begin
for Index := 0 to FPages.Count - 1 do
begin
Result := FPages[Index];
if (Result.Visible > 0) and (Result.Rect.Contains(p)) then
Exit;
end;
Result := nil;
end;
function TPDFiumFrame.GetPageNumber: Integer;
begin
Result := FPageIndex + 1;
end;
function TPDFiumFrame.GetPageTop(Index: Integer): Integer;
var
PageTop: Single;
begin
PageTop := 0;
Result := PAGE_MARGIN * Index;
while Index > 0 do
begin
Dec(Index);
PageTop := PageTop + FPageSize[Index].cy;
end;
Inc(Result, Round(PageTop * FZoom / 100 * Screen.PixelsPerInch / 72));
end;
function TPDFiumFrame.GetPageY(PageIndex: Integer): Integer;
var
y: Double;
begin
Result := PageIndex * PAGE_MARGIN;
y := 0;
while PageIndex > 0 do
begin
Dec(PageIndex);
y := y + FPageSize[PageIndex].cy;
end;
Inc(Result, PageToScreen(y));
end;
function TPDFiumFrame.GetProperties: string;
begin
Result := GetProperty('Title')
+ GetProperty('Author')
+ GetProperty('Subject')
+ GetProperty('Keywords')
+ GetProperty('Creator')
+ GetProperty('Producer')
+ GetProperty('CreationDate')
+ GetProperty('ModDate');
end;
function TPDFiumFrame.GetProperty(const Name: AnsiString): string;
var
L: Integer;
begin
L := FPDF.GetMetaText(TAnsiString(Name), nil, 0);
if L > 2 then
begin
SetLength(Result, (L div 2) - 1);
FPDF.GetMetaText(TAnsiString(Name), Pointer(Result), L);
Result := string(Name) + ': ' + Result + #13;
end else begin
Result := '';
end;
end;
function TPDFiumFrame.GetSelectionText: string;
var
Index: Integer;
Page: TPDFPage;
Count: Integer;
SelLen: Integer;
begin
Count := 0;
Result := '';
for Index := 0 to Pred(FPDF.GetPageCount) do
begin
Page := GetPage(Index);
SelLen := Page.SelStop - Page.SelStart;
if SelLen = 0 then
Continue;
SetLength(Result, Count + Abs(SelLen));
if SelLen < 0 then
Page.Text.GetText(Page.SelStop, - SelLen, @Result[Count + 1])
else
Page.Text.GetText(Page.SelStart, SelLen, @Result[Count + 1]);
Inc(Count, Abs(SelLen));
end;
end;
procedure TPDFiumFrame.GoPage(Index: Integer);
begin
if (Index >= 0) and (Index < PageCount) then
begin
FReload := True;
VertScrollBar.Position := GetPageTop(Index);
end;
end;
procedure TPDFiumFrame.Invalidate;
begin
if FInvalide = False then
begin
inherited;
FInvalide := True;
end else begin
{$IFDEF TRACK_EVENTS}WriteLn('Not invalidated');{$ENDIF}
end;
end;
function TPDFiumFrame.IsLandscape(PageNumber: Integer): Boolean;
begin
Result := (PageNumber > 0) and (PageNumber <= Length(FPageSize))
and (FPageSize[PageNumber - 1].cx > FPageSize[PageNumber - 1].cy);
end;
procedure TPDFiumFrame.LoadVisiblePages;
var
Index : Integer;
Page : TPDFPage;
Top : Double;
Scale : Double;
Client: TRect;
Rect : TRect;
Marge : Integer;
begin
FPageIndex := -1;
FCurPage := nil;
for Index := 0 to FPages.Count - 1 do
begin
Page := FPages[Index];
if Page.Selection = nil then
Dec(Page.Visible)
else
Page.Visible := 0;
end;
Client := ClientRect;
Top := 0;
Marge := PAGE_MARGIN;
Scale := FZoom / 100 * Screen.PixelsPerInch / 72;
for Index := 0 to FPageCount - 1 do
begin
// compute page position
Rect.Top := Round(Top * Scale) + Marge - VertScrollBar.Position;
Rect.Left := PAGE_MARGIN + Round((FTotalSize.cx - FPageSize[Index].cx) / 2 * Scale) - HorzScrollBar.Position;
Rect.Width := Round(FPageSize[Index].cx * Scale);
Rect.Height := Round(FPageSize[Index].cy * Scale);
if Rect.Width < Client.Width - 2 * PAGE_MARGIN then
Rect.Offset((Client.Width - Rect.Width) div 2 - Rect.Left, 0);
// visibility test
if Rect.IntersectsWith(Client) then