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

Merged
merged 4 commits into from
Jun 18, 2025
Merged
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/TextSearchAndNavigation/TextSearchAndNavigation.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

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}") = "TextSearchAndNavigation", "TextSearchAndNavigation\TextSearchAndNavigation.csproj", "{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Release|Any CPU.Build.0 = Release|Any CPU
{38CB40BA-62DB-49B3-B7DB-8F2AE08116EC}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
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:TextSearchAndNavigation"
x:Class="TextSearchAndNavigation.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,12 @@
namespace TextSearchAndNavigation
{
public partial class App : Application
{
public App()
{
InitializeComponent();

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

<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 TextSearchAndNavigation
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
Binary file not shown.
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="TextSearchAndNavigation.MainPage">

<ContentPage.Content>
<Grid>
<!-- Define two rows: One for toolbar and one for PDF viewer -->
<Grid.RowDefinitions>
<RowDefinition Height="45"/>
<!-- 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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Syncfusion.Maui.PdfViewer;
using System.Reflection;

namespace TextSearchAndNavigation
{
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("TextSearchAndNavigation.Assets.PDF_Succinctly.pdf");

if (stream != null)
PdfViewer.LoadDocument(stream);

// 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)
{
PdfViewer.TextSearchProgress += PdfTextSearchProgress;
SearchResult = await PdfViewer.SearchTextAsync(text);
}

private void PdfTextSearchProgress(object? sender, TextSearchProgressEventArgs? e)
{
if (e != null)
// Cancel the search operation when it is running but does not need to be continued.
e.SearchResult?.Clear();
}

// 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Microsoft.Extensions.Logging;
using Syncfusion.Maui.Core.Hosting;

namespace TextSearchAndNavigation
{
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");
});

#if DEBUG
builder.Logging.AddDebug();
#endif
builder.ConfigureSyncfusionCore();
return builder.Build();
}
}
}
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>
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 TextSearchAndNavigation
{
[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
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Android.App;
using Android.Runtime;

namespace TextSearchAndNavigation
{
[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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Foundation;

namespace TextSearchAndNavigation
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
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>

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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ObjCRuntime;
using UIKit;

namespace TextSearchAndNavigation
{
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));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

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

static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
}
}
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="TextSearchAndNavigation.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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<maui:MauiWinUIApplication
x:Class="TextSearchAndNavigation.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:TextSearchAndNavigation.WinUI">

</maui:MauiWinUIApplication>
Loading