Frequently Asked Question:
Display x and y coordinates in Adobe Reader
Is there a way to get the free Adobe Reader to display the x,y coordinates of the cursor?
The coordinate system I'm interested in is simply the document's own layout. I do not need to save any changes, I just want to know how many points the cursor is from the document origin. For example, the corner opposite the lower-left origin is [578,838] in one of my 8.5" x 11.0" documents.
Adobe Reader does have a measuring tool, unfortunately it's only enabled if the particular PDF was created with the full Adobe Acrobat with the author specifically choosing to enable the feature.
Each page in a PDF has settings that define the range of the measurement system (the MediaBox entry) and the location of the visible page within that range (the CropBox entry).
The page content itself might also contain drawing commands that scale or translate the co-ordinate system. Adding new content to the page might be affected by these existing drawing commands. This usually isn't a problem because the page contents can be encapsulated within save state / restore state drawing commands to reset any existing transformations.
Steps to put together an interactive tool to show the co-ordinates using Quick PDF Library:
- Use the LoadFromFile function to open up the PDF
- Call the GetPageBox function to obtain the location and size of the page's CropBox rectangle (specifically the left, bottom, width and height values).
- Convert the page into a bitmap image using the RenderPageToFile function.
- Display that bitmap in an image component in the application
- Add a MouseMove event which will get called as the mouse cursor moves around over the image.
- Each time the event runs it will return the X and Y co-ordinates of the mouse within the image. These are usually given with (0,0) being the top right corner of the image.
- With a few calculations the PDF co-ordinates can be determined from the mouse co-ordinates.
Here's a snippet of code from a sample done in Delphi:
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
CropBoxLeft: Double;
CropBoxBottom: Double;
CropBoxWidth: Double;
CropBoxHeight: Double;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
QP: TQuickPDF;
begin
QP := TQuickPDF.Create;
try
QP.UnlockKey(' -- license key -- ');
QP.LoadFromFile('C:\testdoc.pdf');
QP.RenderPageToFile(96, 1, 0, 'C:\testdoc.bmp');
CropBoxLeft := QP.GetPageBox(2, 0);
CropBoxBottom := QP.GetPageBox(2, 5);
CropBoxWidth := QP.GetPageBox(2, 2);
CropBoxHeight := QP.GetPageBox(2, 3);
Image1.Picture.LoadFromFile('C:\testdoc.bmp');
finally
QP.Free;
end;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
MouseX: Double;
MouseY: Double;
ImageMaxX: Double;
ImageMaxY: Double;
PDFX: Double;
PDFY: Double;
begin
MouseX := X;
MouseY := Image1.Height - Y - 1;
ImageMaxX := Image1.Width - 1;
ImageMaxY := Image1.Height - 1;
PDFX := CropBoxLeft + (MouseX / ImageMaxX) * CropBoxWidth;
PDFY := CropBoxBottom + (MouseY / ImageMaxY) * CropBoxHeight;
Label1.Caption := FormatFloat('0.00', PDFX) + ' ' +
FormatFloat('0.00', PDFY);
end;