Skip to content

Commit

Permalink
Implement bezzad issue #2: Search/Scroll not working
Browse files Browse the repository at this point in the history
Limitations:
- Highlighting the current match not always work
- The order of matches is random within a page
  • Loading branch information
majkimester committed Sep 17, 2023
1 parent 98e1f5a commit 5080cbe
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 155 deletions.
3 changes: 2 additions & 1 deletion src/PdfiumViewer.Demo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
Placement="Bottom"
PlacementRectangle="0,20,0,20"
PlacementTarget = "{Binding ElementName=BtnSearch}"
PopupAnimation="Slide">
PopupAnimation="Slide"
Opened="SearchPopup_Opened">

<Border Background="WhiteSmoke" Height="50"
HorizontalAlignment="Center"
Expand Down
18 changes: 13 additions & 5 deletions src/PdfiumViewer.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ public double ZoomPercent
public bool IsSearchOpen { get => _isSearchOpen; set => SetProperty(ref _isSearchOpen, value); }
private bool _isSearchOpen;

public int SearchMatchItemNo { get; set; }
public int SearchMatchesCount { get; set; }
public int SearchMatchItemNo { get => _searchMatchItemNo; set => SetProperty(ref _searchMatchItemNo, value); }
private int _searchMatchItemNo;

public int SearchMatchesCount { get => _searchMatchesCount; set => SetProperty(ref _searchMatchesCount, value); }
private int _searchMatchesCount;

public int Page
{
get => Renderer.PageNo + 1;
Expand Down Expand Up @@ -139,6 +143,7 @@ private void OpenPdf(object sender, RoutedEventArgs e)

if (dialog.ShowDialog() == true)
{
SearchManager.Reset();
Renderer.OpenPdf(new FileStream(dialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read));

// Open Thumbnails
Expand Down Expand Up @@ -339,7 +344,7 @@ private void Search()
else
{
SearchMatchesCount = SearchManager.MatchesCount;
// DisplayTextSpan(SearchMatches.Items[SearchMatchItemNo++].TextSpan);
SearchMatchItemNo = 1;
}

if (!SearchManager.FindNext(true))
Expand All @@ -357,7 +362,6 @@ private void OnNextFoundClick(object sender, RoutedEventArgs e)
if (SearchMatchesCount > SearchMatchItemNo)
{
SearchMatchItemNo++;
//DisplayTextSpan(SearchMatches.Items[SearchMatchItemNo - 1].TextSpan);
SearchManager.FindNext(true);
}
}
Expand All @@ -367,7 +371,6 @@ private void OnPrevFoundClick(object sender, RoutedEventArgs e)
if (SearchMatchItemNo > 1)
{
SearchMatchItemNo--;
// DisplayTextSpan(SearchMatches.Items[SearchMatchItemNo - 1].TextSpan);
SearchManager.FindNext(false);
}
}
Expand Down Expand Up @@ -480,5 +483,10 @@ private void OnPrint(object sender, RoutedEventArgs e)
}

#endregion

private void SearchPopup_Opened(object sender, EventArgs e)
{
SearchTermTextBox.Focus();
}
}
}
107 changes: 56 additions & 51 deletions src/PdfiumViewer/Core/PdfMarker.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
using System;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using PdfiumViewer.Drawing;
using Color = System.Windows.Media.Color;
using Pen = System.Windows.Media.Pen;

namespace PdfiumViewer.Core
{
public class PdfMarker : IPdfMarker
{
public int Page { get; }
public RectangleF Bounds { get; }
public Color Color { get; }
public Color BorderColor { get; }
public float BorderWidth { get; }

public PdfMarker(int page, RectangleF bounds, Color color)
: this(page, bounds, color, Colors.Transparent, 0)
{
}

public PdfMarker(int page, RectangleF bounds, Color color, Color borderColor, float borderWidth)
{
Page = page;
Bounds = bounds;
Color = color;
BorderColor = borderColor;
BorderWidth = borderWidth;
}

public void Draw(PdfRenderer renderer, DrawingContext graphics)
{
if (renderer == null)
throw new ArgumentNullException(nameof(renderer));
if (graphics == null)
throw new ArgumentNullException(nameof(graphics));

Rect bounds = renderer.BoundsFromPdf(new PdfRectangle(Page, Bounds));
var brush = new SolidColorBrush(Color) { Opacity = .8 };
var pen = new Pen(new SolidColorBrush(BorderColor) { Opacity = .8 }, BorderWidth);
graphics.DrawRectangle(brush, null, bounds);

if (BorderWidth > 0)
{
graphics.DrawRectangle(null, pen, bounds);
}
}
}
}
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using PdfiumViewer.Drawing;
using Color = System.Windows.Media.Color;
using Pen = System.Windows.Media.Pen;

namespace PdfiumViewer.Core
{
public class PdfMarker : IPdfMarker
{
public int Page { get; }
public RectangleF Bounds { get; }
public Color Color { get; }
public Color BorderColor { get; }
public float BorderWidth { get; }

public PdfMarker(int page, RectangleF bounds, Color color)
: this(page, bounds, color, Colors.Transparent, 0)
{
}

public PdfMarker(int page, RectangleF bounds, Color color, Color borderColor, float borderWidth)
{
Page = page;
Bounds = bounds;
Color = color;
BorderColor = borderColor;
BorderWidth = borderWidth;
}

public void Draw(PdfRenderer renderer, DrawingContext graphics)
{
if (renderer == null)
throw new ArgumentNullException(nameof(renderer));
if (graphics == null)
throw new ArgumentNullException(nameof(graphics));

Rect? bounds = renderer.BoundsFromPdf(new PdfRectangle(Page, Bounds));
if (bounds == null) return;

Debug.WriteLine("DrawMarker[" + Page + "]: " + Color + ", bounds=" + bounds);

var brush = new SolidColorBrush(Color) { Opacity = .8 };
graphics.DrawRectangle(brush, null, bounds.Value);

if (BorderWidth > 0)
{
var pen = new Pen(new SolidColorBrush(BorderColor) { Opacity = .8 }, BorderWidth);
graphics.DrawRectangle(null, pen, bounds.Value);
}
}
}
}
16 changes: 10 additions & 6 deletions src/PdfiumViewer/Core/PdfSearchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ public bool FindNext(bool forward)
_firstMatch = _offset;

UpdateHighlights();
ScrollCurrentIntoView();

return true;
}

Expand All @@ -169,16 +167,19 @@ public bool FindNext(bool forward)
}

UpdateHighlights();
ScrollCurrentIntoView();

return _offset != _firstMatch;
}

private void ScrollCurrentIntoView()
{
if (_offset == -1) return;
var current = _bounds[_offset];
if (current.Count > 0)
Renderer.ScrollIntoView(current[0]);
if (current.Count > 0)
{
Renderer.GotoPage(current[0].Page, forceRender: true);
Renderer.ScrollIntoView(current[0]);
}
}

private int FindFirstFromCurrentPage()
Expand Down Expand Up @@ -217,13 +218,16 @@ private void UpdateHighlights()
{
for (int i = 0; i < _matches.Items.Count; i++)
{
AddMatch(i, i == _offset);
bool current = _offset == -1 ? false : i == _offset;
AddMatch(i, current);
}
}
else if (_offset != -1)
{
AddMatch(_offset, true);
}
Renderer.RedrawMarkers();
ScrollCurrentIntoView();
}

private void AddMatch(int index, bool current)
Expand Down
21 changes: 21 additions & 0 deletions src/PdfiumViewer/Drawing/PdfImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Windows.Controls;
using System.Windows.Media;

namespace PdfiumViewer.Drawing
{
public class PdfImage : Image
{
public PdfRenderer Renderer { get; set; }
public int PageNo {get; set; }

protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (Source != null)
{
Renderer.EnsureMarkers();
Renderer.DrawMarkers(drawingContext, PageNo);
}
}
}
}
Loading

0 comments on commit 5080cbe

Please sign in to comment.