【发布时间】:2017-12-28 11:32:39
【问题描述】:
我正在使用 PdfTron SDK,并且我正在尝试在用户放大后绘制注释,注释需要在书的一侧(第三张图片)但是当我们放大时它正在绘制书的中心(图 1 和图 2)。
缩放示例(错误状态):
没有缩放的例子(正确的状态):
现在我正在使用函数 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();
}
}
【问题讨论】:
-
您能否发布用于将注释添加到页面的代码。此外,请包括您用于将其放在右侧的 x,y 值。你如何确定这些坐标?
-
嗨 Ryan,我正在编辑我的帖子,请检查一下。
-
澄清一下,当您第一次添加注释时,它位于页面右侧,即您想要的位置。但是当你放大时,它会“跳”到页面的中间?从那以后它总是在同一个位置?还是继续跳来跳去?在前两个屏幕截图中,注释位于同一位置。
-
不,注释不是“跳转”到页面中间的。在前两个屏幕截图中,我在放大后添加了注释,第三个没有放大。我希望注释始终像第三个屏幕截图一样绘制(即使在放大之后)。
-
在这种情况下,注释对您不起作用,因为它们是相对于页面定位的。因此,当用户缩放/平移时,注释会移动(以便始终位于页面上的同一位置)。用户向上/向下滚动而不是缩放会发生什么?您希望注释保持静止,还是注释也向上向下移动?目前还不清楚你想在缩放/平移上发生什么。也许另一个屏幕截图显示了您想要发生的事情。
标签: android annotations pdftron