Android - PDFTron - 以放大模式绘制注释

时间:2017-12-28 11:32:39

标签: android annotations pdftron

我正在使用PdfTron SDK并且我试图在用户进行放大后绘制注释,注释需要在书的侧面(第三张图片)但是当我们放大它的绘图时这本书的中心(图1和2)。

缩放示例(错误状态):

enter image description here

enter image description here

没有缩放的示例(正确的状态):

enter image description here

现在我正在使用 convPagePtToScreenPt 功能,但是如果用户没有放大,它正在正确地绘制注释。 有人知道假设使用哪种功能吗?

这是我的代码:

public synchronized void drawAnnotation(AnnotationData annotationData){

    if (annotationData == null) {
        return;
    }   


    AnnotationType annotationType = annotationData.getType();

    if (annotationType == null) {
        return;
    }

    ToolManager.Tool tool = mToolManager.createTool(ToolManager.e_text_annot_create, null);
        if (tool instanceof StickyNoteCreate) {
            StickyNoteCreate annotStickyCreate = (StickyNoteCreate) tool;
            Point point;
            double[] pts;
            double[] ptsForScreenSize = {0, 0};
            int orientation = mContext.getResources().getConfiguration().orientation;
            if (mPDFView != null) {
                if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                     ptsForScreenSize = mPDFView.convScreenPtToPagePt((double) BookReader.SCREEN_WIDTH, (double) BookReader.SCREEN_HEIGHT, annotationData.getPage());
                } else {
                    ptsForScreenSize = mPDFView.convScreenPtToPagePt((double) BookReader.SCREEN_HEIGHT, (double) BookReader.SCREEN_WIDTH, annotationData.getPage());
                    if (!TextUtils.isEmpty(annotationData.getStartLoc()) && !TextUtils.isEmpty(annotationData.getEndLoc()) && mPDFView != null) {
                        //if we have an annotation for text
                        pts = mPDFView.convPagePtToScreenPt(annotationData.getStartX(), annotationData.getStartY(), annotationData.getPage());
                    } else {
                        //if we have an annotation for Page
                        pts = new double[]{0, 0};
                    }                   
                    ptsForScreenSize = mPDFView.convPagePtToScreenPt(ptsForScreenSize[0] - INT_ANNO_PADDING, ptsForScreenSize[1], annotationData.getPage());
                    final AnnotationData noteTextHighlight = new AnnotationData(annotationData);
                    //we don't need to set UniqueId for this highlight annotation
                    noteTextHighlight.setUniqueId(null);
                    highlightSelectedText(noteTextHighlight);
                    double marginY = BookReader.SCREEN_HEIGHT * 0.015;
                    point = new Point(ptsForScreenSize[0], pts[1] + marginY);
                    annotStickyCreate.createNoteIconOnPage(annotationData, point);
                }
            }
    }       
}


  public void createNoteIconOnPage(AnnotationData annotationData, Point noteIconPoint) {
    KsLog.d("IsBookReaderAviliable","createNoteIconOnPage : " + BookReader.isBookReaderVisible());
    if(BookReader.isBookReaderVisible()){
        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);

            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);
            }

            KsLog.d("createNoteIconOnPage","getPage() " + annotationData.getPage());
            Page page = pdfDoc.getPage(annotationData.getPage());
            if (page != null) {
                page.annotPushBack(text);
            }


            mAnnotPushedBack = true;
            mAnnot = text;
            mAnnotPageNum = annotationData.getPage();
            KsLog.d("createNoteIconOnPage","mDownPageNum " + mAnnotPageNum);

            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();
    }
}

1 个答案:

答案 0 :(得分:1)

以下代码会在任何页面的右侧放置一个注释,距离顶部1英寸。

Matrix2D mtx = page.getDefaultMatrix(true).inverse();
double x1 = page.getPageWidth() - 25; // this is the width as the a user sees it (rotated). 20 is the width of a Text annotation
double y1 = 72; // not clear how you decide the vertical placement. In this example, this 1 inch from the top as the user views the page. If you have the value from the bottom, switch the boolean value argument in GetDefaultMatrix
mtx.mult(ref x1, ref y1);
com.pdftron.pdf.Annots.Text txt = com.pdftron.pdf.Text.create(doc, new Point(x1, y1));
page.annotPushBack(txt);

关于您的代码,屏幕和页面坐标似乎存在混淆。例如,对于ptsForScreenSize,您在ConvScreenPtToPagePt的结果中传递ConvPagePtToScreenPt,在另一行中传递tf.random_normal的结果。