PDF documents can be printed in Xamarin Forms’ Android, iOS, and UWP platforms by obtaining the currently loaded PDF as stream and printing the stream using native printing functionalities available in the respective platforms using DependencyService of Xamarin Forms. These platform specific printing options can be used by defining an interface in the portable project and implementing the same in the platform projects using the specific code.
The following code snippets illustrate printing PDF documents in Xamarin Forms’ Android, iOS, and UWP platforms.
Define the following interface.
Load the PDF to PdfViewer. Call the Print method with the stream obtained from PdfViewer and file name using DependencyService when the print button is clicked in the code behind. Give the name of the interface as the type parameter to the method DependencyService.Get<T>().
Implement the interface in the platform projects with the available platform specific printing options.
Define a class derived from the PrintDocumentAdapter and write the contents of the input file to the print destination in the OnWrite method.
|Article ID:||Published Date:||Last Revised Date:||Platform:||Control:||.NET Framework:|