PDFBookmark.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #if NETFX_CORE && !UNITY_WSA_10_0
  13. using WinRTLegacy.Text;
  14. #else
  15. using System.Text;
  16. #endif
  17. namespace Paroxe.PdfRenderer
  18. {
  19. #if !UNITY_WEBGL
  20. /// <summary>
  21. /// Represents the bookmark into a PDF document.
  22. /// </summary>
  23. public class PDFBookmark : IDisposable
  24. {
  25. private bool m_Disposed;
  26. private List<PDFBookmark> m_Bookmarks = new List<PDFBookmark>();
  27. private PDFDocument m_Document;
  28. private IntPtr m_NativePointer;
  29. private PDFBookmark m_ParentBookmark;
  30. private IPDFDevice m_Device;
  31. private string m_Title;
  32. public PDFBookmark(PDFDocument document, PDFBookmark parentBookmark, IntPtr nativePointer)
  33. : this(document, parentBookmark, nativePointer, null)
  34. {
  35. if (document == null)
  36. throw new NullReferenceException();
  37. }
  38. public PDFBookmark(PDFDocument document, PDFBookmark parentBookmark, IntPtr nativePointer, IPDFDevice device)
  39. {
  40. if (document == null)
  41. throw new NullReferenceException();
  42. m_ParentBookmark = parentBookmark;
  43. m_NativePointer = nativePointer;
  44. m_Document = document;
  45. m_Device = device;
  46. if (m_NativePointer == IntPtr.Zero)
  47. m_Title = "ROOT";
  48. PDFLibrary.AddRef("PDFBookmark");
  49. PDFBookmark firstChild = GetFirstChild();
  50. if (firstChild != null)
  51. {
  52. PDFBookmark previousSibling = firstChild;
  53. while (previousSibling != null)
  54. {
  55. m_Bookmarks.Add(previousSibling);
  56. previousSibling = previousSibling.GetNextSibling();
  57. }
  58. }
  59. }
  60. ~PDFBookmark()
  61. {
  62. Dispose(false);
  63. }
  64. public void Dispose()
  65. {
  66. Dispose(true);
  67. GC.SuppressFinalize(this);
  68. }
  69. protected virtual void Dispose(bool disposing)
  70. {
  71. if (!m_Disposed)
  72. {
  73. m_NativePointer = IntPtr.Zero;
  74. PDFLibrary.RemoveRef("PDFBookmark");
  75. m_Disposed = true;
  76. }
  77. }
  78. public PDFDocument Document
  79. {
  80. get { return m_Document; }
  81. }
  82. public int ChildCount
  83. {
  84. get { return m_Bookmarks.Count; }
  85. }
  86. public bool IsTopLevelBookmark
  87. {
  88. get { return (m_ParentBookmark == null || m_ParentBookmark.NativePointer == IntPtr.Zero); }
  89. }
  90. public PDFBookmark Parent
  91. {
  92. get { return m_ParentBookmark; }
  93. }
  94. public IntPtr NativePointer
  95. {
  96. get { return m_NativePointer; }
  97. }
  98. public IList<PDFBookmark> GetChildrenBookmarks()
  99. {
  100. return m_Bookmarks;
  101. }
  102. public IEnumerable<PDFBookmark> EnumerateChildrenBookmarks()
  103. {
  104. foreach (PDFBookmark child in m_Bookmarks)
  105. {
  106. yield return child;
  107. }
  108. }
  109. public void ExecuteBookmarkAction()
  110. {
  111. if (m_Device != null)
  112. PDFActionHandlerHelper.ExecuteBookmarkAction(m_Device, this);
  113. }
  114. public PDFBookmark GetChild(int index)
  115. {
  116. return m_Bookmarks[index];
  117. }
  118. public string GetTitle()
  119. {
  120. if (string.IsNullOrEmpty(m_Title))
  121. {
  122. byte[] buffer = new byte[4096];
  123. int length = (int) FPDFBookmark_GetTitle(m_NativePointer, buffer, (uint) buffer.Length);
  124. if (length > 0)
  125. m_Title = Encoding.Unicode.GetString(buffer);
  126. }
  127. return m_Title;
  128. }
  129. public PDFAction GetAction()
  130. {
  131. IntPtr actionPtr = FPDFBookmark_GetAction(m_NativePointer);
  132. if (actionPtr != IntPtr.Zero)
  133. return new PDFAction(this, actionPtr);
  134. return null;
  135. }
  136. public PDFDest GetDest()
  137. {
  138. IntPtr destPtr = FPDFBookmark_GetDest(m_Document.NativePointer, m_NativePointer);
  139. if (destPtr != IntPtr.Zero)
  140. return new PDFDest(this, destPtr);
  141. return null;
  142. }
  143. public PDFBookmark GetFirstChild()
  144. {
  145. IntPtr childPtr = FPDFBookmark_GetFirstChild(m_Document.NativePointer, m_NativePointer);
  146. if (childPtr != IntPtr.Zero)
  147. return new PDFBookmark(m_Document, this, childPtr, m_Device);
  148. return null;
  149. }
  150. public PDFBookmark GetNextSibling()
  151. {
  152. IntPtr nextPtr = FPDFBookmark_GetNextSibling(m_Document.NativePointer, m_NativePointer);
  153. if (nextPtr != IntPtr.Zero)
  154. return new PDFBookmark(m_Document, m_ParentBookmark, nextPtr, m_Device);
  155. return null;
  156. }
  157. #region NATIVE
  158. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  159. private static extern IntPtr FPDFBookmark_GetAction(IntPtr bookmark);
  160. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  161. private static extern IntPtr FPDFBookmark_GetDest(IntPtr document, IntPtr bookmark);
  162. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  163. private static extern IntPtr FPDFBookmark_GetFirstChild(IntPtr document, IntPtr bookmark);
  164. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  165. private static extern IntPtr FPDFBookmark_GetNextSibling(IntPtr document, IntPtr bookmark);
  166. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  167. private static extern uint FPDFBookmark_GetTitle(IntPtr bookmark, [In, Out] byte[] buffer, uint buflen);
  168. #endregion
  169. }
  170. #endif
  171. }