PDFAction.cs 4.9 KB

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