PDFSearchHandle.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. http://www.cgsoso.com/forum-211-1.html
  3. CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源!
  4. CGSOSO 主打游戏开发,影视设计等CG资源素材。
  5. 插件如若商用,请务必官网购买!
  6. daily assets update for try.
  7. U should buy the asset from home store if u use it in your project!
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Runtime.InteropServices;
  12. namespace Paroxe.PdfRenderer
  13. {
  14. #if !UNITY_WEBGL
  15. /// <summary>
  16. /// Represent a search session within a specific page. To search in entire document use PDFTextPage.Search
  17. /// </summary>
  18. public class PDFSearchHandle : IDisposable
  19. {
  20. public enum MatchOption
  21. {
  22. NONE = 0x00000000,
  23. MATCH_CASE = 0x00000001,
  24. MATCH_WHOLE_WORD = 0x00000002,
  25. MATCH_CASE_AND_WHOLE_WORD = 0x00000003
  26. }
  27. private bool m_Disposed;
  28. private IntPtr m_NativePointer;
  29. private PDFTextPage m_TextPage;
  30. public PDFSearchHandle(PDFTextPage textPage, byte[] findWhatUnicode, int startIndex,
  31. MatchOption flags = MatchOption.NONE)
  32. {
  33. if (textPage == null)
  34. throw new NullReferenceException();
  35. if (startIndex < 0)
  36. throw new ArgumentOutOfRangeException();
  37. PDFLibrary.AddRef("PDFSearchHandle");
  38. m_TextPage = textPage;
  39. IntPtr unmanagedPointer = Marshal.AllocHGlobal(findWhatUnicode.Length);
  40. Marshal.Copy(findWhatUnicode, 0, unmanagedPointer, findWhatUnicode.Length);
  41. m_NativePointer = FPDFText_FindStart(textPage.NativePointer, unmanagedPointer, (int) flags, startIndex);
  42. Marshal.FreeHGlobal(unmanagedPointer);
  43. }
  44. ~PDFSearchHandle()
  45. {
  46. Dispose(false);
  47. }
  48. public void Dispose()
  49. {
  50. Dispose(true);
  51. GC.SuppressFinalize(this);
  52. }
  53. protected virtual void Dispose(bool disposing)
  54. {
  55. if (!m_Disposed)
  56. {
  57. lock (PDFLibrary.nativeLock)
  58. {
  59. if (m_NativePointer != IntPtr.Zero)
  60. {
  61. FPDFText_FindClose(m_NativePointer);
  62. m_NativePointer = IntPtr.Zero;
  63. }
  64. }
  65. PDFLibrary.RemoveRef("PDFSearchHandle");
  66. m_Disposed = true;
  67. }
  68. }
  69. public IntPtr NativePointer
  70. {
  71. get { return m_NativePointer; }
  72. }
  73. /// <summary>
  74. /// Return an array containing all the searchResults. If there is no result, this function return null.
  75. /// </summary>
  76. /// <returns></returns>
  77. public IList<PDFSearchResult> GetResults()
  78. {
  79. List<PDFSearchResult> results = new List<PDFSearchResult>();
  80. foreach (PDFSearchResult result in EnumerateSearchResults())
  81. {
  82. results.Add(result);
  83. }
  84. return results;
  85. }
  86. /// <summary>
  87. /// Enumerate search results.
  88. /// </summary>
  89. /// <returns></returns>
  90. public IEnumerable<PDFSearchResult> EnumerateSearchResults()
  91. {
  92. if (m_NativePointer != IntPtr.Zero)
  93. {
  94. while (FPDFText_FindNext(m_NativePointer))
  95. yield return new PDFSearchResult(
  96. m_TextPage.PageIndex,
  97. FPDFText_GetSchResultIndex(m_NativePointer),
  98. FPDFText_GetSchCount(m_NativePointer));
  99. }
  100. }
  101. /// <summary>
  102. /// Get the next search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
  103. /// </summary>
  104. /// <returns></returns>
  105. public PDFSearchResult FindNext()
  106. {
  107. if (FPDFText_FindNext(m_NativePointer))
  108. return new PDFSearchResult(
  109. m_TextPage.PageIndex,
  110. FPDFText_GetSchResultIndex(m_NativePointer),
  111. FPDFText_GetSchCount(m_NativePointer));
  112. return new PDFSearchResult(-1, -1, -1);
  113. }
  114. /// <summary>
  115. /// Get the previous search result. If there is no other result, the function returns an invalid searchResult (validate it with PDFSearchResult.IsValid)
  116. /// </summary>
  117. /// <returns></returns>
  118. public PDFSearchResult FindPrevious()
  119. {
  120. if (FPDFText_FindPrev(m_NativePointer))
  121. return new PDFSearchResult(
  122. m_TextPage.PageIndex,
  123. FPDFText_GetSchResultIndex(m_NativePointer),
  124. FPDFText_GetSchCount(m_NativePointer));
  125. return new PDFSearchResult(-1, -1, -1);
  126. }
  127. #region NATIVE
  128. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  129. private static extern IntPtr FPDFText_FindStart(IntPtr text_page, IntPtr buffer, int flags, int start_index);
  130. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  131. private static extern void FPDFText_FindClose(IntPtr handle);
  132. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  133. private static extern bool FPDFText_FindNext(IntPtr handle);
  134. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  135. private static extern bool FPDFText_FindPrev(IntPtr handle);
  136. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  137. private static extern int FPDFText_GetSchCount(IntPtr handle);
  138. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  139. private static extern int FPDFText_GetSchResultIndex(IntPtr handle);
  140. #endregion
  141. }
  142. #endif
  143. }