Skip to content

Commit f251ab8

Browse files
Merge pull request #1190 from telerik/didi/add-two-kbs
add two kbs for share and save pdf files
2 parents 118fe90 + e51756c commit f251ab8

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: Save PDF Files in Your Mobile and Desktop Apps.
3+
description: Learn how to save pdf files using the PDF Viewer Toolbar in your .NET MAUI application.
4+
type: how-to
5+
page_title: How to Save PDF Files in Your .NET MAUI Application
6+
slug: save-pdf-files-mobile-desktop
7+
tags: maui, pdf, save files, save 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+
18+
## Description
19+
20+
Learn how to save PDF files in your .NET MAUI application.
21+
22+
This knowledge base article also answers the following questions:
23+
- How can I save PDF files?
24+
- How can I use the PDF Viewer Toolbar for Saving PDF documents?
25+
26+
## Solution
27+
28+
For saving files, use the [.NET MAUI File System Helper API](https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers?view=net-maui-9.0&tabs=android). The `Save` command will be executed from a custom toolbar `ButtonToolbarItem` inside the [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%}).
29+
30+
**1.** Define the [PDF VIewer]({%slug pdfviewer-overview%}) and [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%}):
31+
32+
```XAML
33+
<Grid>
34+
<Grid.RowDefinitions>
35+
<RowDefinition Height="Auto"/>
36+
<RowDefinition />
37+
</Grid.RowDefinitions>
38+
<telerik:RadPdfViewerToolbar PdfViewer="{Binding Source={x:Reference pdfViewer}}">
39+
<telerik:ButtonToolbarItem Text="Share" Command="{Binding SaveDocumentCommand}" />
40+
</telerik:RadPdfViewerToolbar>
41+
<telerik:RadPdfViewer x:Name="pdfViewer"
42+
Grid.Row="1"
43+
Document="{Binding Document, Mode=OneWayToSource}" />
44+
</Grid>
45+
```
46+
47+
**2.** In this example, the document is loaded as `Embedded Resource`.
48+
49+
```C#
50+
Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
51+
{
52+
Assembly assembly = typeof(MainPage).Assembly;
53+
string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-overview.pdf"));
54+
Stream stream = assembly.GetManifestResourceStream(fileName);
55+
return stream;
56+
});
57+
this.pdfViewer.Source = streamFunc;
58+
```
59+
60+
**3.** Create a `ViewModel` and implement the `ShareDocumentCommand` which defines the Share API:
61+
62+
```C#
63+
public class ViewModel
64+
{
65+
public ViewModel()
66+
{
67+
this.SaveDocumentCommand = new Command(this.SaveDocument);
68+
}
69+
70+
// save pdf document
71+
private void SaveDocument(object obj)
72+
{
73+
74+
var fileName = "document.pdf";
75+
var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
76+
var filePath = Path.Combine(localFolder, fileName);
77+
78+
using (Stream output = File.OpenWrite(filePath))
79+
{
80+
new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider().Export(this.Document, output);
81+
82+
Application.Current.MainPage.DisplayAlert("Document is saved to local application data: ", filePath, "OK");
83+
}
84+
}
85+
86+
public RadFixedDocument Document { get; set; }
87+
88+
public ICommand SaveDocumentCommand { get; set; }
89+
}
90+
```
91+
92+
> Make sure the app is granted permissions to access local data.
93+
94+
## See Also
95+
96+
- [PDF Viewer Toolbar]({%slug pdfviewer-toolbar%})
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)