1

我正在尝试绘制注释,并且在 80% 的时间内注释正确绘制,但有时注释在中间被剪切,在我放大和缩小之后,注释完全绘制。

这是我绘制注释的代码:

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

这就是它的样子:在此处输入图像描述

如您所见,我们在左侧尺寸中有 3 个注释(3 个红色方块),第一个在中间被剪切

有人知道为什么会这样吗?谢谢大家。

4

1 回答 1

2

我认为annotationData.getPage()回报和实际回报之间存在差异mAnnotPageNum

我将围绕这两个变量之一整合这段代码。也就是说,使用其中一个。我怀疑 20% 的时间它们不是同一个数字。

此外,文本注释只有一个位置,因为它们的大小是固定的。它们的缩放比例与普通注释不同。所以我会注释掉对buildAnnotBBox.

其他事情我会改变。

  1. 如上所述,大小是固定的。所以删除以下行 text.setRect(new Rect(pts[0],pts[1],pts[0] + 30,pts[1] + 30));

  2. 您将BBox原点设置为0.1,根据您的自定义外观,该原点应为 0。

于 2017-05-23T21:33:07.757 回答