annotateSelection method
Implementation
Future<bool> annotateSelection(UDocSelection selection, UDocTextPage text) async {
if (selection.isEmpty) return false;
attach();
final UPdfEdit? edit = _edit;
if (edit == null) return false;
final UDocPageInfo info = viewer.pageInfo(selection.pageIndex);
final List<Rect> quads = selection.rects.map((Rect rect) => deviceRectToPdf(info, rect)).toList();
if (quads.isEmpty) return false;
Rect bounds = quads.first;
for (final Rect quad in quads) {
bounds = bounds.expandToInclude(quad);
}
final UPdfAnnotationStyle style;
switch (_tool) {
case UPdfTool.underline:
style = UPdfAnnotationStyle.underline;
break;
case UPdfTool.strikeOut:
style = UPdfAnnotationStyle.strikeOut;
break;
case UPdfTool.redact:
style = UPdfAnnotationStyle.redact;
break;
default:
style = UPdfAnnotationStyle.highlight;
break;
}
final int number = await edit.addAnnotation(
selection.pageIndex,
UPdfAnnotationSpec(style: style, rect: bounds.inflate(1), quads: quads, color: _color, opacity: _opacity, contents: selection.text, author: _author, rtl: UDocText.isRtl(selection.text)),
);
if (number > 0) _undoStack.add(number);
viewer.invalidatePage(selection.pageIndex);
notifyListeners();
return number > 0;
}