Questo tutorial esplora il Visualizzatore multipagina. demo, che estende il visualizzatore PDF di base con funzionalità di scorrimento continuo. Questa modalità di visualizzazione è simile a come i moderni lettori PDF come Adobe Acrobat visualizzano i documenti, consentendo agli utenti di scorrere tutte le pagine in modo fluido.
Panoramica
La demo del visualizzatore multi-pagina mostra le modalità di visualizzazione avanzate in PDFium VCL, tra cui la navigazione continua a pagina singola e la navigazione continua a doppia pagina (modalità libro). Queste funzionalità sono essenziali per creare un'esperienza di lettura PDF professionale.
Modalità di visualizzazione.
PDFium VCL supporta diverse modalità di visualizzazione tramite. DisplayMode proprietà:
|
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; |
Caratteristiche Principali
- Scorrimento continuo. – Scorri tutte le pagine senza navigazione pagina per pagina.
- Modalità a Doppia Pagina. – Visualizza due pagine affiancate, come in un libro fisico.
- Selezione del Testo tra le Pagine. – Seleziona e copia il testo che si estende su più pagine.
- Navigazione tramite Segnalibri. – Salta istantaneamente alle sezioni segnalate.
- Ricerca con Evidenziazione. – Trova ed evidenzia il testo all'interno del documento.
- Navigazione da tastiera – Supporto per i tasti freccia, Pagina Su/Giù, Home/Fine.
- Prestazioni ottimizzate. – Visualizza solo le pagine visibili per una navigazione fluida.
Requisiti delle librerie DLL di PDFium
Prima di eseguire qualsiasi applicazione PDFium VCL, è necessario installare i file DLL di PDFium. I file DLL si trovano nella cartella: DLLs – Versioni standard per la maggior parte delle applicazioni.
pdfium32.dll/pdfium64.dll– Versioni standard per la maggior parte delle applicazioni.pdfium32v8.dll/pdfium64v8.dll– Versioni estese con il motore JavaScript V8.
Installazione: Esegui PDFiumVCL\DLLs\CopyDlls.bat come amministratore per copiare i file DLL nelle directory del sistema Windows. In Windows a 64 bit, i file DLL a 32 bit devono essere copiati in SysWOW64 e i file DLL a 64 bit devono essere copiati in System32.
Configurazione del componente.
La demo implementa un sistema di selezione del testo sofisticato:
|
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; |
Rilevamento dell'indice dei caratteri.
Per una selezione precisa del testo, la demo utilizza una tolleranza consapevole dello zoom per il rilevamento dei caratteri:
|
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; |
Evidenziazione del testo selezionato.
La demo evidenzia le selezioni nel OnPaint evento:
|
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; |
Copia il testo selezionato negli appunti
|
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; |
Funzionalità di ricerca
Implementa la ricerca di testo con evidenziazione:
|
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; |
Evento di cambio pagina
Gestisci i cambi di pagina per aggiornare gli elementi dell'interfaccia utente:
|
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; |
Navigazione da tastiera
La demo gestisce le scorciatoie da tastiera per la navigazione:
|
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; |
Navigazione a Doppia Pagina
In modalità a doppia pagina, la navigazione avviene a due pagine alla volta:
|
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; |
Conclusione.
La demo del visualizzatore multi-pagina mostra come creare un lettore PDF completo con scorrimento continuo, selezione del testo e funzionalità di ricerca. Queste sono le funzionalità che gli utenti si aspettano dalle moderne applicazioni di visualizzazione PDF.
PDFium VCL gestisce il rendering complesso e l'estrazione del testo, mentre tu ti concentri sull'interfaccia utente e sulle funzionalità specifiche dell'applicazione. Il risultato è un'esperienza di visualizzazione PDF fluida e reattiva che rivaleggia con i lettori PDF commerciali.
Ottieni Componente PDFium VCL su loslab.com e inizia a creare applicazioni PDF professionali in Delphi oggi stesso.