Rank: Member
Groups: Registered
Joined: 2/2/2016(UTC) Posts: 20
|
I would like to implement my own search with PdfFind (instead of PdfSearch). It will support Find, Find Next, and Find Previous across the whole document text (not just single pages) and it will highlight text when found. I looked over PdfSearch code samples. Do you also have samples for PdfFind? Here is the first issue I'm having: After using PdfFind.Find() and getting back a valid, non-null PdfFind object, then PdfFind.FindNext() throws an "External Exception E0434352" message if you change the current PdfViewer page and then invoke PdfFind.FindNext(). With the code below, if I do StartSearch("needle", 0), then scroll the PdfViewer page to a different page, then do FindNext(), I get the exception. Code:
public int StartSearch(string text, int pageIndex)
{
if (pageIndex < 0 || pageIndex >= pdfViewer1.Document.Pages.Count)
{
return -1;
}
lastPdfFind = null; // class var
for (lastPdfFindPageIndex = pageIndex; lastPdfFindPageIndex < pdfViewer1.Document.Pages.Count; lastPdfFindPageIndex++)
{
PdfText pdfText = pdfViewer1.Document.Pages[lastPdfFindPageIndex].Text;
lastPdfFind = PdfFind.Find(text, pdfText, Patagames.Pdf.Enums.FindFlags.None, 0);
if (lastPdfFind != null) // PdfFind.Find() appears to return null if text is not found (undocumented)
{
break;
}
}
if (lastPdfFind != null)
{
HighlightSearchResult(lastPdfFindPageIndex, lastPdfFind);
return lastPdfFind.CharIndex;
}
return -1;
}
public int FindNext()
{
if (this.lastPdfFind == null)
{
return -1;
}
if (lastPdfFind.FindNext()) // exception here if PdfViewer page was changed manually by scrolling
{
HighlightSearchResult(lastPdfFindPageIndex, lastPdfFind);
//MessageBox.Show("lastPdfFind.CharIndex: " + this.lastPdfFind.CharIndex);
return this.lastPdfFind.CharIndex;
}
return -1;
}
private void HighlightSearchResult(int pageIndex, PdfFind pdfFind)
{
pdfViewer1.RemoveHighlightFromText();
pdfViewer1.HighlightText(pageIndex, pdfFind.CharIndex, pdfFind.CharsCount, Color.FromArgb(90, 255, 255, 0));
pdfViewer1.CurrentIndex = pageIndex;
var page = pdfViewer1.Document.Pages[pageIndex];
var ti = page.Text.GetTextInfo(pdfFind.CharIndex, 1);
if (ti.Rects == null || ti.Rects.Count == 0)
{
pdfViewer1.ScrollToPage(pageIndex);
return;
}
var pt = pdfViewer1.PageToClient(pageIndex, new PointF(ti.Rects[0].left, ti.Rects[0].top));
var curPt = pdfViewer1.AutoScrollPosition;
pdfViewer1.AutoScrollPosition = new Point(pt.X - curPt.X, pt.Y - curPt.Y);
}
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,113
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
To prevent the memory deficit the PdfViewer control creates PdfPage object for each page immediately before showing it, and dispose it when it does not shown to the user.
When you create PdfFind via the Find() method, you are passing the PdfText object and it's stored inside PdfFind. PdfText object it's an internal property of PdfPage object. So, when the PdfPage is disposed then PdfText is disposed too, consequently the PdfFind object becomes invalid.
|
|
|
|
Rank: Member
Groups: Registered
Joined: 2/2/2016(UTC) Posts: 20
|
Thanks for the explanation. Would you recommend then that I not use PdfFind if I'm trying to do what I mentioned above (find, find next, find previous with highlighting on the entire document)? Or, is there a way to retain the last PdfFind so that I can continue find next/find previous even after the page has been changed? Edited by user Friday, February 5, 2016 9:34:59 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,113
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
there is many possible solutions. e.g. you can create new instance of the PdfDocument class and use it for search independently from the Viewer.
|
|
|
|
Rank: Member
Groups: Registered
Joined: 2/2/2016(UTC) Posts: 20
|
Thanks for the help again. Having just started using the library, I don't know which of many possible solutions would be a good solution with PdfFind. Is creating a second PdfDocument costly with respect to memory usage? Is it possible just to retain some search state information without another PdfDocument and then use that?
|
|
|
|
Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,113
Thanks: 8 times Was thanked: 130 time(s) in 127 post(s)
|
The page will be recreated when you try to use it again. It means that you can safe to search text on a page (in single-thread environment). Just do not store PdfFind object (lastPdfFind) for future use and your code will be works fine.
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The Patagames Software Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close