We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Translating screen coordinates into PDF coordinates

I would like to integrate Syncfusion's PDF viewer into my Flutter tablet application and let users draw paths and other shapes into the PDF with the pencil. Is there a way to translate screen coordinates into PDF coordinates to be able to do this?


25 Replies

DR Deepika Ravi Syncfusion Team April 10, 2023 05:36 PM UTC

Hi Sébastien Arbogast,


Please find the example application from the attachment for retrieving the tapped coordinates relative to the PDF page. Please find the workflow of this example application below,


We have used the TapGestureRecoginzer to catch the tap action and onTapDown callback. we have _getPdfCoordinates() method, it has the logic to return the tapped location coordinates relative to the PDF page.


Regards,

Deepika R


Attachment: pdf_coordinates_8b478afe.zip


SA Sébastien Arbogast April 11, 2023 09:07 AM UTC

Thanks a lot Deepika. That was really helpful.

Now I am trying to use a slightly modified version of this code to try and draw a blue square in the PDF, whose upper-left corner is where I'm clicking.

The rectangle shape is drawn, but not where I'm clicking, and I get an internal exception in PdfDictionary which I can't quite understand.

I have uploaded my code here: https://github.com/sarbogast/pdf_annotation_poc

Can you please help me figure out what is wrong with my code?



DR Deepika Ravi Syncfusion Team April 13, 2023 01:52 AM UTC

Hi Sébastien Arbogast,

Regarding adding rectangle in tapped position:

I have fixed the in your sample [along with the output screen record of the same] and it can be downloaded from the attachment.

Regarding PdfDictionary exception:

We are able to reproduce the reported issue with the provided details, we are analyzing on this and will update the details on 17 April 2023

Regards,

Deepika R


Attachment: Sample_2625e93a.zip


DR Deepika Ravi Syncfusion Team April 17, 2023 06:22 PM UTC

Hi Sébastien Arbogast,


Please find the modified sample from attachment and let us know whether the issue is resolved at your end.


Regards,

Deepika R


Attachment: Signature_396ef2fd.zip


SA Sébastien Arbogast April 18, 2023 06:51 AM UTC

Hello Deepika,

The only difference I see between your code and mine is that you added those 3 lines to the `SfPdfViewer.memory` constructor:

enableTextSelection: false,
enableDocumentLinkAnnotation: false,
enableHyperlinkNavigation: false,

Could you please clarify how those are connected to the exception I got in PdfDictionary? This would help me figure out why I can't enable text selection, document link annotations and/or hyperlink navigation.



DR Deepika Ravi Syncfusion Team April 19, 2023 05:07 PM UTC

Hi Sébastien Arbogast,

The reported issue is a known problem in SfPDFViewer. We hope that you are looking for a sample to retrieve PDF coordinates, so we have resolved the issue at the sample level to meet your requirements. We will fix the issue at a later time.

Regards,

Deepika R



IS info startmicro July 3, 2023 09:04 PM UTC

Dear Support Team,

I have been struggling with the coordinate transformation for the past few days, and I would greatly appreciate your assistance.

I am working on a Flutter application where I have a Stack containing a CustomPainter and your PDFViewer. I use the CustomPainter to draw a rectangle with the mouse, and then I extract the coordinates of the rectangle to draw an annotation on the PDF document.

The X-axis alignment is not an issue since both widgets are centered horizontally. However, I am facing difficulties with the Y-axis due to scrolling. The scrolling affects the positioning of the annotation, and I would like to find a solution for this.

I was wondering if you could provide me with a simple example or guidance on how to handle this situation. If needed, I can share my current source code with you for further analysis.

Thank you in advance for your invaluable support. I appreciate your assistance.

Best regards, 

Zied



DR Deepika Ravi Syncfusion Team July 4, 2023 02:21 PM UTC

Hi info startmicro,

Regarding text markup annotation:

We have a workaround for adding text markup annotation in SfPdfViewer. Kindly refer the following KB link,

https://support.syncfusion.com/kb/article/11033/how-to-add-text-markup-annotation-in-flutter-pdf-viewer-using-syncfusion-pdf-package

Regarding drawn the rectangle in the tapped location:

We have prepared the workaround for drawn the rectangle in SfpdfViwer (along with the output screen record of the same) and it can be downloaded from the attachment. Please find the workflow of this example application below,


  • We have used the TapGestureRecoginzer to catch the tap action and onTapDown callback we have called _getPdfCoordinates() and _addTextToPdfCoordinates() methods.
  • _getPdfCoordinates() method has the logic to return the tapped location coordinates relative to the PDF page.
  • _addTextToPdfCoordinates() method has the logic to drawn a rectangle to the tapped location.


Please try this and let us know if this meets your requirement.


Regards,

Deepika R


Attachment: rectangle_annotation_4ee954cd.zip


IS info startmicro July 12, 2023 08:43 PM UTC

Hi Deepika,

thank you for your response. Unfortunately, your solution is still not working correctly. I have now implemented your _getPdfCoordinates method. There is still a misalignment between the CustomPainter and the PDF annotation on the document. I also noticed that the calculation for the page number with:

// Gets the page number on which the tap has been made
double value =
(_pdfViewerController.scrollOffset.dy + details.dy) / pageHeight;
int pageNumber = value.toInt();

does not work properly when the heights of the pages in the PDF file are different. 


My main issue remains the calculation of the Y-axis offset. Could you please review my code and provide some suggestions on how to solve this problem?"


best regards,

Zied


Attachment: main.dart_e54e6e1c.zip


DR Deepika Ravi Syncfusion Team July 13, 2023 03:20 PM UTC

We are checking the reported issue in your sample and will provide more details on July 17, 2023.



DR Deepika Ravi Syncfusion Team July 17, 2023 05:01 PM UTC

We have resolved the issue in the shared sample where annotations were not properly added on the corresponding page. We have attached the modified sample along with the output screen recording, which can be downloaded from the attachment. However, in the shared sample, the Gesture Detector and CustomPainterCanvas were used. In SfPdfViewer, we also used a gesture detector, due to the gesture conflicts in the sample, you may experience issues with scrolling operations. We have a plan to implement annotation support in SfPdfViewer. Kindly use the link below to track the status of this feature, https://www.syncfusion.com/feedback/26332/support-for-shape-annotations


Attachment: sample_d4017d60.zip


DR Deepika Ravi Syncfusion Team July 20, 2023 05:29 AM UTC

At present we do not have support for shape annotations in Flutter SfPdfViewer. We do not have any immediate plans to implement this feature. At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented. Kindly use the following link for track the status of the feature,

https://www.syncfusion.com/feedback/26332/support-for-shape-annotations



SA Sébastien Arbogast July 26, 2023 11:01 PM UTC

I am still trying to make this work, but this time with a drawing board stacked on top of the SfPdfViewer, so that I can draw a path and then send it to be translated into a shape on the right page of the PDF.

If you run the attached project, click the pen button in the bottom right to start annotating, then draw a line somewhere and finally click the check button in the bottom right, it draws the same shape into the PDF, but the coordinates are off to the top and to the left by a distance that is proportional to how far bottom-right you draw.

So obviously there is something wrong in my implementation of _getPdfCoordinates that is supposed to translated coordinates from the drawing board space into the PDF space. But it is inspired by your implementation and to be honest I don't fully understand it so I fail to see what is wrong.

Note that this time, I am using PdfPageLayoutMode.single as a pageLayoutMode, so there shouldn't be any issue with scrolling.

Can you please help me figure this out?


Attachment: pdf_annotation_d77c99af.zip


DR Deepika Ravi Syncfusion Team July 27, 2023 06:04 PM UTC

In the volume 2 SP release, we have added support for the onTap callback in SfPdfViewer. This callback provides the following information: page number, position, and page position. Through this, you can fulfill your requirements without _getPdfCorrdinates implementation. The release is expected to be available by the end of July. We will inform you once the release is rolled out.



DR Deepika Ravi Syncfusion Team July 28, 2023 07:08 AM UTC

We are glad to announce that the support for onTap callback is available from the Essential Studio 2023 Volume 2 Service Pack v22.2.5 release. Please find the download link below,

Essential Studio 2023 Volume 2 Service Pack Release v22.2.5 is available for download | Announcements Forums | Syncfusion

Package link: https://pub.dev/packages/syncfusion_flutter_pdfviewer/versions/22.2.5



SA Sébastien Arbogast July 29, 2023 10:21 AM UTC

Hello Deepika,

Unfortunately, this is not going to solve anything.

First of all, because with only the onTap event (corresponding to onPointerUp) and no event for pointer down and pointer move, it's basically useless for drawing.

And second of all, and more importantly, the position sent in this callback is the local position in the coordinate system of the PDF viewer, but not the coordinates in the space of the PDF document itself. So it the document is zoomed in, if there are margins around, or if it scrolled, we still need to translate those coordinates into PDF coordinates, so we are back to square one.



DR Deepika Ravi Syncfusion Team August 1, 2023 04:15 PM UTC

Hi Sébastien Arbogast,


We have resolved the issue in your sample, and it can be downloaded from the attachment. As mentioned earlier, we have plans to implement shape annotation in SfPdfViewer. Kindly use the following link to track the status of this feature: https://www.syncfusion.com/feedback/26332/support-for-shape-annotations


Regards,

Deepika R


Attachment: pdf_annotation_f48ad15d.zip


SA Sébastien Arbogast August 2, 2023 04:18 PM UTC

Your fix does not seem to work. I have attached 2 screenshots on my device, one with a black line in the bottom right corner corresponding to the line I drew in the component, and the second with a red line corresponding to the line drawn into the PDF with the translated coordinates. As you can see, they do not match.


Attachment: screenshots_74d4b46c.zip


DR Deepika Ravi Syncfusion Team August 3, 2023 05:14 PM UTC

Hi Sébastien Arbogast,


The reported issue is a platform-based issue. In the previously shared sample, we only fixed the issue on the Windows platform with a single page layout option. We are now working on updating the sample work for all platforms, including a single page layout and a continuous page layout with both vertical and horizontal scrolling. Kindly provide information about your platforms, preferred page layout options, scrolling, and zooming options. This information will help us provide the sample as soon as possible.



SA Sébastien Arbogast August 3, 2023 05:36 PM UTC

My primary targets for this feature would be web, Android and iOS (no Windows, Linux or macOS at this stage).

In terms of layout, I would like it to work primarily in single page layout mode, without any zooming or scrolling, but taking into account margins that may appear around the document depending on aspect ratio.

Ultimately, if it can also support scrolling and zooming in continuous layout, I could add the same feature in other parts of my application, but that's lower priority.



DR Deepika Ravi Syncfusion Team August 4, 2023 04:43 PM UTC

Hi Sébastien Arbogast ,

We have resolved the issue in the shared sample with the single page layout option. We have attached the modified sample along with the output screen recording, which can be downloaded from the attachment. Please try this and let us know if this meets your requirement.


Attachment: F_181702_5ca49f6a.zip


SA Sébastien Arbogast August 4, 2023 06:20 PM UTC

Indeed, it is working well in iOS and Android now, but I still have the wrong coordinates in a web browser (see attached video).

Plus, I'm not sure about the way you initialize the kIsDesktop constant, as it does not take into account the mobile web browser situation.


Attachment: pdf_annotations_web.mp4_b4f6c8bc.zip


DR Deepika Ravi Syncfusion Team August 7, 2023 04:36 PM UTC

We tried to replicate the reported issue on our side but the shared sample working fine in web platform. For your reference we have attached the output screen recording of the sample, which can be downloaded from the attachment. Kindly share the modified sample and steps/video for replicate the issue on our side. This will be helpful for us to provide the prompt solution.


Attachment: F181702_f85d55db.zip


SA Sébastien Arbogast August 7, 2023 07:48 PM UTC

In the attached video called "pdf_annotation with appbar", I am using your exact code, without any modification, on macOS 13.4.1, Chrome 115.0.5790.170, Flutter 3.10.6, and there is still a shift to the top or to the bottom, depending on whether I'm drawing toward the top or the bottom of the page.

In the other video, I simply commented the appbar in the scaffold, and you can see that the shift is even more there.




Attachment: pdf_annotation_a4c880b7.zip


DR Deepika Ravi Syncfusion Team August 8, 2023 05:26 PM UTC

Regarding issue in chrome platform:

We have checked the reported issue on the web platform on a Windows machine, and it is working fine. However, the issue is only replicated when the Chrome window is maximized on macOS. The 'constrainSizeAndAttemptToPreserveAspectRatio' is not properly updated when the window size is changed to maximize on macOS. We checked the issue without the PDF Viewer, but we still faced the same issue.


Loader.
Up arrow icon