Getting the location within a pdf document

I am using the WPF PdfViewer hosted in a WinForms application. This was the only way I could get the freehand annotation tools.

I am having several issues that all revolve around not being able to get the actual position on a pdf page. I have setup an event for a mouse click on the page which then calls a function to insert text at the position specified in the args variable. As I understand the documentation this is supposed to be the position relative to the current document.

    Private Sub Viewer_PageClicked(sender As Object, args As Syncfusion.Windows.PdfViewer.PageClickedEventArgs) Handles Viewer.PageClicked
        If tsbBallonText.Checked Then
            InsertTextObj(args.Position)
        End If
    End Sub

The InsertTextObj simply captures the text and creates a free text annotation at the position of the mouse click.

    Private Sub InsertTextObj(ByVal pointInPage As Windows.Point)
        Dim pdfDoc As PdfLoadedDocument = Viewer.LoadedDocument
        If pdfDoc Is Nothing Then
            Return
        End If
        Dim iPage As Integer = Viewer.CurrentPageIndex - 1
        Dim pdfPg As PdfLoadedPage = pdfDoc.Pages(iPage)
        Dim dlgText As New dlgTextInput
        dlgText.tbMessageText.Font = textFont
        If dlgText.ShowDialog() = DialogResult.OK Then
            Dim sText As String = dlgText.tbMessageText.Text
            Dim bPopup As Boolean = dlgText.tsbPopUp.Checked
            Dim iRotation As Integer = dlgText.nmRotation.Value
            textFont = dlgText.tbMessageText.Font
            Dim pFont As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfTrueTypeFont(textFont)
            Dim sSize As SizeF = GetTextSize(sText, textFont.Name, textFont.Size)
            Dim freeText As New PdfFreeTextAnnotation(New RectangleF(pointInPage.X, pointInPage.Y, sSize.Width, sSize.Height))
            freeText.Name = "Field_FreeText"
            freeText.Font = pFont
            freeText.Text = sText
            pdfPg.Annotations.Add(freeText)
            Dim ms As New IO.MemoryStream
            pdfDoc.Save(ms)
            pdfView.pdfViewer.Load(ms)
        End If
        dlgText.Close()
    End Sub

When the annotation is created it is no where near the X and Y points I send it. The difference between the points sent from the PageClick function are not consistent when clicking different locations on the page. I have this same issue when attempting to drag and drop text on to the pdf page. I can't find a way to get the actual page location from the screen location that is sent to the Drop event. Since this is a hosted WPF control and not a WinForms control the PointToClient method is not available. Any help on this would be greatly appreciated.

Thanks
ToddH


1 Reply 1 reply marked as answer

DD Divya Dhayalan Syncfusion Team May 27, 2021 11:48 AM UTC

Hi Todd, 
 
Thank you for contacting Syncfusion support. 
 
Please find the table below. 
 
Query 
Details 
I am using the WPF PdfViewer hosted in a WinForms application. This was the only way I could get the freehand annotation tools. 
 
I am having several issues that all revolve around not being able to get the actual position on a pdf page. I have setup an event for a mouse click on the page which then calls a function to insert text at the position specified in the args variable. As I understand the documentation this is supposed to be the position relative to the current document. 
 
    Private Sub Viewer_PageClicked(sender As Object, args As Syncfusion.Windows.PdfViewer.PageClickedEventArgs) Handles Viewer.PageClicked 
        If tsbBallonText.Checked Then 
            InsertTextObj(args.Position) 
        End If 
    End Sub 
 
The InsertTextObj simply captures the text and creates a free text annotation at the position of the mouse click. 
 
    Private Sub InsertTextObj(ByVal pointInPage As Windows.Point) 
        Dim pdfDoc As PdfLoadedDocument = Viewer.LoadedDocument 
        If pdfDoc Is Nothing Then 
            Return 
        End If 
        Dim iPage As Integer = Viewer.CurrentPageIndex - 1 
        Dim pdfPg As PdfLoadedPage = pdfDoc.Pages(iPage) 
        Dim dlgText As New dlgTextInput 
        dlgText.tbMessageText.Font = textFont 
        If dlgText.ShowDialog() = DialogResult.OK Then 
            Dim sText As String = dlgText.tbMessageText.Text 
            Dim bPopup As Boolean = dlgText.tsbPopUp.Checked 
            Dim iRotation As Integer = dlgText.nmRotation.Value 
            textFont = dlgText.tbMessageText.Font 
            Dim pFont As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfTrueTypeFont(textFont) 
            Dim sSize As SizeF = GetTextSize(sText, textFont.Name, textFont.Size) 
            Dim freeText As New PdfFreeTextAnnotation(New RectangleF(pointInPage.X, pointInPage.Y, sSize.Width, sSize.Height)) 
            freeText.Name = "Field_FreeText" 
            freeText.Font = pFont 
            freeText.Text = sText 
            pdfPg.Annotations.Add(freeText) 
            Dim ms As New IO.MemoryStream 
            pdfDoc.Save(ms) 
            pdfView.pdfViewer.Load(ms) 
        End If 
        dlgText.Close() 
    End Sub 
 
When the annotation is created it is nowhere near the X and Y points I send it. The difference between the points sent from the PageClick function are not consistent when clicking different locations on the page. 
There are two factors need to consider, if you need to add any annotations in the pages using PdfLoadedDocument and based on the position obtained from “PageClicked” event. The factors are 
 
1.       Pixel to Point Conversion [The position values obtained from the control will be in pixels, and the annotations are expected to be added in points value if you are using PdfLoadedDocument]. 
2.       Zoom Factor [The position values obtained from the control will be varied with the zoom values, and the annotations are expected to be added in 100% zoom value if you are using PdfLoadedDocument] 
 
Please find the below code snippet to achieve the proper positioning of elements. 
 
'Get the ZoomRatio of PdfViewerControl 
Dim zoomfactor As Single = WPFUserControl_PdfViewer.PdfViewer.ZoomPercentage / 100.0F 
 
Dim clickedPosition As System.Windows.Point = mouseClickedPosition 
Dim unitConvertor As PdfUnitConvertor = New PdfUnitConvertor() 
 
'Convert the screen coordinates to Points with respect to zoom  
Dim positionX As Single = unitConvertor.ConvertFromPixels(CSng((clickedPosition.X / zoomfactor)), PdfGraphicsUnit.Point) 
 
Dim positionY As Single = unitConvertor.ConvertFromPixels(CSng((clickedPosition.Y / zoomfactor)), PdfGraphicsUnit.Point) 
 
We have created sample for your reference, and it can be downloaded from the below link, 
 I have this same issue when attempting to drag and drop text on to the pdf page. I can't find a way to get the actual page location from the screen location that is sent to the Drop event. Since this is a hosted WPF control and not a WinForms control the PointToClient method is not available. Any help on this would be greatly appreciated. 
In our PdfViewerControl we do not have support to drag and drop text on to the PDF Pages. We suspect that you are drag and drop the elements and adding it to the pages with the help of “PageClicked” event. If so, please consider the above factors and let us know if it helps. Also, kindly provide us more details if our observation is different from yours.  
 
 
Regards, 
Divya 


Marked as answer
Loader.
Up arrow icon