Skip to content

Working with Text Search in .NET MAUI PDF Viewer #55

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions TextSearch/TextSearch.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
  1. The sample is in TextSearch folder which is fine.
  2. We may need to add a folder inside (with folder name based on use case) and add the sample. because in future we may add text search samples inside this folder.

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TextSearch", "TextSearch\TextSearch.csproj", "{F4638210-0C15-45E5-A02B-1576688E5569}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F4638210-0C15-45E5-A02B-1576688E5569}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4638210-0C15-45E5-A02B-1576688E5569}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4638210-0C15-45E5-A02B-1576688E5569}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{F4638210-0C15-45E5-A02B-1576688E5569}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4638210-0C15-45E5-A02B-1576688E5569}.Release|Any CPU.Build.0 = Release|Any CPU
{F4638210-0C15-45E5-A02B-1576688E5569}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions TextSearch/TextSearch/App.xaml
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:TextSearch"
x:Class="TextSearch.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions TextSearch/TextSearch/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TextSearch
{
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
}
15 changes: 15 additions & 0 deletions TextSearch/TextSearch/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="TextSearch.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TextSearch"
Shell.FlyoutBehavior="Disabled"
Title="TextSearch">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions TextSearch/TextSearch/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TextSearch
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
Binary file added TextSearch/TextSearch/Assets/PDF_Succinctly.pdf
Binary file not shown.
41 changes: 41 additions & 0 deletions TextSearch/TextSearch/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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="TextSearch.MainPage">

<ContentPage.Content>
<Grid>
<!-- Define two rows: One for toolbar and one for PDF viewer -->
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<!-- Toolbar row -->
<RowDefinition Height="*"/>
<!-- PDF viewer row -->
</Grid.RowDefinitions>

<!-- Toolbar: Contains title and buttons for search actions -->
<ScrollView Orientation="Horizontal" HorizontalScrollBarVisibility="Never">
<Grid ColumnDefinitions="Auto, *">
<!-- Title Label on the Left -->
<Label Text="PDF Viewer" FontSize="16" VerticalOptions="Center" Grid.Column="0"/>

<!-- Button Container with Spacing -->
<HorizontalStackLayout Grid.Column="1" Spacing="10" HorizontalOptions="End" Padding="0,0,15,0">
<Button Text="&#xE715;" FontFamily="MauiMaterialAssets" Clicked="SearchButtonClicked"/>
<!-- Starts search -->
<Button Text="&#xE707;" FontFamily="MauiMaterialAssets" Clicked="PreviousMatchButtonClicked"/>
<!-- Moves to previous match -->
<Button Text="&#xE706;" FontFamily="MauiMaterialAssets" Clicked="NextMatchButtonClicked"/>
<!-- Moves to next match -->
<Button Text="&#xE70B;" FontFamily="MauiMaterialAssets" Clicked="CloseSearchButtonClicked"/>
<!-- Clears search results -->
</HorizontalStackLayout>
</Grid>
</ScrollView>

<!-- PDF Viewer Component -->
<syncfusion:SfPdfViewer x:Name="PdfViewer" ShowToolbars="False" Grid.Row="1"/>
</Grid>
</ContentPage.Content>
</ContentPage>
54 changes: 54 additions & 0 deletions TextSearch/TextSearch/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Syncfusion.Maui.PdfViewer;
using System.Reflection;

namespace TextSearch
{
public partial class MainPage : ContentPage
{
// Stores the search results to enable navigation and clearing.
TextSearchResult? SearchResult;

public MainPage()
{
InitializeComponent();

// Load the PDF document from embedded resources.
Stream? stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("TextSearch.Assets.PDF_Succinctly.pdf");
PdfViewer.LoadDocument(stream!);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Here force not null operator is used. Please check with null as below

if (stream != null)


// Customize the highlight colors for search results.
PdfViewer.TextSearchSettings.CurrentMatchHighlightColor = Color.FromRgba("#8F00FF43"); // Highlight color for the current match.
PdfViewer.TextSearchSettings.OtherMatchesHighlightColor = Color.FromRgba("#00FF0043"); // Highlight color for other matches.
}

// Initiates a text search when the search button is clicked.
private void SearchButtonClicked(object sender, EventArgs e)
{
SearchText("the");
}

// Searches for the specified text asynchronously and stores the result.
async void SearchText(string text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
SearchResult = await PdfViewer.SearchTextAsync(text);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We may need to show the search progress to the users. Did we handle it? https://help.syncfusion.com/maui/pdf-viewer/text-search#text-search-progress
  2. We may need to show the search completion to the users. Did we use any "Search completed', "no matches found" kind of things?

}

// Moves to the next match when the next button is clicked.
private void NextMatchButtonClicked(object sender, EventArgs e)
{
SearchResult?.GoToNextMatch();
}

// Moves to the previous match when the previous button is clicked.
private void PreviousMatchButtonClicked(object sender, EventArgs e)
{
SearchResult?.GoToPreviousMatch();
}

// Clears the search highlights and resets the search state.
private void CloseSearchButtonClicked(object sender, EventArgs e)
{
SearchResult?.Clear();
}
}
}
30 changes: 30 additions & 0 deletions TextSearch/TextSearch/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core.Hosting;

namespace TextSearch
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

builder.ConfigureSyncfusionCore();

Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("MDAxQDMyMzgyZTMwMmUzMGFMV081MVlpVnJ5aHJyQzUzMFFBOUJwZ1B0YWkxNC80WnA3SHdvbmdqNzA9");

#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
}
6 changes: 6 additions & 0 deletions TextSearch/TextSearch/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
11 changes: 11 additions & 0 deletions TextSearch/TextSearch/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace TextSearch
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
}
16 changes: 16 additions & 0 deletions TextSearch/TextSearch/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Android.App;
using Android.Runtime;

namespace TextSearch
{
[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#512BD4</color>
<color name="colorPrimaryDark">#2B0B98</color>
<color name="colorAccent">#2B0B98</color>
</resources>
10 changes: 10 additions & 0 deletions TextSearch/TextSearch/Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Foundation;

namespace TextSearch
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
14 changes: 14 additions & 0 deletions TextSearch/TextSearch/Platforms/MacCatalyst/Entitlements.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- See https://aka.ms/maui-publish-app-store#add-entitlements for more information about adding entitlements.-->
<dict>
<!-- App Sandbox must be enabled to distribute a MacCatalyst app through the Mac App Store. -->
<key>com.apple.security.app-sandbox</key>
<true/>
<!-- When App Sandbox is enabled, this value is required to open outgoing network connections. -->
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>

38 changes: 38 additions & 0 deletions TextSearch/TextSearch/Platforms/MacCatalyst/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- The Mac App Store requires you specify if the app uses encryption. -->
<!-- Please consult https://developer.apple.com/documentation/bundleresources/information_property_list/itsappusesnonexemptencryption -->
<!-- <key>ITSAppUsesNonExemptEncryption</key> -->
<!-- Please indicate <true/> or <false/> here. -->

<!-- Specify the category for your app here. -->
<!-- Please consult https://developer.apple.com/documentation/bundleresources/information_property_list/lsapplicationcategorytype -->
<!-- <key>LSApplicationCategoryType</key> -->
<!-- <string>public.app-category.YOUR-CATEGORY-HERE</string> -->
<key>UIDeviceFamily</key>
<array>
<integer>2</integer>
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>arm64</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/appicon.appiconset</string>
</dict>
</plist>
16 changes: 16 additions & 0 deletions TextSearch/TextSearch/Platforms/MacCatalyst/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ObjCRuntime;
using UIKit;

namespace TextSearch
{
public class Program
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
}
}
17 changes: 17 additions & 0 deletions TextSearch/TextSearch/Platforms/Tizen/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

namespace TextSearch
{
internal class Program : MauiApplication
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
}
}
15 changes: 15 additions & 0 deletions TextSearch/TextSearch/Platforms/Tizen/tizen-manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="8" xmlns="http://tizen.org/ns/packages">
<profile name="common" />
<ui-application appid="maui-application-id-placeholder" exec="TextSearch.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<label>maui-application-title-placeholder</label>
<icon>maui-appicon-placeholder</icon>
<metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
</ui-application>
<shortcut-list />
<privileges>
<privilege>http://tizen.org/privilege/internet</privilege>
</privileges>
<dependencies />
<provides-appdefined-privileges />
</manifest>
8 changes: 8 additions & 0 deletions TextSearch/TextSearch/Platforms/Windows/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<maui:MauiWinUIApplication
x:Class="TextSearch.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:TextSearch.WinUI">

</maui:MauiWinUIApplication>
Loading