ImageEn, unit iexUserInteractions

TIEPdfViewer.ScrToPage

TIEPdfViewer.ScrToPage


Declaration

function ScrToPage(ScrX, ScrY: Integer; CheckBounds: Boolean = True): TDPoint; overload;
function ScrToPage(ScrRect: TRect; CheckBounds: Boolean = True): TDRect; overload;


Description

Convert a screen position (in pixels) to a value on the current page (in PDF points).
When CheckBounds is true, the result will be -1 for values that are outside the page area

Note: PDF points are not affected by the display DPI


PDF Page Points

Objects on a PDF page are specified in points that originate at the bottom-left, i.e. when X,Y = (0,0). The top-left of the page is specified by (PageWidth, PageHeight).
To convert PDF points to screen values, use PageToScr.



Examples

// Convert a screen point to a page point
pagePt := ImageEnView1.PdfViewer.ScrToPage( 100, 100 );


// Show position on page as the cursor is moved
procedure TfrmMain.ImageEnView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  pt: TDPoint;
begin
  pt := ImageEnView1.PdfViewer.ScrToPage( X, Y, True );
  if ( pt.X = -1 ) or ( pt.Y = -1 ) then
    Caption := 'Page Pos: INVALID'
  else
    Caption := format( 'Page Pos: %d, %d', [ Round( pt.X ), Round( pt.Y )]);
end;


// Delete the item clicked on
procedure TfrmMain.ImageEnView1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  idx: Integer;
  pt: TDPoint;
begin
  pt := ImageEnView1.PdfViewer.ScrToPage( X, Y, True );
  idx := ImageEnView1.PdfViewer.Objects.FindObjectAt( pt );
  if idx > -1 then
    if MessageDlg( format( 'Delete object %d?', [ idx + 1 ]), mtConfirmation, [ mbYes,mbNo ], 0 ) = mrYes then
      ImageEnView1.PdfViewer.Objects.RemoveObject( idx );
end;


// Add a spot wherever user clicks on a PDF page
procedure TfrmMain.ImageEnView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
const
  Spot_Size         = 20;
  Spot_Border_Color = clBlack;
  Spot_Border_Size  = 2;
  Spot_Fill_Color   = clRed;
  Spot_Opacity      = 255;
var
  clickPos: TDPoint;
  obj: TPdfObject;
begin
  clickPos := ImageEnView1.PdfViewer.ScrToPage( X, Y, True );

  // Center ellipse over click position
  obj := ImageEnView1.PdfViewer.Objects.AddEllipse( clickPos.X - Spot_Size div 2, clickPos.Y - Spot_Size div 2, Spot_Size, Spot_Size );

  obj.StrokeColor := TColor2TRGBA( Spot_Border_Color, Spot_Opacity );
  obj.PathStrokeWidth := Spot_Border_Size;
  obj.FillColor := TColor2TRGBA( Spot_Fill_Color, Spot_Opacity );
  obj.PathFillMode := pfAlternate; // Ensure path is filled
end;


See Also

PageToScr