PDFTextPage.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using Paroxe.PdfRenderer.Internal;
  4. using UnityEngine;
  5. namespace Paroxe.PdfRenderer
  6. {
  7. #if !UNITY_WEBGL
  8. /// <summary>
  9. /// This class allow to access the text of a page.
  10. /// </summary>
  11. public class PDFTextPage : IDisposable, IEquatable<PDFTextPage>
  12. {
  13. private bool m_Disposed;
  14. private IntPtr m_NativePointer;
  15. private PDFPage m_Page;
  16. private static Dictionary<IntPtr, int> s_InstanceMap = new Dictionary<IntPtr, int>();
  17. public PDFTextPage(PDFPage page)
  18. {
  19. if (page == null)
  20. throw new NullReferenceException();
  21. PDFLibrary.AddRef("PDFTextPage");
  22. m_Page = page;
  23. m_NativePointer = NativeMethods.FPDFText_LoadPage(m_Page.NativePointer);
  24. if (m_NativePointer != IntPtr.Zero)
  25. {
  26. if (s_InstanceMap.ContainsKey(m_NativePointer))
  27. {
  28. s_InstanceMap[m_NativePointer] = s_InstanceMap[m_NativePointer] + 1;
  29. }
  30. else
  31. s_InstanceMap[m_NativePointer] = 1;
  32. }
  33. }
  34. ~PDFTextPage()
  35. {
  36. Dispose(false);
  37. }
  38. public void Dispose()
  39. {
  40. Dispose(true);
  41. GC.SuppressFinalize(this);
  42. }
  43. protected virtual void Dispose(bool _)
  44. {
  45. if (!m_Disposed)
  46. {
  47. lock (PDFLibrary.nativeLock)
  48. {
  49. if (m_NativePointer != IntPtr.Zero)
  50. {
  51. s_InstanceMap[m_NativePointer] = s_InstanceMap[m_NativePointer] - 1;
  52. if (s_InstanceMap[m_NativePointer] == 0)
  53. {
  54. ((IIPDFPageInternal)m_Page).OnTextPageClose(this);
  55. NativeMethods.FPDFText_ClosePage(m_NativePointer);
  56. s_InstanceMap.Remove(m_NativePointer);
  57. m_NativePointer = IntPtr.Zero;
  58. }
  59. }
  60. }
  61. PDFLibrary.RemoveRef("PDFTextPage");
  62. m_Disposed = true;
  63. }
  64. }
  65. public IntPtr NativePointer
  66. {
  67. get { return m_NativePointer; }
  68. }
  69. /// <summary>
  70. /// Return a refence to the page.
  71. /// </summary>
  72. public PDFPage Page
  73. {
  74. get { return m_Page; }
  75. }
  76. /// <summary>
  77. /// Return the page index of the text.
  78. /// </summary>
  79. public int PageIndex
  80. {
  81. get { return m_Page.PageIndex; }
  82. }
  83. /// <summary>
  84. /// Return the number of character in the page.
  85. /// </summary>
  86. /// <returns></returns>
  87. public int CountChars()
  88. {
  89. return NativeMethods.FPDFText_CountChars(m_NativePointer);
  90. }
  91. /// <summary>
  92. /// Count number of rectangular areas occupied by a segment of texts.
  93. /// </summary>
  94. /// <param name="startIndex"></param>
  95. /// <param name="count"></param>
  96. /// <returns></returns>
  97. public int CountRects(int startIndex, int count)
  98. {
  99. return NativeMethods.FPDFText_CountRects(m_NativePointer, startIndex, count);
  100. }
  101. /// <summary>
  102. /// Extract text within a rectangular boundary on the page.
  103. /// </summary>
  104. /// <param name="left"></param>
  105. /// <param name="top"></param>
  106. /// <param name="right"></param>
  107. /// <param name="bottom"></param>
  108. /// <param name="charCount"></param>
  109. /// <returns></returns>
  110. public string GetBoundedText(float left, float top, float right, float bottom, int charCount)
  111. {
  112. byte[] textBuffer = new byte[(charCount + 1) * 2];
  113. NativeMethods.FPDFText_GetBoundedText(m_NativePointer, left, top, right, bottom, textBuffer, textBuffer.Length);
  114. return PDFLibrary.Encoding.GetString(textBuffer);
  115. }
  116. /// <summary>
  117. /// Get bounding box of a particular character.
  118. /// </summary>
  119. /// <param name="charIndex"></param>
  120. /// <returns></returns>
  121. public Rect GetCharBox(int charIndex)
  122. {
  123. double left;
  124. double right;
  125. double bottom;
  126. double top;
  127. NativeMethods.FPDFText_GetCharBox(m_NativePointer, charIndex, out left, out right, out bottom, out top);
  128. return new Rect((float)left, (float)top, Mathf.Abs((float)right - (float)left),
  129. Mathf.Abs((float)bottom - (float)top));
  130. }
  131. /// <summary>
  132. /// Get the index of a character at or nearby a certain position on the page.
  133. /// </summary>
  134. /// <param name="pos"></param>
  135. /// <param name="tolerance"></param>
  136. /// <returns></returns>
  137. public int GetCharIndexAtPos(Vector2 pos, Vector2 tolerance)
  138. {
  139. /*Vector2 pagePos = m_Page.ConvertUnityUIDevicePositionToPagePosition(pos,
  140. m_Page.Document.GetPageSize(m_Page.PageIndex));*/
  141. return NativeMethods.FPDFText_GetCharIndexAtPos(m_NativePointer, pos.x, pos.y, tolerance.x, tolerance.y);
  142. }
  143. /// <summary>
  144. /// Get the font size of a particular character.
  145. /// </summary>
  146. /// <param name="charIndex"></param>
  147. /// <returns></returns>
  148. public double GetFontSize(int charIndex)
  149. {
  150. return NativeMethods.FPDFText_GetFontSize(m_NativePointer, charIndex);
  151. }
  152. /// <summary>
  153. /// Get a rectangular area from the result generated by CountRects
  154. /// </summary>
  155. /// <param name="rectIndex"></param>
  156. /// <returns></returns>
  157. public Rect GetRect(int rectIndex)
  158. {
  159. double left;
  160. double right;
  161. double bottom;
  162. double top;
  163. NativeMethods.FPDFText_GetRect(m_NativePointer, rectIndex, out left, out top, out right, out bottom);
  164. return new Rect((float)left, (float)top, Mathf.Abs((float)right - (float)left),
  165. Mathf.Abs((float)bottom - (float)top));
  166. }
  167. /// <summary>
  168. /// Extract text string from the page.
  169. /// </summary>
  170. /// <param name="startIndex"></param>
  171. /// <param name="count"></param>
  172. /// <returns></returns>
  173. public string GetText(int startIndex, int count)
  174. {
  175. byte[] textBuffer = new byte[(count + 1) * 2];
  176. NativeMethods.FPDFText_GetText(m_NativePointer, startIndex, count, textBuffer);
  177. return PDFLibrary.Encoding.GetString(textBuffer);
  178. }
  179. /// <summary>
  180. /// Get a character in the page.
  181. /// </summary>
  182. /// <param name="charIndex"></param>
  183. /// <returns></returns>
  184. public string GetChar(int charIndex)
  185. {
  186. return char.ConvertFromUtf32((int) NativeMethods.FPDFText_GetUnicode(m_NativePointer, charIndex));
  187. }
  188. public IList<PDFSearchResult> Search(string findWhat,
  189. PDFSearchHandle.MatchOption flags = PDFSearchHandle.MatchOption.NONE, int startIndex = 0)
  190. {
  191. if (string.IsNullOrEmpty(findWhat.Trim()))
  192. return new List<PDFSearchResult>();
  193. return Search(PDFLibrary.Encoding.GetBytes(findWhat.Trim() + "\0"), flags, startIndex);
  194. }
  195. public IList<PDFSearchResult> Search(byte[] findWhatUnicode,
  196. PDFSearchHandle.MatchOption flags = PDFSearchHandle.MatchOption.NONE, int startIndex = 0)
  197. {
  198. List<PDFSearchResult> searchResults = new List<PDFSearchResult>();
  199. if (findWhatUnicode == null)
  200. return searchResults;
  201. using (PDFSearchHandle searchHandle = new PDFSearchHandle(this, findWhatUnicode, startIndex, flags))
  202. {
  203. foreach (PDFSearchResult result in searchHandle.EnumerateSearchResults())
  204. {
  205. searchResults.Add(result);
  206. }
  207. }
  208. return searchResults;
  209. }
  210. public bool Equals(PDFTextPage other)
  211. {
  212. return (m_NativePointer != IntPtr.Zero && m_NativePointer == other.m_NativePointer);
  213. }
  214. }
  215. #endif
  216. }