How to get X and Y coordiantes on tap
How can I get the X,Y coordinates when the user taps on a PDF? I have tried many times, but still can't exactly get the X and Y coordinates that would be correct. The solution that seems the most useful would be this: https://github.com/DImuthuUpe/AndroidPdfViewer/issues/757#issuecomment-510046024
but I can't access the pdfFile variable, since it is not accessible from outside the package: 'pdfFile' is not public in 'com. github. barteksc. pdfviewer. PDFView'. Cannot be accessed from outside package.
Is there another workaround? I need the X,Y coordinates to send them to a web service along with the PDF file where some text will be inserted at the provided coordinates.
Thanks!
@tevzselcan I've just finished solving it...
I recommend you to use the fork of this library: AndroidPdfViewer fork
You can clone the library and make some adjustments, if you need the pdfFile you can expose a method in the PdfView:
public PdfFile getPdfFile() { return pdfFile; }
and set public class PdfFile
because PdfFile is package visibility.
Now in order to make use of the function convertScreenPointToPdfPagePoint() you have to make changes in the pdfium-android library.
Currently in the library they use: api 'com.github.barteksc:pdfium-android:1.9.0'.
But that library does not have the function you mention, since it is a pull request that was closed (I don't understand why).
I tried to add pdfium-android and implement the functions manually but it brings problems with the ndk and some .so that are not compiled in the apk.
After so much, what a surprise “Infomaniak” has already fixed it and we can make use of it, moreover, he already applied the convertScreenPointToPdfPagePoint() method and update proyect !!! Credits: Pdfium fork
With this you have everything to obtain the correct X,Y point.
This fork version at 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.3' is the only one that worked for me, thanks a lot!
Edit: Original post was asking for clarification on implementation since I couldn't quite figure it out. I worked on it for a few days and finally got it to work, so I wanted to give more detail for anyone else looking for the answer
First step is to import mhiew/AndroidPdfViewer 3.2.0-beta.3, but you will want to download as zip file to modify
In my build.gradle for the app, I commented out implementation 'com.github.mhiew:android-pdf-viewer:3.2.0-beta.3' and instead entered implementation project(":android-pdf-viewer")
I did not need to create a getPdfFile() method in PdfView, nor did I need to make the PdfFile class public
Instead, I did the following modifications:
-
In the build.gradle for the imported android-pdf viewer, I commented out api 'com.github.mhiew:pdfium-android:1.9.2' and instead added api 'com.github.Infomaniak:PdfiumAndroid:1.9.6' as per @enriquebautista suggestion. It is important to use 1.9.6 as opposed to the latest version (1.9.8) because the latest version doesn't have the changes in PdfiumCore and mainJNILib.cpp described here: https://github.com/barteksc/PdfiumAndroid/pull/46/commits
-
In the PdfFile, I added the below method
public PointF mapDeviceCoordsToPage(int pageIndex, int startX, int startY, int sizeX, int sizeY,
int deviceX, int deviceY) {
int docPage = documentPage(pageIndex);
return pdfiumCore.mapDeviceCoordsToPage(pdfDocument, docPage, startX, startY, sizeX, sizeY, 0, deviceX, deviceY);
}
- In the PDFView file, I added the below method
public PointF convertScreenPointToPdfPagePoint(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if (pdfFile == null) {
return null;
}
float mappedX = -getCurrentXOffset() + x;
float mappedY = -getCurrentYOffset() + y;
int page = pdfFile.getPageAtOffset(isSwipeVertical() ? mappedY : mappedX, getZoom());
SizeF pageSize = pdfFile.getScaledPageSize(page, getZoom());
int pageX, pageY;
if (isSwipeVertical()) {
pageX = (int) pdfFile.getSecondaryPageOffset(page, getZoom());
pageY = (int) pdfFile.getPageOffset(page, getZoom());
} else {
pageY = (int) pdfFile.getSecondaryPageOffset(page, getZoom());
pageX = (int) pdfFile.getPageOffset(page, getZoom());
}
return pdfFile.mapDeviceCoordsToPage(page, pageX, pageY, (int) pageSize.getWidth(),
(int) pageSize.getHeight(), (int) mappedX, (int) mappedY);
}
- Finally in my app activity, I call on the convertScreenPointToPdfPagePoint method inside an onTap listener (as part of the pdfview configuration i.e., .onTap(new onTapListener() {...convertScreenPointToPdfPagePoint...})
Hope this helps anyone looking to understand how to implement