1
votes

I'm trying to draw annotation and in 80% of the time the annotation draw properly but sometimes the annotation is cut in the middle and after i do zoom in and do zoom out the annotation is drawing completely.

this is the code where i draw the annotation:

public void createNoteIconOnPage(AnnotationData annotationData, Point noteIconPoint) {
    try {
        mPDFView.docLock(true);
        PDFDoc pdfDoc = mPDFView.getDoc();
        double[] pts = mPDFView.convScreenPtToPagePt(noteIconPoint.x, noteIconPoint.y, annotationData.getPage());
        Point p = new Point(pts[0], pts[1]);
        com.pdftron.pdf.annots.Text text = com.pdftron.pdf.annots.Text.create(pdfDoc, p);
        text.setUniqueID(annotationData.getUniqueId());
        //creating the annotation appearance - icon

        // Let's create an appearance for the annotation using an image
        ElementBuilder builder = new ElementBuilder();
        ElementWriter writer = new ElementWriter();
        writer.begin(pdfDoc);

        Bitmap imageBitmap = Bitmap.createBitmap(150, 150, Bitmap.Config.ARGB_8888);
        imageBitmap.eraseColor(Color.RED);
        Image image = Image.create(mPDFView.getDoc(), imageBitmap);

    //    Image image = Image.create(pdfDoc, annotationData.getDrawable());

        int w = image.getImageWidth(), h = image.getImageHeight();

        Element element = builder.createImage(image, 0, 0, w, h);

        writer.writePlacedElement(element);

        writer.writeElement(builder.createTextBegin(Font.create(pdfDoc, Font.e_times_roman), 12));
        writer.writeElement(element);
        writer.writeElement(builder.createTextEnd());

        Obj appearance = writer.end();
        appearance.putRect("BBox", 0.1, 0.1, w, h);
        text.setAppearance(appearance);

        /*
        The left icons spouse to be bigger the the regular icons
         */
        if(annotationData.getType() == AnnotationData.AnnotationType.LINK && (annotationData.getShard() == AnnotationData.LEFT_LINK_A || annotationData.getShard() == AnnotationData.LEFT_LINK_B)){
            text.setRect(new Rect(pts[0],pts[1],pts[0] + 30,pts[1] + 30));
        }

        if (annotationData.getType() == AnnotationData.AnnotationType.NOTE) {
            text.setContents(AnnotationData.NOTE_TYPE_CONTENTS);
        } else if (annotationData.getType() == AnnotationData.AnnotationType.LINK) {
            text.setContents(AnnotationData.LINK_TYPE_CONTENTS);
        }

        Page page = pdfDoc.getPage(annotationData.getPage());
        if (page != null) {
            page.annotPushBack(text);
        }


        mAnnotPushedBack = true;
        mAnnot = text;
        mAnnotPageNum = mDownPageNum;
        buildAnnotBBox();

        mPDFView.update(mAnnot, mAnnotPageNum);
        raiseAnnotationAddedEvent(mAnnot, mAnnotPageNum);

    } catch (Exception ex) {
        Log.e(PDFTronReader.TAG, ex.toString());
        mNextToolMode = ToolManager.e_pan;

    } finally {
        mPDFView.docUnlock();
    }
    mPDFView.waitForRendering();
}

and this is how it's look:enter image description here

as you can see, we have 3 annotations in the left size (3 red squares) and the first one is cut in the middle

does somebody knows why it's happen? thanks you all.

1

1 Answers

2
votes

I think there is a discrepancy between what annotationData.getPage() returns and what mAnnotPageNum is.

I would consolidate this code around one of those two variables. That is, use one or the other. I suspect 20% of the time they are not the same number.

Furthermore, Text annots only have a position, as their size is fixed. They don't scale the same as normal annotations. So I would comment out the call to buildAnnotBBox.

Other things I would change.

  1. As mentioned above, the size is fixed. So remove the following line text.setRect(new Rect(pts[0],pts[1],pts[0] + 30,pts[1] + 30));

  2. You set the BBox origin at 0.1, when this should be 0 based on your custom appearance.