PDFAction.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.Runtime.InteropServices;
  11. #if NETFX_CORE && !UNITY_WSA_10_0
  12. using WinRTLegacy.Text;
  13. #else
  14. using System.Text;
  15. #endif
  16. namespace Paroxe.PdfRenderer
  17. {
  18. #if !UNITY_WEBGL
  19. /// <summary>
  20. /// Represents the PDF action into a PDF document.
  21. /// </summary>
  22. public class PDFAction : IDisposable
  23. {
  24. private bool m_Disposed;
  25. private IntPtr m_NativePointer;
  26. private IDisposable m_Source;
  27. private PDFDocument m_Document;
  28. private ActionType m_ActionType = ActionType.Unknown;
  29. private string m_FilePath;
  30. private string m_URIPath;
  31. public PDFAction(PDFLink link, IntPtr nativePointer)
  32. {
  33. if (link == null)
  34. throw new NullReferenceException();
  35. if (nativePointer == IntPtr.Zero)
  36. throw new NullReferenceException();
  37. PDFLibrary.AddRef("PDFAction");
  38. m_Source = link;
  39. m_Document = link.Page.Document;
  40. m_NativePointer = nativePointer;
  41. }
  42. public PDFAction(PDFBookmark bookmark, IntPtr nativePointer)
  43. {
  44. if (bookmark == null)
  45. throw new NullReferenceException();
  46. if (nativePointer == IntPtr.Zero)
  47. throw new NullReferenceException();
  48. PDFLibrary.AddRef("PDFAction");
  49. m_Source = bookmark;
  50. m_Document = bookmark.Document;
  51. m_NativePointer = nativePointer;
  52. }
  53. ~PDFAction()
  54. {
  55. Dispose(false);
  56. }
  57. public enum ActionType
  58. {
  59. /// <summary>
  60. /// Unsupported action type.
  61. /// </summary>
  62. Unsupported = 0,
  63. /// <summary>
  64. /// Go to a destination within current document.
  65. /// </summary>
  66. GoTo = 1,
  67. /// <summary>
  68. /// Go to a destination within another document.
  69. /// </summary>
  70. RemoteGoTo = 2,
  71. /// <summary>
  72. /// Universal Resource Identifier, including web pages and other Internet based resources.
  73. /// </summary>
  74. Uri = 3,
  75. /// <summary>
  76. /// Launch an application or open a file.
  77. /// </summary>
  78. Launch = 4,
  79. Unknown = 133709999
  80. };
  81. public IDisposable Source
  82. {
  83. get { return m_Source; }
  84. }
  85. public PDFDocument Document
  86. {
  87. get { return m_Document; }
  88. }
  89. public IntPtr NativePointer
  90. {
  91. get { return m_NativePointer; }
  92. }
  93. public void Dispose()
  94. {
  95. Dispose(true);
  96. GC.SuppressFinalize(this);
  97. }
  98. protected virtual void Dispose(bool disposing)
  99. {
  100. if (!m_Disposed)
  101. {
  102. m_NativePointer = IntPtr.Zero;
  103. PDFLibrary.RemoveRef("PDFAction");
  104. m_Disposed = true;
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the PDFDest object associated with this action.
  109. /// </summary>
  110. /// <returns></returns>
  111. public PDFDest GetDest()
  112. {
  113. IntPtr destPtr = FPDFAction_GetDest(m_Document.NativePointer, m_NativePointer);
  114. if (destPtr != IntPtr.Zero)
  115. return new PDFDest(this, destPtr);
  116. return null;
  117. }
  118. public string GetFilePath()
  119. {
  120. if (string.IsNullOrEmpty(m_FilePath))
  121. {
  122. byte[] buffer = new byte[4096];
  123. int filePathLength = (int) FPDFAction_GetFilePath(m_NativePointer, buffer, (uint) buffer.Length);
  124. if (filePathLength > 0)
  125. m_FilePath =
  126. Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, buffer, 0,
  127. filePathLength));
  128. }
  129. return m_FilePath;
  130. }
  131. /// <summary>
  132. /// Gets type of current action.
  133. /// </summary>
  134. /// <returns></returns>
  135. public ActionType GetActionType()
  136. {
  137. if (m_ActionType == ActionType.Unknown)
  138. m_ActionType = (ActionType) FPDFAction_GetType(m_NativePointer);
  139. return m_ActionType;
  140. }
  141. /// <summary>
  142. /// Gets URL assigned to the current action.
  143. /// </summary>
  144. /// <returns></returns>
  145. public string GetURIPath()
  146. {
  147. if (string.IsNullOrEmpty(m_URIPath))
  148. {
  149. byte[] buffer = new byte[4096];
  150. int uriLength =
  151. (int)
  152. FPDFAction_GetURIPath(m_Document.NativePointer, m_NativePointer, buffer, (uint)buffer.Length);
  153. if (uriLength > 0)
  154. m_URIPath =
  155. Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, buffer, 0,
  156. uriLength));
  157. }
  158. return m_URIPath;
  159. }
  160. #region NATIVE
  161. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  162. private static extern IntPtr FPDFAction_GetDest(IntPtr document, IntPtr action);
  163. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  164. private static extern uint FPDFAction_GetFilePath(IntPtr action, [In, Out] byte[] buffer, uint buflen);
  165. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  166. private static extern uint FPDFAction_GetType(IntPtr action);
  167. [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)]
  168. private static extern uint FPDFAction_GetURIPath(IntPtr document, IntPtr action, [In, Out] byte[] buffer, uint buflen);
  169. #endregion
  170. }
  171. #endif
  172. }