PDFTron Ink Annotation

Hello, I have requirement of saving draw annotation points to db and send it to server. And also delete them from db as soon as erase is triggered. I am able to get draw points on commitAnnotation()

public void commitAnnotation() {
       
      ArrayList<DrawPoints> points=new ArrayList<DrawPoints>();
       
        try {
            mPDFView.docLock(true);
            pdftron.PDF.Rect rect = getShapeBBox();
            if (rect != null) {
                float sx = mPDFView.getScrollX();
                float sy = mPDFView.getScrollY();
                pdftron.PDF.Annots.Ink ink = pdftron.PDF.Annots.Ink.create(mPDFView.getDoc(), rect);

                int pathIdx = 0;
                ListIterator<LinkedList<PointF>> itrPaths = mStrokes.listIterator(0);
                while (itrPaths.hasNext()) {
                    LinkedList<PointF> path = itrPaths.next();
                    ListIterator<PointF> itr = path.listIterator(0);
                    double[] pts = new double[2];
                    pdftron.PDF.Point p = new pdftron.PDF.Point();
                    int pointIdx = 0;
                    while (itr.hasNext()) {
                        PointF p_ = itr.next();
                        DrawPoints drawPoints=new DrawPoints();
                        pts = mPDFView.convScreenPtToPagePt(p_.x - sx, p_.y - sy, mPageForFreehandAnnot);
                        p.x = pts[0];
                        p.y = pts[1];
                        ink.setPoint(pathIdx, pointIdx++, p);
                        drawPoints.setX(p.x);
                        drawPoints.setY(p.y);
                        drawPoints.setPathindex(pathIdx);
                        drawPoints.setPointindex(pointIdx);
                        points.add(drawPoints);
                    }
                    pathIdx++;
                }

                setStyle(ink);
                
                ink.refreshAppearance();

                Page page = mPDFView.getDoc().getPage(mPageForFreehandAnnot);
                page.annotPushBack(ink);

                mAnnot = ink;
                mAnnotPageNum = mPageForFreehandAnnot;
                buildAnnotBBox();

                mPDFView.update(mAnnot, mAnnotPageNum); // Update the region where the annotation occupies.
            }
            
            mPaint.setStrokeJoin(oldStrokeJoin);
            mPaint.setStrokeCap(oldStrokeCap);

        } catch (Exception ex) {
            mNextToolMode = ToolManager.e_pan;
            
        } finally {
            mPDFView.docUnlock();
            
            mStrokePoints.clear();
            mStrokes.clear();
        }
        if(mListener!=null){
          mListener.getStrokePoints(points);
        }else{
          Log.e(TAG, " No Listener Registered ");
        }
        mPDFView.waitForRendering();
    }

But when I try to erase, points drawn and erased points differ.

Here is the erased method where I added log

public boolean onMove(MotionEvent e1, MotionEvent e2, float x_dist, float y_dist) {

        float x = e2.getX() + mPDFView.getScrollX();
        float y = e2.getY() + mPDFView.getScrollY();

        // Don't allow the annotation to go beyond the page
        if (mPageCropOnClientF != null) {
            if (x < mPageCropOnClientF.left) {
                x = mPageCropOnClientF.left;
            } else if (x > mPageCropOnClientF.right) {
                x = mPageCropOnClientF.right;
            }
            if (y < mPageCropOnClientF.top) {
                y = mPageCropOnClientF.top;
            } else if (y > mPageCropOnClientF.bottom) {
                y = mPageCropOnClientF.bottom;
            }
        }

        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;

            mStrokePoints.add(new PointF(x, y));

            float sx = mPDFView.getScrollX();
            float sy = mPDFView.getScrollY();

            mCurrentPt.x = x - sx;
            mCurrentPt.y = y - sy;

            mPt1.x = Math.min(Math.min(x, mPt1.x), mPt1.x);
            mPt1.y = Math.min(Math.min(y, mPt1.y), mPt1.y);
            mPt2.x = Math.max(Math.max(x, mPt2.x), mPt2.x);
            mPt2.y = Math.max(Math.max(y, mPt2.y), mPt2.y);

            mPt1BBox.x = Math.min(Math.min(mPt1.x, mPt1BBox.x), mPt1BBox.x);
            mPt1BBox.y = Math.min(Math.min(mPt1.y, mPt1BBox.y), mPt1BBox.y);
            mPt2BBox.x = Math.max(Math.max(mPt2.x, mPt2BBox.x), mPt2BBox.x);
            mPt2BBox.y = Math.max(Math.max(mPt2.y, mPt2BBox.y), mPt2BBox.y);

            float min_x = mPt1BBox.x - mThicknessDraw;
            float max_x = mPt2BBox.x + mThicknessDraw;
            float min_y = mPt1BBox.y - mThicknessDraw;
            float max_y = mPt2BBox.y + mThicknessDraw;

            mPDFView.invalidate((int)min_x, (int)min_y, (int) FloatMath.ceil(max_x), (int)FloatMath.ceil(max_y));

            // Erase
            double[] pt1points = mPDFView.convScreenPtToPagePt(mPrevPt.x, mPrevPt.y, mDownPageNum);
            double[] pt2points = mPDFView.convScreenPtToPagePt(mCurrentPt.x, mCurrentPt.y, mDownPageNum);
            Point pdfPt1 = new Point(pt1points[0], pt1points[1]);
            Point pdfPt2 = new Point(pt2points[0], pt2points[1]);

            try {
                mPDFView.docLock(true);
                Page page = mPDFView.getDoc().getPage(mDownPageNum);
                int annot_num = page.getNumAnnots();
                for (int i=annot_num-1;i>=0;--i) {
                    Annot annot = page.getAnnot(i);
                    if (!annot.isValid())
                        continue;
                    if (annot.getType() == Annot.e_Ink) {
                        Ink ink = new Ink(annot);
                        if (ink.erase(pdfPt1, pdfPt2, mEraserHalfThickness)) {
// RectF rect=new RectF((float)pdfPt1.x, (float)pdfPt1.y, (float)pdfPt2.x, (float)pdfPt2.y);
// double xcord=(pdfPt1.x)+(pdfPt2.x-pdfPt1.x);
// double ycord=(pdfPt1.y)+(pdfPt2.y-pdfPt1.y);
                          Log.i("Erased", "Erased onMove "+pdfPt1.x+" "+pdfPt1.y+" "+pdfPt2.x+" "+pdfPt2.y);
// Log.i("Erased ", "Erased proc x "+xcord+" y "+ycord);
// Log.i("Erased", "Erased onMove "+(float)pdfPt2.x+" "+(float)pdfPt2.y);
// Log.i("Erased ","Rect centerx "+rect.centerX()+" Rect centery "+rect.centerY());
                          /* double[] pts = new double[2];
                           pts=mPDFView.convScreenPtToPagePt(pdfPt1.x, pdfPt1.y, mDownPageNum);
                           for (int j = 0; j < pts.length; j++) {
                Log.i("Erased ", "Erased converted pts "+pts[0]+" "+pts[1]);
              }*/
// DrawPoints points=new DrawPoints();
                            if (!mInkList.contains(ink)) {
                                mInkList.add(ink);
                                mDoUpdate = true;
                            }
                        }

                    }

                }
            } catch (Exception ex) {

            } finally {
                mPDFView.docUnlock();
                mPrevPt.x = mCurrentPt.x;
                mPrevPt.y = mCurrentPt.y;
            }

        }

        return true;
    }

My Log cat for drawn points and erased points

03-07 10:59:38.195: E/(3089): Draw Points x 52.299402940790685 y 589.9475197447107 pathIndex 0 pointIndex 1
03-07 10:59:38.195: E/(3089): Draw Points x 63.144005123706506 y 588.3935750290891 pathIndex 0 pointIndex 2
03-07 10:59:38.199: E/(3089): Draw Points x 66.81837741932964 y 587.5599369857464 pathIndex 0 pointIndex 3
03-07 10:59:38.199: E/(3089): Draw Points x 70.43296010118843 y 587.357617157794 pathIndex 0 pointIndex 4
03-07 10:59:38.199: E/(3089): Draw Points x 75.16016738924066 y 586.2787784285849 pathIndex 0 pointIndex 5
03-07 10:59:38.203: E/(3089): Draw Points x 77.78309661995092 y 585.285701415204 pathIndex 0 pointIndex 6
03-07 10:59:38.203: E/(3089): Draw Points x 79.90088270114325 y 585.285701415204 pathIndex 0 pointIndex 7
03-07 10:59:38.203: E/(3089): Draw Points x 81.54386332572773 y 583.7317566995823 pathIndex 0 pointIndex 8
03-07 10:59:38.207: E/(3089): Draw Points x 87.42159361007361 y 583.7317566995823 pathIndex 0 pointIndex 9
03-07 10:59:38.207: E/(3089): Draw Points x 91.14993473166606 y 583.7317566995823 pathIndex 0 pointIndex 10
03-07 10:59:38.207: E/(3089): Draw Points x 95.51476634476029 y 583.7317566995823 pathIndex 0 pointIndex 11
03-07 10:59:38.207: E/(3089): Draw Points x 99.44661359616538 y 583.7317566995823 pathIndex 0 pointIndex 12
03-07 10:59:38.207: E/(3089): Draw Points x 103.89189229275314 y 583.7317566995823 pathIndex 0 pointIndex 13
03-07 10:59:38.211: E/(3089): Draw Points x 110.0055859258124 y 583.7317566995823 pathIndex 0 pointIndex 14
03-07 10:59:38.211: E/(3089): Draw Points x 113.34020136861596 y 582.790861150701 pathIndex 0 pointIndex 15
03-07 10:59:38.215: E/(3089): Draw Points x 113.95340870893756 y 582.1778278013189 pathIndex 0 pointIndex 16
03-07 10:59:38.215: E/(3089): Draw Points x 117.11713341441049 y 582.6957988282874 pathIndex 0 pointIndex 17
03-07 10:59:38.215: E/(3089): Draw Points x 121.46583132220326 y 582.6957988282874 pathIndex 0 pointIndex 18
03-07 10:59:38.219: E/(3089): Draw Points x 122.35758233934763 y 582.6957988282874 pathIndex 0 pointIndex 19
03-07 10:59:38.219: E/(3089): Draw Points x 123.456635652258 y 582.6957988282874 pathIndex 0 pointIndex 20
03-07 10:59:38.223: E/(3089): Draw Points x 124.31542329504128 y 582.6957988282874 pathIndex 0 pointIndex 21
03-07 10:59:38.223: E/(3089): Draw Points x 127.18364811576674 y 583.7317566995823 pathIndex 0 pointIndex 22
03-07 10:59:38.223: E/(3089): Draw Points x 132.77065535752314 y 583.7317566995823 pathIndex 0 pointIndex 23
03-07 10:59:38.223: E/(3089): Draw Points x 133.6412111147617 y 583.7317566995823 pathIndex 0 pointIndex 24
03-07 10:59:38.227: E/(3089): Draw Points x 136.05607881651525 y 585.285701415204 pathIndex 0 pointIndex 25
03-07 10:59:38.235: E/(3089): Draw Points x 141.33214843198078 y 585.285701415204 pathIndex 0 pointIndex 26
03-07 10:59:38.235: E/(3089): Draw Points x 141.9308670722474 y 585.285701415204 pathIndex 0 pointIndex 27
03-07 10:59:38.243: E/(3089): Draw Points x 142.45815452315952 y 585.285701415204 pathIndex 0 pointIndex 28
03-07 10:59:38.243: E/(3089): Draw Points x 144.00322570086536 y 585.285701415204 pathIndex 0 pointIndex 29
03-07 10:59:38.243: E/(3089): Draw Points x 145.1125919312821 y 585.285701415204 pathIndex 0 pointIndex 30
03-07 10:59:38.247: E/(3089): Draw Points x 146.70571624301334 y 585.285701415204 pathIndex 0 pointIndex 31
03-07 10:59:38.247: E/(3089): Draw Points x 148.32275640025028 y 585.285701415204 pathIndex 0 pointIndex 32
03-07 10:59:38.247: E/(3089): Draw Points x 151.25668652668406 y 585.285701415204 pathIndex 0 pointIndex 33
03-07 10:59:38.251: E/(3089): Draw Points x 153.66174746639166 y 586.321659286499 pathIndex 0 pointIndex 34
03-07 10:59:38.251: E/(3089): Draw Points x 156.08648519962384 y 586.321659286499 pathIndex 0 pointIndex 35
03-07 10:59:38.251: E/(3089): Draw Points x 156.9558072029276 y 586.321659286499 pathIndex 0 pointIndex 36
03-07 10:59:38.251: E/(3089): Draw Points x 158.2375826370564 y 586.321659286499 pathIndex 0 pointIndex 37
03-07 10:59:38.255: E/(3089): Draw Points x 162.0338118597801 y 586.321659286499 pathIndex 0 pointIndex 38
03-07 10:59:38.255: E/(3089): Draw Points x 162.6182948777219 y 586.321659286499 pathIndex 0 pointIndex 39
03-07 10:59:38.259: E/(3089): Draw Points x 163.17297799293027 y 587.3193549684565 pathIndex 0 pointIndex 40
03-07 10:59:38.259: E/(3089): Draw Points x 165.50439331314413 y 587.357617157794 pathIndex 0 pointIndex 41

03-07 11:00:27.715: I/Erased(3089): Erased onMove 44.00977861802119 592.0194354873008 53.10653109189292 592.0194354873008
03-07 11:00:27.743: I/Erased(3089): Erased onMove 53.10653109189292 592.0194354873008 54.099671374706475 592.0194354873008
03-07 11:00:27.775: I/Erased(3089): Erased onMove 54.099671374706475 592.0194354873008 57.24378255608735 592.0194354873008
03-07 11:00:27.787: I/Erased(3089): Erased onMove 57.24378255608735 592.0194354873008 58.09896384121504 592.0194354873008
03-07 11:00:27.815: I/Erased(3089): Erased onMove 58.09896384121504 592.0194354873008 60.14478094287625 592.0194354873008
03-07 11:00:27.847: I/Erased(3089): Erased onMove 60.14478094287625 592.0194354873008 63.74655156464269 592.0194354873008
03-07 11:00:27.879: I/Erased(3089): Erased onMove 63.74655156464269 592.0194354873008 65.25188953870406 592.0194354873008
03-07 11:00:28.031: I/Erased(3089): Erased onMove 65.25188953870406 592.0194354873008 69.09683622449506 592.0194354873008
03-07 11:00:28.047: I/Erased(3089): Erased onMove 69.09683622449506 592.0194354873008 70.82364884721466 590.9834776160058
03-07 11:00:28.071: I/Erased(3089): Erased onMove 70.82364884721466 590.9834776160058 73.38504855476538 590.9834776160058
03-07 11:00:28.119: I/Erased(3089): Erased onMove 73.38504855476538 590.9834776160058 76.96726892187235 590.9834776160058
03-07 11:00:28.151: I/Erased(3089): Erased onMove 76.96726892187235 590.9834776160058 78.0569899934809 590.9834776160058
03-07 11:00:28.167: I/Erased(3089): Erased onMove 78.0569899934809 590.9834776160058 78.72255278924166 590.3397744091926
03-07 11:00:28.187: I/Erased(3089): Erased onMove 78.72255278924166 590.3397744091926 79.34499746671634 589.9475197447107
03-07 11:00:28.231: I/Erased(3089): Erased onMove 79.34499746671634 589.9475197447107 80.65527578021107 589.9475197447107
03-07 11:00:28.267: I/Erased(3089): Erased onMove 80.65527578021107 589.9475197447107 82.77157502973836 589.9475197447107
03-07 11:00:28.299: I/Erased(3089): Erased onMove 82.77157502973836 589.9475197447107 83.66933664297545 587.9678192000694
03-07 11:00:28.315: I/Erased(3089): Erased onMove 83.66933664297545 587.9678192000694 85.51557031964239 588.3935750290891
03-07 11:00:28.363: I/Erased(3089): Erased onMove 85.51557031964239 588.3935750290891 86.94055611435908 588.3935750290891
03-07 11:00:28.387: I/Erased(3089): Erased onMove 86.94055611435908 588.3935750290891 88.38069493817267 588.3935750290891
03-07 11:00:28.419: I/Erased(3089): Erased onMove 88.38069493817267 588.3935750290891 89.33717058482063 588.3935750290891
03-07 11:00:28.439: I/Erased(3089): Erased onMove 89.33717058482063 588.3935750290891 90.14787345886225 588.3935750290891
03-07 11:00:28.467: I/Erased(3089): Erased onMove 90.14787345886225 588.3935750290891 92.48422379481536 588.3935750290891
03-07 11:00:28.487: I/Erased(3089): Erased onMove 92.48422379481536 588.3935750290891 93.45597900942533 588.3935750290891
03-07 11:00:28.523: I/Erased(3089): Erased onMove 93.45597900942533 588.3935750290891 94.98384090147624 588.3935750290891
03-07 11:00:28.543: I/Erased(3089): Erased onMove 94.98384090147624 588.3935750290891 96.88375869166595 588.3935750290891
03-07 11:00:28.647: I/Erased(3089): Erased onMove 96.88375869166595 588.3935750290891 97.68604673117778 588.3935750290891
03-07 11:00:28.671: I/Erased(3089): Erased onMove 97.68604673117778 588.3935750290891 99.75625419908886 588.3935750290891
03-07 11:00:28.703: I/Erased(3089): Erased onMove 99.75625419908886 588.3935750290891 102.55516735645045 588.3935750290891
03-07 11:00:28.727: I/Erased(3089): Erased onMove 102.55516735645045 588.3935750290891 103.29383798152838 588.3935750290891
03-07 11:00:28.759: I/Erased(3089): Erased onMove 103.29383798152838 588.3935750290891 105.11931928431733 589.4031653643669
03-07 11:00:28.791: I/Erased(3089): Erased onMove 105.11931928431733 589.4031653643669 107.21806126631066 589.9475197447107
03-07 11:00:28.819: I/Erased(3089): Erased onMove 107.21806126631066 589.9475197447107 108.06574512368059 589.9475197447107
03-07 11:00:28.947: I/Erased(3089): Erased onMove 108.06574512368059 589.9475197447107 110.01153325247253 589.9475197447107
03-07 11:00:28.967: I/Erased(3089): Erased onMove 110.01153325247253 589.9475197447107 111.88101844560333 589.9475197447107
03-07 11:00:29.011: I/Erased(3089): Erased onMove 111.88101844560333 589.9475197447107 113.42782953270444 589.9475197447107
03-07 11:00:29.031: I/Erased(3089): Erased onMove 113.42782953270444 589.9475197447107 114.42641098671766 589.9475197447107
03-07 11:00:29.059: I/Erased(3089): Erased onMove 114.42641098671766 589.9475197447107 116.75368215909913 589.9475197447107
03-07 11:00:29.087: I/Erased(3089): Erased onMove 116.75368215909913 589.9475197447107 118.26240504780219 589.9475197447107
03-07 11:00:29.103: I/Erased(3089): Erased onMove 118.26240504780219 589.9475197447107 119.38730392391119 589.9475197447107
03-07 11:00:29.139: I/Erased(3089): Erased onMove 119.38730392391119 589.9475197447107 121.49113909522521 589.9475197447107
03-07 11:00:29.167: I/Erased(3089): Erased onMove 121.49113909522521 589.9475197447107 122.9164096023884 589.9475197447107
03-07 11:00:29.203: I/Erased(3089): Erased onMove 122.9164096023884 589.9475197447107 124.08433169263469 589.9475197447107
03-07 11:00:29.235: I/Erased(3089): Erased onMove 124.08433169263469 589.9475197447107 126.46180715974839 589.9475197447107
03-07 11:00:29.255: I/Erased(3089): Erased onMove 126.46180715974839 589.9475197447107 127.53776712977634 589.9475197447107
03-07 11:00:29.271: I/Erased(3089): Erased onMove 127.53776712977634 589.9475197447107 128.63571322761692 589.9475197447107
03-07 11:00:29.307: I/Erased(3089): Erased onMove 128.63571322761692 589.9475197447107 131.48135086092037 589.9475197447107
03-07 11:00:29.343: I/Erased(3089): Erased onMove 131.48135086092037 589.9475197447107 133.12312936628626 589.0118913760896
03-07 11:00:29.387: I/Erased(3089): Erased onMove 133.12312936628626 589.0118913760896 134.88837816928293 588.3935750290891
03-07 11:00:29.415: I/Erased(3089): Erased onMove 134.88837816928293 588.3935750290891 137.57210932467848 588.3935750290891
03-07 11:00:29.439: I/Erased(3089): Erased onMove 137.57210932467848 588.3935750290891 139.67553324468082 587.8248460998535
03-07 11:00:29.467: I/Erased(3089): Erased onMove 139.67553324468082 587.8248460998535 141.0385149954937 587.357617157794
03-07 11:00:29.483: I/Erased(3089): Erased onMove 141.0385149954937 587.357617157794 143.19986208100016 587.357617157794
03-07 11:00:29.503: I/Erased(3089): Erased onMove 143.19986208100016 587.357617157794 144.00322570086536 587.357617157794
03-07 11:00:29.547: I/Erased(3089): Erased onMove 144.00322570086536 587.357617157794 146.6449775877607 587.357617157794
03-07 11:00:29.575: I/Erased(3089): Erased onMove 146.6449775877607 587.357617157794 148.7199618978297 587.357617157794
03-07 11:00:29.599: I/Erased(3089): Erased onMove 148.7199618978297 587.357617157794 150.4794848171802 587.357617157794
03-07 11:00:29.643: I/Erased(3089): Erased onMove 150.4794848171802 587.357617157794 151.43244901032136 587.357617157794
03-07 11:00:29.671: I/Erased(3089): Erased onMove 151.43244901032136 587.357617157794 153.3290767900183 587.357617157794
03-07 11:00:29.707: I/Erased(3089): Erased onMove 153.3290767900183 587.357617157794 154.99660595443396 586.321659286499
03-07 11:00:29.727: I/Erased(3089): Erased onMove 154.99660595443396 586.321659286499 155.85333734065927 586.321659286499
03-07 11:00:29.767: I/Erased(3089): Erased onMove 155.85333734065927 586.321659286499 156.56309383505965 586.321659286499
03-07 11:00:29.799: I/Erased(3089): Erased onMove 156.56309383505965 586.321659286499 156.4376938197359 587.6069145394183
03-07 11:00:29.835: I/Erased(3089): Erased onMove 156.4376938197359 587.6069145394183 158.03860027167133 587.357617157794
03-07 11:00:29.871: I/Erased(3089): Erased onMove 158.03860027167133 587.357617157794 159.63925364587664 587.357617157794
03-07 11:00:29.887: I/Erased(3089): Erased onMove 159.63925364587664 587.357617157794 160.34572012978407 587.357617157794
03-07 11:00:29.939: I/Erased(3089): Erased onMove 160.34572012978407 587.357617157794 161.2933064211581 587.357617157794
03-07 11:00:29.967: I/Erased(3089): Erased onMove 161.2933064211581 587.357617157794 161.10058772959604 588.6510816195873
03-07 11:00:30.015: I/Erased(3089): Erased onMove 161.10058772959604 588.6510816195873 162.28594054851118 588.3935750290891
03-07 11:00:30.095: I/Erased(3089): Erased onMove 162.28594054851118 588.3935750290891 163.2763602457249 588.3935750290891
03-07 11:00:30.163: I/Erased(3089): Erased onMove 163.2763602457249 588.3935750290891 164.92199818647663 588.3935750290891
03-07 11:00:30.279: I/Erased(3089): Erased onMove 164.92199818647663 588.3935750290891 164.7272548730728 589.9973127881315

How can I achieve this?

I think this has been answered in other posts, including StackOverflow.

In general I would avoid doing logic based on coordinates.