Text search

In this document
chevron_rightIn Xamarin.Android
chevron_rightIn Xamarin.iOS

linkIn Xamarin.Android

Text search via UI controls in Xamarin.Android is identical to text search via UI controls in Android. See tutorial here.

For example, the equivalent of the example provided in the Android guide will be:

  1. Add the controls to your layout, a simple example will be:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">
    <pdftron.PDF.Controls.SearchToolbar
        android:id="@+id/searchtoolbar"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetStart="@dimen/second_keyline"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />
    <pdftron.PDF.PDFViewCtrl
        android:id="@+id/pdfviewctrl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="vertical|horizontal" />
  </LinearLayout>
  <pdftron.PDF.Controls.FindTextOverlay
        android:id="@+id/find_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
</RelativeLayout>
  1. Setup PDFViewCtrl and ToolManager

  2. Setup events handling

var mSearchToolbar = FindViewById<pdftron.PDF.Controls.SearchToolbar>(Resource.Id.searchtoolbar);
var mSearchOverlay = FindViewById<pdftron.PDF.Controls.FindTextOverlay>(Resource.Id.find_text_view);
mSearchOverlay.SetPdfViewCtrl(mPdfViewCtrl);

mSearchToolbar.ExitSearch += (sender, e) =>
{
    mSearchToolbar.Visibility = ViewStates.Gone;
    mSearchOverlay.Visibility = ViewStates.Gone;
    mSearchOverlay.ExitSearchMode();
};
mSearchToolbar.ClearSearchQuery += (sender, e) =>
{
    mSearchOverlay?.CancelFindText();
};
mSearchToolbar.SearchQuerySubmit += (sender, e) =>
{
    mSearchOverlay?.QueryTextSubmit(e.Query);
};
mSearchToolbar.SearchQueryChange += (sender, e) =>
{
    mSearchOverlay?.SetSearchQuery(e.Query);
};
mSearchToolbar.SearchOptionsItemSelected += (sender, e) =>
{
    int id = e.Item.ItemId;
    if (id == Resource.Id.action_match_case)
    {
        bool isChecked = e.Item.IsChecked;
        mSearchOverlay?.SetSearchMatchCase(!isChecked);
        mSearchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
    else if (id == Resource.Id.action_whole_word)
    {
        bool isChecked = e.Item.IsChecked;
        mSearchOverlay?.SetSearchWholeWord(!isChecked);
        mSearchOverlay?.ResetFullTextResults();
        e.Item.SetChecked(!isChecked);
    }
};
  1. Show the layout
mSearchToolbar.Visibility = ViewStates.Visible;
mSearchOverlay.Visibility = ViewStates.Visible;

linkIn Xamarin.iOS

Using find text toolbar in Xamarin.iOS is identical to using FindTextToolbar in iOS. See tutorial here.

For example, the equivalent of the example provided in the iOS guide will be:

var findTextToolbar = new pdftron.PDF.Controls.FindTextToolbar(mPdfViewCtrl);
this.View.AddSubview(findTextToolbar);
findTextToolbar.Hidden = true;
findTextToolbar.FindTextToolbarDidCancel += (sender, e) =>
{
    findTextToolbar.Hidden = true;
};
findTextToolbar.TranslatesAutoresizingMaskIntoConstraints = false;

NSLayoutConstraint.ActivateConstraints(new NSLayoutConstraint[] {
    findTextToolbar.TopAnchor.ConstraintEqualTo(this.View.LayoutMarginsGuide.TopAnchor),
    findTextToolbar.LeadingAnchor.ConstraintEqualTo(this.View.LeadingAnchor),
    findTextToolbar.WidthAnchor.ConstraintEqualTo(this.View.WidthAnchor)
});

To show:

findTextToolbar.Hidden = false;

To highlight search result:

mPdfViewCtrl.TextSearchResult += (sender, e) =>
{
    var sel = e.Selection;
    UIColor myColor = new UIColor(0.0f, 1.0f, 0.0f, 0.3f);
    mPdfViewCtrl.HighlightSelection(sel, myColor);
};