123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using Paroxe.PdfRenderer.Internal;
- namespace Paroxe.PdfRenderer
- {
- #if !UNITY_WEBGL
- /// <summary>
- /// Represents the bookmark into a PDF document.
- /// </summary>
- public class PDFBookmark : IDisposable
- {
- private bool m_Disposed;
- private List<PDFBookmark> m_Bookmarks = new List<PDFBookmark>();
- private PDFDocument m_Document;
- private IntPtr m_NativePointer;
- private PDFBookmark m_ParentBookmark;
- private IPDFDevice m_Device;
- private string m_Title;
- public PDFBookmark(PDFDocument document, PDFBookmark parentBookmark, IntPtr nativePointer)
- : this(document, parentBookmark, nativePointer, null)
- {
- if (document == null)
- throw new NullReferenceException();
- }
- public PDFBookmark(PDFDocument document, PDFBookmark parentBookmark, IntPtr nativePointer, IPDFDevice device)
- {
- if (document == null)
- throw new NullReferenceException();
- m_ParentBookmark = parentBookmark;
- m_NativePointer = nativePointer;
- m_Document = document;
- m_Device = device;
- if (m_NativePointer == IntPtr.Zero)
- m_Title = "ROOT";
- PDFLibrary.AddRef("PDFBookmark");
- PDFBookmark firstChild = GetFirstChild();
- if (firstChild != null)
- {
- PDFBookmark previousSibling = firstChild;
- while (previousSibling != null)
- {
- m_Bookmarks.Add(previousSibling);
- previousSibling = previousSibling.GetNextSibling();
- }
- }
- }
- ~PDFBookmark()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!m_Disposed)
- {
- m_NativePointer = IntPtr.Zero;
- PDFLibrary.RemoveRef("PDFBookmark");
- m_Disposed = true;
- }
- }
- public PDFDocument Document
- {
- get { return m_Document; }
- }
- public int ChildCount
- {
- get { return m_Bookmarks.Count; }
- }
- public bool IsTopLevelBookmark
- {
- get { return (m_ParentBookmark == null || m_ParentBookmark.NativePointer == IntPtr.Zero); }
- }
- public PDFBookmark Parent
- {
- get { return m_ParentBookmark; }
- }
- public IntPtr NativePointer
- {
- get { return m_NativePointer; }
- }
- public IList<PDFBookmark> GetChildrenBookmarks()
- {
- return m_Bookmarks;
- }
- public IEnumerable<PDFBookmark> EnumerateChildrenBookmarks()
- {
- foreach (PDFBookmark child in m_Bookmarks)
- {
- yield return child;
- }
- }
- public void ExecuteBookmarkAction()
- {
- if (m_Device != null)
- PDFActionHandlerHelper.ExecuteBookmarkAction(m_Device, this);
- }
- public PDFBookmark GetChild(int index)
- {
- return m_Bookmarks[index];
- }
- public string GetTitle()
- {
- if (string.IsNullOrEmpty(m_Title))
- {
- byte[] buffer = new byte[4096];
- int length = (int) NativeMethods.FPDFBookmark_GetTitle(m_NativePointer, buffer, (uint)buffer.Length);
- if (length > 0)
- m_Title = PDFLibrary.Encoding.GetString(buffer);
- }
- return m_Title;
- }
- public PDFAction GetAction()
- {
- IntPtr actionPtr = NativeMethods.FPDFBookmark_GetAction(m_NativePointer);
- if (actionPtr != IntPtr.Zero)
- return new PDFAction(this, actionPtr);
- return null;
- }
- public PDFDest GetDest()
- {
- IntPtr destPtr = NativeMethods.FPDFBookmark_GetDest(m_Document.NativePointer, m_NativePointer);
- if (destPtr != IntPtr.Zero)
- return new PDFDest(this, destPtr);
- return null;
- }
- public PDFBookmark GetFirstChild()
- {
- IntPtr childPtr = NativeMethods.FPDFBookmark_GetFirstChild(m_Document.NativePointer, m_NativePointer);
- if (childPtr != IntPtr.Zero)
- return new PDFBookmark(m_Document, this, childPtr, m_Device);
- return null;
- }
- public PDFBookmark GetNextSibling()
- {
- IntPtr nextPtr = NativeMethods.FPDFBookmark_GetNextSibling(m_Document.NativePointer, m_NativePointer);
- if (nextPtr != IntPtr.Zero)
- return new PDFBookmark(m_Document, m_ParentBookmark, nextPtr, m_Device);
- return null;
- }
- }
- #endif
- }
|