PDFBookmark.cs 4.9 KB

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