PDFSearchHandle.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using Paroxe.PdfRenderer.Internal;
  5. namespace Paroxe.PdfRenderer
  6. {
  7. #if !UNITY_WEBGL
  8. /// <summary>
  9. /// Represent a search session within a specific page. To search in entire document use PDFTextPage.Search
  10. /// </summary>
  11. public class PDFSearchHandle : IDisposable
  12. {
  13. public enum MatchOption
  14. {
  15. NONE = 0x00000000,
  16. MATCH_CASE = 0x00000001,
  17. MATCH_WHOLE_WORD = 0x00000002,
  18. MATCH_CASE_AND_WHOLE_WORD = 0x00000003
  19. }
  20. private bool m_Disposed;
  21. private IntPtr m_NativePointer;
  22. private PDFTextPage m_TextPage;
  23. public PDFSearchHandle(PDFTextPage textPage, byte[] findWhatUnicode, int startIndex,
  24. MatchOption flags = MatchOption.NONE)
  25. {
  26. if (textPage == null)
  27. throw new NullReferenceException();
  28. if (startIndex < 0)
  29. throw new ArgumentOutOfRangeException();
  30. PDFLibrary.AddRef("PDFSearchHandle");
  31. m_TextPage = textPage;
  32. IntPtr unmanagedPointer = Marshal.AllocHGlobal(findWhatUnicode.Length);
  33. Marshal.Copy(findWhatUnicode, 0, unmanagedPointer, findWhatUnicode.Length);
  34. m_NativePointer = NativeMethods.FPDFText_FindStart(textPage.NativePointer, unmanagedPointer, (uint)flags, startIndex);
  35. Marshal.FreeHGlobal(unmanagedPointer);
  36. }
  37. ~PDFSearchHandle()
  38. {
  39. Dispose(false);
  40. }
  41. public void Dispose()
  42. {
  43. Dispose(true);
  44. GC.SuppressFinalize(this);
  45. }
  46. protected virtual void Dispose(bool disposing)
  47. {
  48. if (!m_Disposed)
  49. {
  50. lock (PDFLibrary.nativeLock)
  51. {
  52. if (m_NativePointer != IntPtr.Zero)
  53. {
  54. NativeMethods.FPDFText_FindClose(m_NativePointer);
  55. m_NativePointer = IntPtr.Zero;
  56. }
  57. }
  58. PDFLibrary.RemoveRef("PDFSearchHandle");
  59. m_Disposed = true;
  60. }
  61. }
  62. public IntPtr NativePointer
  63. {
  64. get { return m_NativePointer; }
  65. }
  66. /// <summary>
  67. /// Return an array containing all the searchResults. If there is no result, this function return null.
  68. /// </summary>
  69. /// <returns></returns>
  70. public IList<PDFSearchResult> GetResults()
  71. {
  72. List<PDFSearchResult> results = new List<PDFSearchResult>();
  73. foreach (PDFSearchResult result in EnumerateSearchResults())
  74. {
  75. results.Add(result);
  76. }
  77. return results;
  78. }
  79. /// <summary>
  80. /// Enumerate search results.
  81. /// </summary>
  82. /// <returns></returns>
  83. public IEnumerable<PDFSearchResult> EnumerateSearchResults()
  84. {
  85. if (m_NativePointer != IntPtr.Zero)
  86. {
  87. while (NativeMethods.FPDFText_FindNext(m_NativePointer))
  88. yield return new PDFSearchResult(
  89. m_TextPage.PageIndex, NativeMethods.FPDFText_GetSchResultIndex(m_NativePointer), NativeMethods.FPDFText_GetSchCount(m_NativePointer));
  90. }
  91. }
  92. /// <summary>
  93. /// Get the next search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
  94. /// </summary>
  95. /// <returns></returns>
  96. public PDFSearchResult FindNext()
  97. {
  98. if (NativeMethods.FPDFText_FindNext(m_NativePointer))
  99. return new PDFSearchResult(
  100. m_TextPage.PageIndex, NativeMethods.FPDFText_GetSchResultIndex(m_NativePointer), NativeMethods.FPDFText_GetSchCount(m_NativePointer));
  101. return new PDFSearchResult(-1, -1, -1);
  102. }
  103. /// <summary>
  104. /// Get the previous search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
  105. /// </summary>
  106. /// <returns></returns>
  107. public PDFSearchResult FindPrevious()
  108. {
  109. if (NativeMethods.FPDFText_FindPrev(m_NativePointer))
  110. return new PDFSearchResult(
  111. m_TextPage.PageIndex, NativeMethods.FPDFText_GetSchResultIndex(m_NativePointer), NativeMethods.FPDFText_GetSchCount(m_NativePointer));
  112. return new PDFSearchResult(-1, -1, -1);
  113. }
  114. }
  115. #endif
  116. }