本教程探討了 多頁檢視器 示例,它擴充套件了基本的 PDF 檢視器,增加了連續滾動功能。這種檢視模式類似於現代 PDF 閱讀器(如 Adobe Acrobat)顯示文件的方式,允許使用者無縫滾動瀏覽所有頁面。
概述
多頁檢視器示例展示了 PDFium VCL 中的高階檢視模式,包括單頁連續滾動和雙頁連續滾動(書籍模式)。這些功能對於建立專業的 PDF 閱讀體驗至關重要。
顯示模式
PDFium VCL 通過 DisplayMode 屬性支援多種顯示模式:
|
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; |
主要特性
- 連續滾動 無需逐頁瀏覽,即可滾動瀏覽所有頁面。
- 雙頁模式。 像翻書一樣,同時檢視兩頁內容。
- 跨頁文本選擇。 選取並複製跨越多個頁面的文本。
- 書籤導航。 快速跳轉到已新增書籤的部分。
- 帶有高亮顯示的搜尋功能。 查詢並高亮文件中的文本。
- 鍵盤導航。 箭頭鍵、Page Up/Down、Home/End 快捷鍵支援。
- 最佳化效能。 只渲染可見頁面,以實現流暢的滾動。
PDFium DLL 的要求
在執行任何 PDFium VCL 應用程式之前,必須安裝 PDFium DLL 檔案。這些 DLL 檔案位於: DLLs 資料夾:
pdfium32.dll/pdfium64.dll標準版本,適用於大多數應用程式。pdfium32v8.dll/pdfium64v8.dll擴充套件版本,採用 V8 JavaScript 引擎。
安裝: 執行 PDFiumVCL\DLLs\CopyDlls.bat 以管理員身份複製 DLL 檔案到 Windows 系統目錄。在 64 位 Windows 系統上,32 位 DLL 檔案應放置在 SysWOW64 ,而 64 位 DLL 檔案應放置在 System32.
元件設定。
該演示程式實現了複雜的文本選擇系統:
|
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; |
字元索引檢測。
為了實現精確的文本選擇,該演示程式使用基於縮放的容差來進行字元檢測:
|
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; |
突出顯示選定的文本。
演示中,選中的內容會以高亮顯示。 OnPaint 事件:
|
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; |
複製選中文本到剪貼簿。
|
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; |
搜尋功能。
實現帶高亮的文本搜尋:
|
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; |
頁面切換事件。
處理頁面切換以更新 UI 元素:
|
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; |
鍵盤導航。
演示程式支援鍵盤快捷鍵進行導航。
|
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; |
雙頁導航
在雙頁模式下,一次導航兩個頁面。
|
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; |
結論。
Multi-Page Viewer 演示程式展示瞭如何構建一個功能完善的 PDF 閱讀器,具有連續滾動、文本選擇和搜尋功能。 這些是使用者期望在現代 PDF 檢視應用程式中看到的特性。
PDFium VCL 負責複雜的渲染和文本提取,而您可以專注於使用者介面和特定於應用程式的功能。 結果是流暢、響應迅速的 PDF 檢視體驗,可與商業 PDF 閱讀器相媲美。
獲取 PDFium VCL 元件 at loslab.com,立即開始使用 Delphi 構建專業的 PDF 應用程式。