html Tworzenie wielostronicowej przeglądarki PDF z ciągłym przewijaniem w Delphi | losLab Software Development Blog

Artykuł techniczny

Tworzenie wielostronicowej przeglądarki PDF z ciągłym przewijaniem w Delphi

· Programowanie PDF

W tym samouczku omówiono Przeglądarka wielostronicowa , które rozszerza podstawową przeglądarkę PDF o możliwości ciągłego przewijania. Ten tryb przeglądania jest podobny do sposobu, w jaki nowoczesne czytniki PDF, takie jak Adobe Acrobat, wyświetlają dokumenty, umożliwiając użytkownikom płynne przewijanie wszystkich stron.

Przegląd

Demo przeglądarki wielostronicowej prezentuje zaawansowane tryby przeglądania w PDFium VCL, w tym ciągłe przewijanie jednej strony i ciągłe przewijanie dwóch stron (tryb książki). Te funkcje są niezbędne do stworzenia profesjonalnego doświadczenia w czytaniu PDF.

Tryby wyświetlania

PDFium VCL obsługuje wiele trybów wyświetlania poprzez DisplayMode właściwość:

Zakreślacz składni Urvanov v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
type
  TPdfDisplayMode = (
    dmSingleContinuous,  // Single page with continuous vertical scrolling
    dmDoubleContinuous   // Two pages side-by-side (book layout)
  );
 
// Switch between display modes
procedure TFormMain.ComboBoxDisplayModeChange(Sender: TObject);
begin
  case ComboBoxDisplayMode.ItemIndex of
    0: PdfView.DisplayMode := dmSingleContinuous;
    1: PdfView.DisplayMode := dmDoubleContinuous;
  end;
end;
[Czas formatowania: 0,0008 sekundy]

Kluczowe funkcje

  • Ciągłe przewijanie – Przewijaj wszystkie strony bez nawigacji strona po stronie
  • Tryb dwustronicowy – Wyświetl dwie strony obok siebie jak w fizycznej książce
  • Zaznaczanie tekstu na stronach – Zaznacz i skopiuj tekst obejmujący wiele stron
  • Nawigacja zakładek – Natychmiastowe przechodzenie do sekcji z zakładkami
  • Wyszukiwanie z wyróżnianiem – Znajdź i zaznacz tekst w całym dokumencie
  • Nawigacja za pomocą klawiatury – Klawisze strzałek, strona w górę/w dół, obsługa Home/End
  • Zoptymalizowana wydajność – Renderuje tylko widoczne strony w celu płynnego przewijania

PDFium Wymagania DLL

Przed uruchomieniem jakiejkolwiek aplikacji PDFium VCL należy zainstalować pliki DLL PDFium. Biblioteki DLL znajdują się w pliku DLLs :

  • pdfium32.dll / pdfium64.dll – Wersje standardowe do większości zastosowań
  • pdfium32v8.dll / pdfium64v8.dll – Wersje rozszerzone z silnikiem V8 JavaScript

Instalacja: Uruchom PDFiumVCL\DLLs\CopyDlls.bat jako Administrator, aby skopiować biblioteki DLL do katalogów systemu Windows. W 64-bitowym systemie Windows 32-bitowe biblioteki DLL trafiają do SysWOW64 i 64-bitowe biblioteki DLL trafiają do System32.

Konfigurowanie komponentu

Demo implementuje zaawansowany system zaznaczania tekstu:

Zakreślacz składni Urvanov v2.9.1
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
procedure TFormMain.PdfViewMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  CharIndex: Integer;
begin
  if Button = mbLeft then
  begin
    // Get character index at click position
    CharIndex := GetPreciseCharacterIndex(X, Y);
    
    if CharIndex >= 0 then
    begin
      SelectionMode := True;
      Selecting := True;
      SelectionStart := CharIndex;
      SelectionEnd := CharIndex;
    end;
  end;
end;
 
procedure TFormMain.PdfViewMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
  CharIndex: Integer;
begin
  if Selecting then
  begin
    CharIndex := GetPreciseCharacterIndex(X, Y);
    if CharIndex >= 0 then
    begin
      SelectionEnd := CharIndex;
      PdfView.Invalidate; // Redraw to show selection
    end;
  end;
end;
[Czas formatowania: 0,0004 sekundy]

Wykrywanie indeksu znaków

W celu precyzyjnego zaznaczania tekstu w wersji demonstracyjnej zastosowano tolerancję uwzględniającą powiększenie przy wykrywaniu znaków:

Zakreślacz składni Urvanov v2.9.1
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
function TFormMain.GetPreciseCharacterIndex(X, Y: Integer): Integer;
var
  BaseTolerance: Single;
  ZoomFactor: Single;
  AdjustedTolerance: Single;
  CharIndex: Integer;
begin
  Result := -1;
  
  if not PdfView.Active then
    Exit;
    
  // Calculate dynamic tolerance based on zoom level
  ZoomFactor := PdfView.Zoom;
  BaseTolerance := 5.0;
  
  // Adjust tolerance inversely to zoom
  AdjustedTolerance := BaseTolerance / ZoomFactor;
  
  // Get character at position with tolerance
  CharIndex := PdfView.CharacterIndexAtPos(X, Y,
    AdjustedTolerance, AdjustedTolerance);
    
  Result := CharIndex;
end;
[Czas formatu: 0,0005 sekundy]

Podświetlanie wybranego tekstu

Demo rysuje najważniejsze elementy wyboru w OnPaint zdarzenie:

Zakreślacz składni Urvanov v2.9.1
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
procedure TFormMain.PdfViewPaint(Sender: TObject);
var
  I, StartIdx, EndIdx: Integer;
  Rect: TPdfRectangle;
  R: TRect;
begin
  if (SelectionStart >= 0) and (SelectionEnd >= 0) then
  begin
    StartIdx := Min(SelectionStart, SelectionEnd);
    EndIdx := Max(SelectionStart, SelectionEnd);
    
    // Draw highlight for each character in selection
    for I := StartIdx to EndIdx do
    begin
      Rect := PdfView.CharacterRectangle[I];
      
      // Convert PDF coordinates to screen coordinates
      if PdfView.PageToDevice(
        Rect.Left, Rect.Top,
        PdfView.Left, PdfView.Top,
        PdfView.Width, PdfView.Height,
        PdfView.Rotation, R.Left, R.Top) then
      begin
        // Draw highlight rectangle
        PdfView.Canvas.Brush.Color := clHighlight;
        PdfView.Canvas.Brush.Style := bsSolid;
        PdfView.Canvas.FillRect(R);
      end;
    end;
  end;
end;
[Czas formatu: 0,0005 sekundy]

Skopiuj zaznaczony tekst do schowka

Zakreślacz składni Urvanov v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure TFormMain.MenuItemCopyClick(Sender: TObject);
var
  StartIdx, EndIdx: Integer;
  SelectedText: string;
begin
  if (SelectionStart >= 0) and (SelectionEnd >= 0) then
  begin
    StartIdx := Min(SelectionStart, SelectionEnd);
    EndIdx := Max(SelectionStart, SelectionEnd);
    
    // Set page number for text extraction
    Pdf.PageNumber := PdfView.PageNumber;
    
    // Extract text from selection range
    SelectedText := Pdf.Text(StartIdx, EndIdx - StartIdx + 1);
    
    // Copy to clipboard
    Clipboard.AsText := SelectedText;
  end;
end;
[Czas formatowania: 0,0003 sekundy]

Funkcja wyszukiwania

Zaimplementuj wyszukiwanie tekstu z podświetlaniem:

Zakreślacz składni Urvanov v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
procedure TFormMain.SpeedButtonSearchClick(Sender: TObject);
var
  SearchText: string;
  FoundIndex: Integer;
begin
  SearchText := EditSearch.Text;
  
  if SearchText = '' then
    Exit;
    
  // Find first occurrence
  FoundIndex := Pdf.FindFirst(SearchText, [], 0, True);
  
  if FoundIndex >= 0 then
  begin
    SearchStart := FoundIndex;
    SearchEnd := FoundIndex + Length(SearchText) - 1;
    PdfView.Invalidate; // Redraw to show highlight
  end
  else
    ShowMessage('Text not found');
end;
[Czas formatowania: 0,0003 sekundy]

Zdarzenie zmiany strony

Obsługuj zmiany strony w celu aktualizacji elementów interfejsu użytkownika:

Zakreślacz składni Urvanov v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
procedure TFormMain.PdfViewPageChanged(Sender: TObject);
begin
  if PdfView.Active then
  begin
    // Update page indicator
    SpeedButtonPageNumber.Caption :=
      IntToStr(PdfView.PageNumber) + ' of ' + IntToStr(PdfView.PageCount);
    
    // Update navigation button states
    SpeedButtonFirstPage.Enabled := PdfView.PageNumber > 1;
    SpeedButtonPreviousPage.Enabled := PdfView.PageNumber > 1;
    SpeedButtonNextPage.Enabled := PdfView.PageNumber < PdfView.PageCount;
    SpeedButtonLastPage.Enabled := PdfView.PageNumber < PdfView.PageCount;
    
    // Update bookmark tree selection
    if not DisableBookmarks then
      UpdateBookmarkSelection(PdfView.PageNumber);
  end;
end;
[Czas formatowania: 0,0003 sekundy]

Nawigacja za pomocą klawiatury

Demo obsługuje skróty klawiaturowe do nawigacji:

Zakreślacz składni Urvanov v2.9.1
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
procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    VK_ESCAPE:
      begin
        // Exit selection mode on Escape
        if SelectionMode then
        begin
          SelectionMode := False;
          SelectionStart := -1;
          SelectionEnd := -1;
          PdfView.Invalidate;
        end;
        Key := 0;
      end;
      
    VK_PRIOR, VK_UP, VK_LEFT:
      begin
        if SpeedButtonPreviousPage.Enabled then
          SpeedButtonPreviousPage.Click;
        Key := 0;
      end;
      
    VK_NEXT, VK_DOWN, VK_RIGHT:
      begin
        if SpeedButtonNextPage.Enabled then
          SpeedButtonNextPage.Click;
        Key := 0;
      end;
      
    VK_HOME:
      begin
        if SpeedButtonFirstPage.Enabled then
          SpeedButtonFirstPage.Click;
        Key := 0;
      end;
      
    VK_END:
      begin
        if SpeedButtonLastPage.Enabled then
          SpeedButtonLastPage.Click;
        Key := 0;
      end;
  end;
end;
[Czas formatu: 0,0005 sekundy]

Nawigacja dwustronicowa

W trybie dwustronicowym nawiguj po dwóch stronach jednocześnie:

Zakreślacz składni Urvanov v2.9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
procedure TFormMain.SpeedButtonNextPageClick(Sender: TObject);
begin
  if PdfView.DisplayMode = dmSingleContinuous then
    PdfView.PageNumber := PdfView.PageNumber + 1
  else
    PdfView.PageNumber := PdfView.PageNumber + 2;
end;
 
procedure TFormMain.SpeedButtonPreviousPageClick(Sender: TObject);
begin
  if PdfView.DisplayMode = dmSingleContinuous then
    PdfView.PageNumber := PdfView.PageNumber - 1
  else
    PdfView.PageNumber := PdfView.PageNumber - 2;
end;
[Czas formatowania: 0,0002 sekundy]

Wniosek

Demo przeglądarki wielostronicowej pokazuje, jak zbudować kompletny czytnik PDF z funkcją ciągłego przewijania, zaznaczania tekstu i wyszukiwania. Oto funkcje, których użytkownicy oczekują od nowoczesnych aplikacji do przeglądania PDF.

PDFium VCL obsługuje złożone renderowanie i wyodrębnianie tekstu, podczas gdy Ty skupiasz się na interfejsie użytkownika i funkcjach specyficznych dla aplikacji. Rezultatem jest płynne i responsywne PDF oglądanie, które może konkurować z komercyjnymi czytnikami PDF.

Pobierz PDFium VCL Komponent w losLab.com i już dziś rozpocznij tworzenie profesjonalnych aplikacji PDF w Delphi.