Right to Left (RTL) in Flutter PDF Viewer (SfPdfViewer)
5 Sep 20255 minutes to read
The SfPdfViewer supports right-to-left rendering. All the user interface elements will be rendered based on LTR and RTL direction, and functionalities like text search and copying text also support RTL languages.
RTL Rendering Ways
Right-to-left rendering can be switched in the following ways:
Wrapping the SfPdfViewer with Directionality Widget
To change the rendering direction from right to left, wrap the SfPdfViewer widget inside the Directionality widget and set the textDirection property as TextDirection.rtl.
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.bookmark,
color: Colors.white,
semanticLabel: 'Bookmark',
),
onPressed: () {
_pdfViewerKey.currentState?.openBookmarkView();
},
),
],
),
body: Directionality(
textDirection: TextDirection.rtl,
child: SfPdfViewer.network(
'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
key: _pdfViewerKey,
),
),
);
}Changing the Locale to RTL Languages
To change the SfPdfViewer rendering direction from right to left, change the locale to any of the RTL languages such as Arabic, Persian, Hebrew, Pashto, and Urdu.
To use flutter_localizations, add the package as a dependency in the pubspec.yaml file.
dependencies:
flutter_localizations:
sdk: flutterThen, import the flutter_localizations library, and specify localizationsDelegates and supportedLocales for MaterialApp.
final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const <Locale>[
Locale('en'),
Locale('ar'),
// ... other locales the app supports
],
locale: const Locale('ar'),
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter PDF Viewer'),
actions: <Widget>[
IconButton(
icon: const Icon(
Icons.bookmark,
color: Colors.white,
semanticLabel: 'Bookmark',
),
onPressed: () {
_pdfViewerKey.currentState?.openBookmarkView();
},
),
],
),
body: SfPdfViewer.network(
'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf',
key: _pdfViewerKey,
),
),
);
}NOTE
RTL scrolling is not supported in single-page layout mode when the scrollDirection property is set to
PdfScrollDirection.horizontal.