-
Notifications
You must be signed in to change notification settings - Fork 1
972249 - How to include Open and Save options in the built-in toolbar? #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
59c395f
e7583a2
b23e1bd
5026652
ed65ffd
21e410b
204ac0e
166cae4
bf4d380
0153690
86ab546
564453c
ae4d02a
3bcadf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version = "1.0" encoding = "UTF-8" ?> | ||
| <Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:InsertToolbarItems" | ||
| x:Class="InsertToolbarItems.App"> | ||
| <Application.Resources> | ||
| <ResourceDictionary> | ||
| <ResourceDictionary.MergedDictionaries> | ||
| <ResourceDictionary Source="Resources/Styles/Colors.xaml" /> | ||
| <ResourceDictionary Source="Resources/Styles/Styles.xaml" /> | ||
| </ResourceDictionary.MergedDictionaries> | ||
| </ResourceDictionary> | ||
| </Application.Resources> | ||
| </Application> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| namespace InsertToolbarItems | ||
| { | ||
| public partial class App : Application | ||
| { | ||
| public App() | ||
| { | ||
| Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Add valid license key"); | ||
| InitializeComponent(); | ||
| } | ||
|
|
||
| protected override Window CreateWindow(IActivationState? activationState) | ||
| { | ||
| return new Window(new AppShell()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8" ?> | ||
| <Shell | ||
| x:Class="InsertToolbarItems.AppShell" | ||
| xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:InsertToolbarItems" | ||
| Title="InsertToolbarItems"> | ||
|
|
||
| <ShellContent | ||
| Title="Home" | ||
| ContentTemplate="{DataTemplate local:MainPage}" | ||
| Route="MainPage" /> | ||
|
|
||
| </Shell> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| namespace InsertToolbarItems | ||
| { | ||
| public partial class AppShell : Shell | ||
| { | ||
| public AppShell() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using System.Xml.Linq; | ||
|
|
||
| namespace InsertToolbarItems | ||
| { | ||
| /// <summary> | ||
| /// Provides the functionality to open and save PDF files. | ||
| /// </summary> | ||
| public partial class FileService | ||
| { | ||
| /// <summary> | ||
| /// Shows a file picker and lets the user choose the required PDF file. | ||
| /// </summary> | ||
| /// <returns>The data of the PDF file that is chosen by the user.</returns> | ||
| public async static Task<PdfFileData?> OpenFile(string fileExtension) | ||
| { | ||
| FilePickerFileType pdfFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>{ | ||
| { DevicePlatform.iOS, new[] { $"com.adobe.{fileExtension}" } }, | ||
| { DevicePlatform.Android, new[] { $"application/{fileExtension}" } }, | ||
| { DevicePlatform.WinUI, new[] { fileExtension } }, | ||
| { DevicePlatform.MacCatalyst, new[] { fileExtension } }, | ||
| }); | ||
| PickOptions options = new() | ||
| { | ||
| PickerTitle = "Please select a PDF file", | ||
| }; | ||
| #if !ANDROID | ||
| options.FileTypes = pdfFileType; | ||
| #else | ||
| if (fileExtension == "pdf") | ||
| options.FileTypes = pdfFileType; | ||
| #endif | ||
| return await PickFile(options, fileExtension); | ||
| } | ||
| static async Task<PdfFileData?> PickFile(PickOptions options, string fileExtension) | ||
| { | ||
| try | ||
| { | ||
| var result = await FilePicker.Default.PickAsync(options); | ||
| if (result != null) | ||
| { | ||
| if (result.FileName != null) | ||
| { | ||
| if (result.FileName.EndsWith($".{fileExtension}", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return new PdfFileData(result.FileName, await result.OpenReadAsync()); | ||
| } | ||
| else | ||
| Application.Current?.Windows[0].Page?.DisplayAlert("Error", $"Pick a file of type {fileExtension}", "OK"); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| string message; | ||
| if (ex != null && string.IsNullOrEmpty(ex.Message) == false) | ||
| message = ex.Message; | ||
| else | ||
| message = "File open failed."; | ||
| Application.Current?.Windows[0].Page?.DisplayAlert("Error", message, "OK"); | ||
| } | ||
| return null; | ||
| } | ||
| /// <summary> | ||
| /// Saves the PDF stream with given file name using platform specific file saving APIs. | ||
| /// </summary> | ||
| /// <param name="fileName">The file name with which the PDF needs to be saved.</param> | ||
| /// <param name="fileStream">The stream of the PDF to be saved.</param> | ||
| /// <returns></returns> | ||
| public async static Task<string?> SaveAsAsync(string fileName, Stream fileStream) | ||
| { | ||
| return await PlatformSaveAsAsync(fileName, fileStream); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the given stream to the specified file path. | ||
| /// </summary> | ||
| /// <param name="stream">The stream of the PDF file to be written.</param> | ||
| /// <param name="filePath">The file path to which the PDF file needs to be written.</param> | ||
| /// <returns></returns> | ||
| static async Task WriteStream(Stream stream, string? filePath) | ||
| { | ||
| if (!string.IsNullOrEmpty(filePath)) | ||
| { | ||
| await using var fileStream = new FileStream(filePath, FileMode.OpenOrCreate); | ||
| fileStream.SetLength(0); | ||
| if (stream.CanSeek) | ||
| { | ||
| stream.Seek(0, SeekOrigin.Begin); | ||
| } | ||
|
|
||
| await stream.CopyToAsync(fileStream).ConfigureAwait(false); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this copy is required instead of directly used?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have optimized the code sir
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For optimization I have created a method and used in three platforms sir
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to copy the input stream into the file for saving |
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
Deepak1799 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net9.0-android;net9.0-ios;net9.0-maccatalyst</TargetFrameworks> | ||
| <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net9.0-windows10.0.19041.0</TargetFrameworks> | ||
| <!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET --> | ||
| <!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> --> | ||
|
|
||
| <!-- Note for MacCatalyst: | ||
| The default runtime is maccatalyst-x64, except in Release config, in which case the default is maccatalyst-x64;maccatalyst-arm64. | ||
| When specifying both architectures, use the plural <RuntimeIdentifiers> instead of the singular <RuntimeIdentifier>. | ||
| The Mac App Store will NOT accept apps with ONLY maccatalyst-arm64 indicated; | ||
| either BOTH runtimes must be indicated or ONLY macatalyst-x64. --> | ||
| <!-- For example: <RuntimeIdentifiers>maccatalyst-x64;maccatalyst-arm64</RuntimeIdentifiers> --> | ||
|
|
||
| <OutputType>Exe</OutputType> | ||
| <RootNamespace>InsertToolbarItems</RootNamespace> | ||
| <UseMaui>true</UseMaui> | ||
| <SingleProject>true</SingleProject> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <!-- Display name --> | ||
| <ApplicationTitle>InsertToolbarItems</ApplicationTitle> | ||
|
|
||
| <!-- App Identifier --> | ||
| <ApplicationId>com.companyname.inserttoolbaritems</ApplicationId> | ||
|
|
||
| <!-- Versions --> | ||
| <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> | ||
| <ApplicationVersion>1</ApplicationVersion> | ||
|
|
||
| <!-- To develop, package, and publish an app to the Microsoft Store, see: https://aka.ms/MauiTemplateUnpackaged --> | ||
| <WindowsPackageType>None</WindowsPackageType> | ||
|
|
||
| <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion> | ||
| <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">15.0</SupportedOSPlatformVersion> | ||
| <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion> | ||
| <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion> | ||
| <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion> | ||
| <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <!-- App Icon --> | ||
| <MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" /> | ||
|
|
||
| <!-- Splash Screen --> | ||
| <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" /> | ||
|
|
||
| <!-- Images --> | ||
| <MauiImage Include="Resources\Images\*" /> | ||
| <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" /> | ||
|
|
||
| <!-- Custom Fonts --> | ||
| <MauiFont Include="Resources\Fonts\*" /> | ||
|
|
||
| <!-- Raw Assets (also remove the "Resources\Raw" prefix) --> | ||
| <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Remove="Assets\PDF_Succinctly.pdf" /> | ||
| <None Remove="Images\After Toolbar Item Insertion.png" /> | ||
| <None Remove="Images\Before Toolbar Item Insertion.png" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <EmbeddedResource Include="Assets\PDF_Succinctly.pdf" /> | ||
| <EmbeddedResource Include="Images\After Toolbar Item Insertion.png" /> | ||
| <EmbeddedResource Include="Images\Before Toolbar Item Insertion.png" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" /> | ||
| <PackageReference Include="Syncfusion.Maui.PdfViewer" Version="*" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| | ||
| Microsoft Visual Studio Solution File, Format Version 12.00 | ||
| # Visual Studio Version 17 | ||
| VisualStudioVersion = 17.14.36203.30 d17.14 | ||
| MinimumVisualStudioVersion = 10.0.40219.1 | ||
| Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InsertToolbarItems", "InsertToolbarItems.csproj", "{773AA570-9D7E-4621-9F49-91AD746F5094}" | ||
| EndProject | ||
| Global | ||
| GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
| Debug|Any CPU = Debug|Any CPU | ||
| Release|Any CPU = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
| {773AA570-9D7E-4621-9F49-91AD746F5094}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
| {773AA570-9D7E-4621-9F49-91AD746F5094}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
| {773AA570-9D7E-4621-9F49-91AD746F5094}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
| {773AA570-9D7E-4621-9F49-91AD746F5094}.Release|Any CPU.Build.0 = Release|Any CPU | ||
| EndGlobalSection | ||
| GlobalSection(SolutionProperties) = preSolution | ||
| HideSolutionNode = FALSE | ||
| EndGlobalSection | ||
| GlobalSection(ExtensibilityGlobals) = postSolution | ||
| SolutionGuid = {B8544DAF-EDF6-4AC3-8C43-DA0D9ACB7EDE} | ||
| EndGlobalSection | ||
| EndGlobal |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:syncfusion="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer" | ||
| x:Class="InsertToolbarItems.MainPage"> | ||
|
|
||
|
|
||
|
||
| <Grid> | ||
| <syncfusion:SfPdfViewer x:Name="pdfViewer"/> | ||
| </Grid> | ||
|
|
||
| </ContentPage> | ||
Uh oh!
There was an error while loading. Please reload this page.