|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Deploy SfSmartPdfViewer in Blazor MAUI in windows | Syncfusion |
| 4 | +description: Learn how to deploy SfSmartPdfViewer in Blazor MAUI Application on Windows in Syncfusion Blazor SfSmartPdfViewer component and much more details. |
| 5 | +platform: document-processing |
| 6 | +control: SfSmartPdfViewer |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Using Smart PDF Viewer Component in the Blazor MAUI app |
| 11 | + |
| 12 | +In this section, we'll guide you through the process of adding Syncfusion® Blazor Smart PDF Viewer component to your Blazor Maui app. We'll break it down into simple steps to make it easy to follow. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +To use the MAUI project templates, install the Mobile development with the .NET extension for Visual Studio. For more details, refer to [here](https://learn.microsoft.com/en-us/dotnet/MAUI/get-started/installation?tabs=vswin). |
| 17 | + |
| 18 | +## Create a new Blazor MAUI App in Visual Studio |
| 19 | + |
| 20 | +Create a new Blazor MAUI app and by selecting the template **.NET MAUI Blazor Hybrid APP ** in VS. |
| 21 | + |
| 22 | +## Install Smart PDF Viewer NuGet package in Blazor Maui App |
| 23 | + |
| 24 | +Add the following NuGet packages into the Blazor Maui app. |
| 25 | + |
| 26 | +* [Syncfusion.Blazor.SfSmartPdfViewer](https://www.nuget.org/packages/Syncfusion.Blazor.SfSmartPdfViewer) |
| 27 | +* [Syncfusion.Blazor.Themes](https://www.nuget.org/packages/Syncfusion.Blazor.Themes) |
| 28 | + |
| 29 | +## Register Syncfusion<sup style="font-size:70%">®</sup> Blazor Service |
| 30 | + |
| 31 | +* In the **~/_Imports.razor** file, add the following namespaces: |
| 32 | + |
| 33 | +{% tabs %} |
| 34 | +{% highlight razor tabtitle="~/_Imports.razor" %} |
| 35 | + |
| 36 | +@using Syncfusion.Blazor |
| 37 | +@using Syncfusion.Blazor.SmartPdfViewer |
| 38 | + |
| 39 | +{% endhighlight %} |
| 40 | +{% endtabs %} |
| 41 | + |
| 42 | +* Register the Syncfusion<sup style="font-size:70%">®</sup> Blazor Service in the **~/MauiProgram.cs** file. |
| 43 | + |
| 44 | +{% tabs %} |
| 45 | +{% highlight c# tabtitle="~/MauiProgram.cs" hl_lines="3 20 28" %} |
| 46 | + |
| 47 | +using Microsoft.Extensions.Logging; |
| 48 | +using MauiBlazorWindow.Data; |
| 49 | +using Syncfusion.Blazor; |
| 50 | + |
| 51 | +namespace MauiBlazorWindow; |
| 52 | + |
| 53 | +public static class MauiProgram |
| 54 | +{ |
| 55 | + public static MauiApp CreateMauiApp() |
| 56 | + { |
| 57 | + var builder = MauiApp.CreateBuilder(); |
| 58 | + builder |
| 59 | + .UseMauiApp<App>() |
| 60 | + .ConfigureFonts(fonts => |
| 61 | + { |
| 62 | + fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); |
| 63 | + }); |
| 64 | + |
| 65 | + builder.Services.AddMauiBlazorWebView(); |
| 66 | + builder.Services.AddMemoryCache(); |
| 67 | + |
| 68 | +#if DEBUG |
| 69 | + builder.Services.AddBlazorWebViewDeveloperTools(); |
| 70 | + builder.Logging.AddDebug(); |
| 71 | +#endif |
| 72 | + |
| 73 | + builder.Services.AddSingleton<WeatherForecastService>(); |
| 74 | + builder.Services.AddSyncfusionBlazor(); |
| 75 | + |
| 76 | + return builder.Build(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +{% endhighlight %} |
| 81 | +{% endtabs %} |
| 82 | + |
| 83 | +## To Configure Azure OpenAI Service |
| 84 | + |
| 85 | +In **Visual Studio**, Go to Tools → NuGet Package Manager → Package Manager Console. Run these commands one by one: |
| 86 | + |
| 87 | +{% tabs %} |
| 88 | +{% highlight razor tabtitle="Package Manager" %} |
| 89 | + |
| 90 | +Install-Package Azure.AI.OpenAI |
| 91 | +Install-Package Microsoft.Extensions.AI |
| 92 | +Install-Package Microsoft.Extensions.AI.OpenAI -Version 9.8.0-preview.1.25412.6 |
| 93 | + |
| 94 | +{% endhighlight %} |
| 95 | +{% endtabs %} |
| 96 | + |
| 97 | +In **Visual Studio Code**, Open terminal in VS Code. Run these commands: |
| 98 | + |
| 99 | +{% tabs %} |
| 100 | +{% highlight razor tabtitle="Package Manager" %} |
| 101 | + |
| 102 | +dotnet add package Azure.AI.OpenAI |
| 103 | +dotnet add package Microsoft.Extensions.AI |
| 104 | +dotnet add package Microsoft.Extensions.AI.OpenAI --version 9.8.0-preview.1.25412.6 |
| 105 | + |
| 106 | +{% endhighlight %} |
| 107 | +{% endtabs %} |
| 108 | + |
| 109 | +To configure the AI service, add the following settings to the **~/MauiProgram.cs** file in your Blazor Server app. |
| 110 | + |
| 111 | +{% tabs %} |
| 112 | +{% highlight razor tabtitle="Blazor Server App" hl_lines="10 11 12 13 14 15 17" %} |
| 113 | + |
| 114 | +using Azure.AI.OpenAI; |
| 115 | +using Microsoft.Extensions.AI; |
| 116 | +using Sample.Components; |
| 117 | +using Syncfusion.Blazor; |
| 118 | +using Syncfusion.Blazor.AI; |
| 119 | +using System.ClientModel; |
| 120 | + |
| 121 | +var builder = WebApplication.CreateBuilder(args); |
| 122 | + |
| 123 | +string azureOpenAiKey = "api-key"; |
| 124 | +string azureOpenAiEndpoint = "endpoint URL"; |
| 125 | +string azureOpenAiModel = "deployment-name"; |
| 126 | +AzureOpenAIClient azureOpenAIClient = new AzureOpenAIClient(new Uri(azureOpenAiEndpoint), new ApiKeyCredential(azureOpenAiKey)); |
| 127 | +IChatClient azureOpenAiChatClient = azureOpenAIClient.GetChatClient(azureOpenAiModel).AsIChatClient(); |
| 128 | +builder.Services.AddChatClient(azureOpenAiChatClient); |
| 129 | + |
| 130 | +builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>(); |
| 131 | + |
| 132 | +var app = builder.Build(); |
| 133 | +.... |
| 134 | + |
| 135 | +{% endhighlight %} |
| 136 | +{% endtabs %} |
| 137 | + |
| 138 | +Here, |
| 139 | +* **apiKey**: “Azure OpenAI API Key”; |
| 140 | +* **deploymentName**: “Azure OpenAI deployment name”; |
| 141 | +* **endpoint**: “Azure OpenAI deployment end point URL”; |
| 142 | + |
| 143 | +For **Azure OpenAI**, first [deploy an Azure OpenAI Service resource and model](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/create-resource?pivots=web-portal), then values for `apiKey`, `deploymentName` and `endpoint` will all be provided to you. |
| 144 | + |
| 145 | +## Adding stylesheet and script |
| 146 | + |
| 147 | +Add the following stylesheet and script to the head section of the **~/wwwroot/index.html** file. |
| 148 | + |
| 149 | +{% tabs %} |
| 150 | +{% highlight html %} |
| 151 | + |
| 152 | +<head> |
| 153 | + <!-- Syncfusion Blazor Smart PDF Viewer control's theme style sheet --> |
| 154 | + <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" /> |
| 155 | + <!-- Syncfusion Blazor Smart PDF Viewer control's scripts --> |
| 156 | + <script src="_content/Syncfusion.Blazor.SfSmartPdfViewer/scripts/syncfusion-blazor-sfsmartpdfviewer.min.js" type="text/javascript"></script> |
| 157 | +</head> |
| 158 | + |
| 159 | +{% endhighlight %} |
| 160 | +{% endtabs %} |
| 161 | + |
| 162 | +## Add Smart PDF Viewer component |
| 163 | + |
| 164 | +Add the Syncfusion<sup style="font-size:70%">®</sup> PDF Viewer (Next Gen) component in the **~/Pages/Index.razor** file. |
| 165 | + |
| 166 | +{% tabs %} |
| 167 | +{% highlight razor %} |
| 168 | + |
| 169 | +@page "/" |
| 170 | + |
| 171 | +<SfSmartPdfViewer Height="100%" Width="100%" DocumentPath="https://cdn.syncfusion.com/content/pdf/http-succinctly.pdf"> |
| 172 | +</SfSmartPdfViewer> |
| 173 | + |
| 174 | +{% endhighlight %} |
| 175 | +{% endtabs %} |
| 176 | + |
| 177 | +## Run on Windows |
| 178 | + |
| 179 | +Run the sample in Windows Machine mode, and it will run Blazor MAUI in Windows. |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +Upon successfully launching the application, the PDF Viewer component will seamlessly render the specified PDF document within its interface. |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | +## Run on Android |
| 188 | + |
| 189 | +To run the PDF Viewer in a Blazor Android MAUI application using the Android emulator, follow these steps: |
| 190 | + |
| 191 | +1. Add the following Assets files in your Project by creating Assets folder(Platform -> Android -> Assets) |
| 192 | + [model](https://github.com/SyncfusionExamples/blazor-smart-pdf-viewer-examples/blob/master/Common/LocalEmbeddingsModel/model.onnx) & [vocab](https://github.com/SyncfusionExamples/blazor-smart-pdf-viewer-examples/blob/master/Common/LocalEmbeddingsModel/vocab.txt) |
| 193 | + |
| 194 | +2. Right click the Added files and go to (Properties -> BuildAction) set as AndroidAsset. |
| 195 | +3. Add the following code in your .csproj file |
| 196 | +``` |
| 197 | +<ItemGroup> |
| 198 | + <AndroidAsset Include="Platforms\Android\Assets\model.onnx" /> |
| 199 | + <AndroidAsset Include="Platforms\Android\Assets\vocab.txt" /> |
| 200 | +</ItemGroup> |
| 201 | +``` |
| 202 | +4. Add the following code in your MauiProgram.cs file |
| 203 | + |
| 204 | +{% tabs %} |
| 205 | +{% highlight c# tabtitle="~/MauiProgram.cs" hl_lines="7 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25" %} |
| 206 | + |
| 207 | +public static class MauiProgram |
| 208 | +{ |
| 209 | + public static MauiApp CreateMauiApp() |
| 210 | + { |
| 211 | + ... |
| 212 | + builder.Services.AddSyncfusionBlazor(); |
| 213 | +#if ANDROID |
| 214 | + EnsureModelExistsAsync(); |
| 215 | +#endif |
| 216 | + return builder.Build(); |
| 217 | + } |
| 218 | + private static async void EnsureModelExistsAsync() |
| 219 | + { |
| 220 | + string[] requiredFiles = { "model.onnx", "vocab.txt" }; |
| 221 | + string targetDir = Path.Combine(FileSystem.AppDataDirectory, "LocalEmbeddingsModel/default"); |
| 222 | + Directory.CreateDirectory(targetDir); |
| 223 | + foreach (string fileName in requiredFiles) |
| 224 | + { |
| 225 | + string targetPath = Path.Combine(targetDir, fileName); |
| 226 | + if (!File.Exists(targetPath)) |
| 227 | + { |
| 228 | + using Stream assetStream = await FileSystem.OpenAppPackageFileAsync(fileName); |
| 229 | + using FileStream fileStream = File.Create(targetPath); |
| 230 | + await assetStream.CopyToAsync(fileStream); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | +} |
| 235 | +{% endhighlight %} |
| 236 | +{% endtabs %} |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | +Refer [here](https://learn.microsoft.com/en-us/dotnet/maui/android/emulator/device-manager#android-device-manager-on-windows) to install and launch Android emulator. |
| 241 | + |
| 242 | +N> If you encounter any errors while using the Android Emulator, refer to the following link for troubleshooting guidance[Troubleshooting Android Emulator](https://learn.microsoft.com/en-us/dotnet/maui/android/emulator/troubleshooting). |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +>[View Sample in GitHub](https://github.com/SyncfusionExamples/blazor-smart-pdf-viewer-examples/tree/master/GettingStartedMAUI). |
| 247 | +
|
| 248 | +## See also |
| 249 | + |
| 250 | +* [Document Summarizer in Blazor Smart PDF Viewer](../document-summarizer) |
| 251 | +* [Smart Redaction in Blazor Smart PDF Viewer](../smart-redaction) |
| 252 | +* [Smart Fill in Blazor Smart PDF Viewer](../smart-fill). |
| 253 | +* [Render PDF Document from embedded source in the MAUI Android app](../how-to/deploy-maui-using-android-emulator). |
0 commit comments