I am attempting to capture the position of a mouse click with respect to the page coordinates, rather than the PDFViewer control coordinates. I read this KB entry:
And while it does work for the control, it does not account for a page that is not displayed with Fit to Width, that is, if the PDF is Fit to Height and the corresponding window is not large enough, there will be a grey border on the left and right of the PDF. The result is that if a user clicks where they think is position X:10, Y:10 on the PDF page, it is reported with that example as X:196,Y:10, where the grey border to the left of the displayed PDF has a width of 186.
I have attempted to do a calculation to resolve the X coordinate for the presence of a border using the following modification to the code linked above:
private void PdfViewer_MouseUp(object sender, MouseButtonEventArgs e)
{
System.Drawing.Point position = new System.Drawing.Point
{
X = (int)e.GetPosition(PdfViewer).X,
Y = (int)e.GetPosition(PdfViewer).Y
};
//Displays the x and y coordinates of PdfViewer Control
xLabel.Content = position.X;
yLabel.Content = position.Y;
//Displays the x coordinate of position on PDF page
xLabelPage.Content = GetPageX(position);
}
private object GetPageX(System.Drawing.Point position)
{
var controlWidth = (int)PdfViewer.ActualWidth;
var document = PdfViewer.LoadedDocument;
var firstPage = document.Pages[0] as PdfLoadedPage; //TODO: Should really check for current page index here.
var pageWidth = (int)firstPage.Size.Width;
var excessWidth = controlWidth - pageWidth; //Excess width being the grey border on either side of a non-Fit To Width pdf
var offsetX = excessWidth / 2; //Assumes equal size border on either side of control
return position.X - offsetX ; //Removes size equal to left border from assumed position on PDF page
}
Unfortunately, this still does not produce the correct offset for the X coordinate. I believe that the issue could be the margin settings for the currently loaded PDF document, as I'm not sure if the content of PdfLoadedPage.Size.Width includes the margin value. The testing I've done with the math above leads me to believe that it does not contain the margin size.
Alas, I cannot seem to find a way to determine the margin of a currently Loaded Document. While you can get there with a PDFDocument via PageSettings, that option does not seem to be available for a pre-existing PDF that is loaded in the viewer and there does not seem to be a way to directly convert/cast one to the other so that I can get at those values.
Additionally, as can be see in the attached file, there does not seem to be a way to determine the relative Y position on the document if the document has been scrolled a bit.
There is, of course, the chance that I'm going about this the hard way. If there is an easier way to achieve the task below, please let me know.
Task: Apply a Custom stamp, without the user having to select a stamp from the toolbar, with a double click at a specific position on the currently loaded PDF.
I am using this event to apply the stamp, but because of the reasons listed above, it is not ending up where the user intends.
private void PDFViewer_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//Get Current Mouse Position
System.Drawing.Point position = new System.Drawing.Point
{
X = (int)e.GetPosition(PDFViewer).X + stampWidth,
Y = (int)e.GetPosition(PDFViewer).Y + stampHeight
};
RectangleF rectangle = new RectangleF(position.X, position.Y, 200, 80);
PdfRubberStampAnnotation rubberStampAnnotation = new PdfRubberStampAnnotation(rectangle);
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 14, PdfFontStyle.Bold);
//Create a new PDF template.
PdfTemplate template = new PdfTemplate(new SizeF(200, 80));
template.Graphics.DrawRectangle(PdfBrushes.LightBlue, 0, 0, 200, 80);
template.Graphics.DrawString("StampText", font, PdfBrushes.Black, PointF.Empty);
rubberStampAnnotation.Appearance.Normal.Graphics.DrawPdfTemplate(template, PointF.Empty);
var page = document.Pages[0] as PdfLoadedPage; //This should check for the current page index rather than assuming page 1
page.Annotations.Add(rubberStampAnnotation);
//Save modified document to Memory Stream
MemoryStream memoryStream = new MemoryStream();
document.Save(memoryStream);
//Load modified document from Memory Stream to display stamped document to user
PDFViewer.Load(memoryStream);
}
Attachment:
XYCoordinates_3696a9cc.zip