|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: RTL support in Flutter PDF Viewer widget | Syncfusion |
| 4 | +description: Learn here all about the Right to Left (RTL) support in Syncfusion Flutter PDF Viewer (SfPdfViewer) widget and more. |
| 5 | +platform: flutter |
| 6 | +control: SfPdfViewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Right to Left (RTL) in Flutter PDF Viewer (SfPdfViewer) |
| 11 | + |
| 12 | +The [SfPdfViewer](https://pub.dev/documentation/syncfusion_flutter_pdfviewer/latest/pdfviewer/SfPdfViewer-class.html) supports right-to-left rendering. All the user interface elements will be rendered based on LTR and RTL direction, and the functionalities like text search and copying text also support RTL languages. |
| 13 | + |
| 14 | +## RTL rendering ways |
| 15 | + |
| 16 | +Right to left rendering can be switched in the following ways: |
| 17 | + |
| 18 | +### Wrapping the SfPdfViewer with Directionality widget |
| 19 | + |
| 20 | +To change the rendering direction from right to left, wrap the [SfPdfViewer](https://pub.dev/documentation/syncfusion_flutter_pdfviewer/latest/pdfviewer/SfPdfViewer-class.html) widget inside the [Directionality](https://api.flutter.dev/flutter/widgets/Directionality-class.html) widget and set the `textDirection` property as [TextDirection.rtl](https://api.flutter.dev/flutter/widgets/Directionality/textDirection.html). |
| 21 | + |
| 22 | +{% tabs %} |
| 23 | +{% highlight dart hl_lines="24 25" %} |
| 24 | + |
| 25 | + final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey(); |
| 26 | + |
| 27 | + @override |
| 28 | + void initState() { |
| 29 | + super.initState(); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + return Scaffold( |
| 35 | + appBar: AppBar( |
| 36 | + title: const Text('Syncfusion Flutter PDF Viewer'), |
| 37 | + actions: <Widget>[ |
| 38 | + IconButton( |
| 39 | + icon: const Icon( |
| 40 | + Icons.bookmark, |
| 41 | + color: Colors.white, |
| 42 | + semanticLabel: 'Bookmark', |
| 43 | + ), |
| 44 | + onPressed: () { |
| 45 | + _pdfViewerKey.currentState?.openBookmarkView(); |
| 46 | + }, |
| 47 | + ), |
| 48 | + ], |
| 49 | + ), |
| 50 | + body: Directionality( |
| 51 | + textDirection: TextDirection.rtl, |
| 52 | + child: SfPdfViewer.network( |
| 53 | + 'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf', |
| 54 | + key: _pdfViewerKey, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | +{% endhighlight %} |
| 61 | +{% endtabs %} |
| 62 | + |
| 63 | +### Changing the locale to RTL languages |
| 64 | + |
| 65 | +To change the [SfPdfViewer](https://pub.dev/documentation/syncfusion_flutter_pdfviewer/latest/pdfviewer/SfPdfViewer-class.html) rendering direction from right to left, change the [locale](https://api.flutter.dev/flutter/material/MaterialApp/locale.html) to any of the RTL languages such as Arabic, Persian, Hebrew, Pashto, and Urdu. |
| 66 | + |
| 67 | +To use `flutter_localizations`, add the package as dependency to `pubspec.yaml` file. |
| 68 | + |
| 69 | +{% highlight dart %} |
| 70 | + |
| 71 | +dependencies: |
| 72 | +flutter_localizations: |
| 73 | + sdk: flutter |
| 74 | + |
| 75 | +{% endhighlight %} |
| 76 | + |
| 77 | +Then, import the `flutter_localizations` library, specify [localizationsDelegates](https://api.flutter.dev/flutter/widgets/LocalizationsDelegate-class.html) and `supportedLocales` for `MaterialApp`. |
| 78 | + |
| 79 | +{% tabs %} |
| 80 | +{% highlight dart %} |
| 81 | + |
| 82 | + final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey(); |
| 83 | + |
| 84 | + @override |
| 85 | + Widget build(BuildContext context) { |
| 86 | + return MaterialApp( |
| 87 | + localizationsDelegates: const [ |
| 88 | + GlobalMaterialLocalizations.delegate, |
| 89 | + GlobalWidgetsLocalizations.delegate, |
| 90 | + GlobalCupertinoLocalizations.delegate, |
| 91 | + ], |
| 92 | + supportedLocales: const <Locale>[ |
| 93 | + Locale('en'), |
| 94 | + Locale('ar'), |
| 95 | + // ... other locales the app supports |
| 96 | + ], |
| 97 | + locale: const Locale('ar'), |
| 98 | + home: Scaffold( |
| 99 | + appBar: AppBar( |
| 100 | + title: const Text('Syncfusion Flutter PDF Viewer'), |
| 101 | + actions: <Widget>[ |
| 102 | + IconButton( |
| 103 | + icon: const Icon( |
| 104 | + Icons.bookmark, |
| 105 | + color: Colors.white, |
| 106 | + semanticLabel: 'Bookmark', |
| 107 | + ), |
| 108 | + onPressed: () { |
| 109 | + _pdfViewerKey.currentState?.openBookmarkView(); |
| 110 | + }, |
| 111 | + ), |
| 112 | + ], |
| 113 | + ), |
| 114 | + body: SfPdfViewer.network( |
| 115 | + 'https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf', |
| 116 | + key: _pdfViewerKey, |
| 117 | + ), |
| 118 | + ), |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | +{% endhighlight %} |
| 123 | +{% endtabs %} |
0 commit comments