|
| 1 | +--- |
| 2 | +title: Share PDF Files in Your Mobile and Desktop Apps. |
| 3 | +description: Learn how to share pdf files using the PDF Viewer Toolbar in your .NET MAUI application. |
| 4 | +type: how-to |
| 5 | +page_title: How to Share PDF Files in Your .NET MAUI Application |
| 6 | +slug: share-pdf-files-mobile-desktop |
| 7 | +tags: maui, pdf, share files, share documents, telerik, dotnet, pdf viewer |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +| Version | Product | Author | |
| 14 | +| --- | --- | ---- | |
| 15 | +| 11.0.0 | Telerik UI for .NET MAUI PDF Viewer | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +Learn how to share PDF files in your .NET MAUI application. |
| 20 | + |
| 21 | +This knowledge base article also answers the following questions: |
| 22 | +- How can I transfer PDF files in .NET MAUI application? |
| 23 | +- How can I use the PDF Viewer Toolbar for Sharing PDF documents? |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +For sharing files, use the [.NET MAUI Share File API](https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?view=net-maui-9.0&tabs=android). The `Share` command will be executed from a custom toolbar `ButtonToolbarItem` inside the [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%}). |
| 28 | + |
| 29 | +**1.** Define the [PDF VIewer]({%slug pdfviewer-overview%}) and [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%}): |
| 30 | + |
| 31 | +```XAML |
| 32 | +<Grid> |
| 33 | + <Grid.RowDefinitions> |
| 34 | + <RowDefinition Height="Auto"/> |
| 35 | + <RowDefinition /> |
| 36 | + </Grid.RowDefinitions> |
| 37 | + <telerik:RadPdfViewerToolbar PdfViewer="{Binding Source={x:Reference pdfViewer}}"> |
| 38 | + |
| 39 | + <telerik:ButtonToolbarItem Text="Share" |
| 40 | + Command="{Binding ShareDocumentCommand}" /> |
| 41 | + </telerik:RadPdfViewerToolbar> |
| 42 | + <telerik:RadPdfViewer x:Name="pdfViewer" |
| 43 | + Grid.Row="1" |
| 44 | + Document="{Binding Document, Mode=OneWayToSource}" /> |
| 45 | +</Grid> |
| 46 | +``` |
| 47 | + |
| 48 | +**2.** In this example, load the document as `Embedded Resource`. |
| 49 | + |
| 50 | +```C# |
| 51 | +Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() => |
| 52 | +{ |
| 53 | + Assembly assembly = typeof(MainPage).Assembly; |
| 54 | + string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-overview.pdf")); |
| 55 | + Stream stream = assembly.GetManifestResourceStream(fileName); |
| 56 | + return stream; |
| 57 | +}); |
| 58 | +this.pdfViewer.Source = streamFunc; |
| 59 | +``` |
| 60 | + |
| 61 | +**3.** Create a `ViewModel` and implement the `ShareDocumentCommand` which defines the Share API: |
| 62 | + |
| 63 | +```C# |
| 64 | +public class ViewModel |
| 65 | +{ |
| 66 | + public ViewModel() |
| 67 | + { |
| 68 | + this.ShareDocumentCommand = new Command(this.ShareDocument); |
| 69 | + } |
| 70 | + |
| 71 | + private async void ShareDocument(object obj) |
| 72 | + { |
| 73 | + await ShareAsync(); |
| 74 | + } |
| 75 | + |
| 76 | + // share pdf document |
| 77 | + private async Task ShareAsync() |
| 78 | + { |
| 79 | + Assembly assembly = typeof(MainPage).Assembly; |
| 80 | + string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-overview.pdf")); |
| 81 | + Stream stream = assembly.GetManifestResourceStream(fileName); |
| 82 | + var cacheFile = Path.Combine(FileSystem.CacheDirectory, "pdf-overview.pdf"); |
| 83 | + using (var file = new FileStream(cacheFile, FileMode.Create, FileAccess.Write)) |
| 84 | + { |
| 85 | + stream.CopyTo(file); |
| 86 | + } |
| 87 | + var request = new ShareFileRequest |
| 88 | + { |
| 89 | + Title = "Share pdf document", |
| 90 | + File = new ShareFile(cacheFile) |
| 91 | + }; |
| 92 | + await Share.Default.RequestAsync(request); |
| 93 | + } |
| 94 | + |
| 95 | + public RadFixedDocument Document { get; set; } |
| 96 | + |
| 97 | + public ICommand ShareDocumentCommand { get; set; } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## See Also |
| 102 | + |
| 103 | +- [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%}) |
| 104 | +- [Share Files in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?view=net-maui-9.0&tabs=android) |
0 commit comments