getSelectedTextLines method
Gets the selected text lines in the SfPdfViewer.
This example demonstrates how to highlight the selected text in the SfPdfViewer.
class MyAppState extends State<MyApp>{
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
final PdfViewerController _pdfViewerController = PdfViewerController();
final String _pdfPath = 'assets/sample.pdf';
Uint8List ?_memoryBytes;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Syncfusion Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.check_box_outline_blank_outlined,
color: Colors.white,
),
onPressed: () async {
final ByteData data = await rootBundle.load(_pdfPath);
final PdfDocument ?document = PdfDocument(
inputBytes: data.buffer
.asUint8List(data.offsetInBytes, data.lengthInBytes));
if (document != null) {
_pdfViewerKey.currentState!.getSelectedTextLines().forEach((pdfTextline) {
final PdfPage _page = document.pages[pdfTextline.pageNumber];
final PdfRectangleAnnotation rectangleAnnotation =
PdfRectangleAnnotation(pdfTextline.bounds,
'Rectangle Annotation',
author: 'Syncfusion',
color: PdfColor.fromCMYK(0, 0, 255, 0),
innerColor: PdfColor.fromCMYK(0, 0, 255, 0),
opacity: 0.5,);
_page.annotations.add(rectangleAnnotation);
_page.annotations.flattenAllAnnotations();
});
final List<int> bytes = document.save();
_memoryBytes = Uint8List.fromList(bytes);
}},),
IconButton(
icon: Icon(
Icons.file_upload,
color: Colors.white,
),
onPressed: () {
_pdfViewerController.clearSelection();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SafeArea(
child: SfPdfViewer.memory(
_memoryBytes!,
),
)),);},),],),
body: SfPdfViewer.asset(
_pdfPath,
key: _pdfViewerKey,
controller: _pdfViewerController,
),);
}
}
Implementation
List<PdfTextLine> getSelectedTextLines() {
final List<PdfTextLine>? selectedTextLines =
_pdfPagesKey[_pdfViewerController.pageNumber]
?.currentState
?.canvasRenderBox
?.getSelectedTextLines();
return selectedTextLines ?? <PdfTextLine>[];
}