Thanks Guys for such a great library using PDFium in android. It makes it real easier to show pdf in android our own way.
I have tried all the possible methods to find out to match the drawn points on the pdf and the point where user have tapped.
Everything works fine if the zoom=1.0 i.e default value.
If I zoom in to the page, and click on the point which is visible, the single tap event gives me some different x,y. I understand this, so I have tried to implement below things, but still no success.
float xPositionInRealScale = pdfView.toRealScale(-pdfView.getCurrentXOffset() + e.getX());
float yPositionInRealScale = pdfView.toRealScale(-pdfView.getCurrentYOffset() + e.getY());
float x = xPositionInRealScale / pdfView.getPageSize(currentPage).getWidth() * 100;
float y = yPositionInRealScale / pdfView.getPageSize(currentPage).getHeight() * 100;
and applying same method to my x,y points which are saved in db. If they match or are close by the pop-up is shown.
I tried to undersand this PR but unable to use it in my code.
your help is really appreciated.
try this:
float scale = get current page scale;
float x = e.getX();
float y = e.getY();
PointF pageTranslate = find page current translation;
float sourceX =( x - pageTranslate.x) / scale;
float sourceY = (y - pageTranslate.y) / scale;
PdfDocument doc = get PdfDocument instance;
PdfiumCore core = get PdfiumCore instance;
int startX = 0;
int startY = 0;
int sizeX = get page width;
int sizeY = get page height;
int rotate = 0;
PointF mapped = core. mapDeviceCoordsToPage(doc, pageIndex, startX, startY, sizeX,
sizeY, rotate, sourceX, sourceY);
@xiasenhai thanks for your reply.
I cannot find the method "mapDeviceCoordsToPage" in the class PDFiumCore.
I see, that you did some commit, but those changes are not available in the recent project which I am using.
Version of pdfium-android: '1.9.0'
@xiasenhai thanks for your reply.
I cannot find the method "mapDeviceCoordsToPage" in the class PDFiumCore.
I see, that you did some commit, but those changes are not available in the recent project which I am using.
Version of pdfium-android: '1.9.0'
@SagarKhurana were you able to access the method "mapDeviceCoordsToPage" ?
I've been trying but it seems that this method is exposed in a pull request which hasnt been merged. So it isnt available in Version of pdfium-android: '1.9.0'.
If you were able to found a workaround to this issue, please help me. I really need it for a project im working on.
Thanks!!
@Lucasq95 Sorry for the little late reply.
but Yes, I was able to resolve my issue with the below code.
float xPositionInRealScale = pdfView.toRealScale(-pdfView.getCurrentXOffset() + e.getX());
float yPositionInRealScale = pdfView.toRealScale(-pdfView.getCurrentYOffset() + e.getY());
if (pdfView.isSwipeVertical()) {
xPositionInRealScale = xPositionInRealScale - pdfView.pdfFile.getSecondaryPageOffset(currentPage, 1);
yPositionInRealScale = yPositionInRealScale - pdfView.pdfFile.getPageOffset(currentPage, 1);
} else {
xPositionInRealScale = xPositionInRealScale - pdfView.pdfFile.getPageOffset(currentPage, 1);
yPositionInRealScale = yPositionInRealScale - pdfView.pdfFile.getSecondaryPageOffset(currentPage, 1);
}
float xPer = xPositionInRealScale / pdfView.getPageSize(currentPage).getWidth() * 100;
float yPer = yPositionInRealScale / pdfView.getPageSize(currentPage).getHeight() * 100;
`
So my requirement was to create the canvas point on pdf page and recognize it after user single tap on it.
I converted the x-axis and y-axis to the width and height %.
for example, if the point is marked at the center then it is 50%:x and 50%:y so it will work on any device irrespective of the size of the screen or zoom level. Using the current offset and page offset I have made sure of that.
Hope this helps,
Let me know if you have any other doubt.
Thanks.
@SagarKhurana thanks a lot for the response. Thats a really interesting resolution.
In my case, i solved it by using mapDeviceCoordsToPage method. This method isnt available in version 1.9.0 of PdfiumAndroid because it only existed on a pull request that was closed for some reason, so i created my own fork of PdfiumAndroid and implemented the method as it was done in that pull request.
Then i also forked AndroidPdfViewer to integrate that method and it worked.
This forks are available in my profile but in the next days i will delete them and push some new forks with a cleaner code. If someone tries to use them, they are also available in jitpack so that you can add the dependency to the latest tag on gradle.
Convert the screen touch point to pdf page coordinate:
private PointF convertScreenPointToPdfPagePoint(MotionEvent e, PDFView pdfView) {
float x = e.getX();
float y = e.getY();
PdfFile pdfFile = pdfView.pdfFile;
if (pdfFile == null) {
return null;
}
float mappedX = -pdfView.getCurrentXOffset() + x;
float mappedY = -pdfView.getCurrentYOffset() + y;
int page = pdfFile.getPageAtOffset(pdfView.isSwipeVertical() ? mappedY : mappedX, pdfView.getZoom());
SizeF pageSize = pdfFile.getScaledPageSize(page, pdfView.getZoom());
int pageX, pageY;
if (pdfView.isSwipeVertical()) {
pageX = (int) pdfFile.getSecondaryPageOffset(page, pdfView.getZoom());
pageY = (int) pdfFile.getPageOffset(page, pdfView.getZoom());
} else {
pageY = (int) pdfFile.getSecondaryPageOffset(page, pdfView.getZoom());
pageX = (int) pdfFile.getPageOffset(page, pdfView.getZoom());
}
return pdfFile.mapDeviceCoordsToPage(page, pageX, pageY, (int) pageSize.getWidth(),
(int) pageSize.getHeight(), (int) mappedX, (int) mappedY);
}
incase someone really need it.
mapDeviceCoordsToPage() can be found in @xiasenhai 's PR HERE
@benjinus thanks
Most helpful comment
@Lucasq95 Sorry for the little late reply.
but Yes, I was able to resolve my issue with the below code.
`
So my requirement was to create the canvas point on pdf page and recognize it after user single tap on it.
I converted the x-axis and y-axis to the width and height %.
for example, if the point is marked at the center then it is 50%:x and 50%:y so it will work on any device irrespective of the size of the screen or zoom level. Using the current offset and page offset I have made sure of that.
Hope this helps,
Let me know if you have any other doubt.
Thanks.