using System;
using Paroxe.PdfRenderer.Internal;
using System.Text;
namespace Paroxe.PdfRenderer
{
#if !UNITY_WEBGL
///
/// Represents the PDF action into a PDF document.
///
public class PDFAction : IDisposable
{
private bool m_Disposed;
private IntPtr m_NativePointer;
private IDisposable m_Source;
private PDFDocument m_Document;
private ActionType m_ActionType = ActionType.Unknown;
private string m_FilePath;
private string m_URIPath;
public PDFAction(PDFLink link, IntPtr nativePointer)
{
if (link == null)
throw new NullReferenceException();
if (nativePointer == IntPtr.Zero)
throw new NullReferenceException();
PDFLibrary.AddRef("PDFAction");
m_Source = link;
m_Document = link.Page.Document;
m_NativePointer = nativePointer;
}
public PDFAction(PDFBookmark bookmark, IntPtr nativePointer)
{
if (bookmark == null)
throw new NullReferenceException();
if (nativePointer == IntPtr.Zero)
throw new NullReferenceException();
PDFLibrary.AddRef("PDFAction");
m_Source = bookmark;
m_Document = bookmark.Document;
m_NativePointer = nativePointer;
}
~PDFAction()
{
Dispose(false);
}
public enum ActionType
{
///
/// Unsupported action type.
///
Unsupported = 0,
///
/// Go to a destination within current document.
///
GoTo = 1,
///
/// Go to a destination within another document.
///
RemoteGoTo = 2,
///
/// Universal Resource Identifier, including web pages and other Internet based resources.
///
Uri = 3,
///
/// Launch an application or open a file.
///
Launch = 4,
Unknown = 133709999
};
public IDisposable Source
{
get { return m_Source; }
}
public PDFDocument Document
{
get { return m_Document; }
}
public IntPtr NativePointer
{
get { return m_NativePointer; }
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!m_Disposed)
{
m_NativePointer = IntPtr.Zero;
PDFLibrary.RemoveRef("PDFAction");
m_Disposed = true;
}
}
///
/// Gets the PDFDest object associated with this action.
///
///
public PDFDest GetDest()
{
IntPtr destPtr = NativeMethods.FPDFAction_GetDest(m_Document.NativePointer, m_NativePointer);
if (destPtr != IntPtr.Zero)
return new PDFDest(this, destPtr);
return null;
}
public string GetFilePath()
{
if (string.IsNullOrEmpty(m_FilePath))
{
byte[] buffer = new byte[4096];
int filePathLength = (int)NativeMethods.FPDFAction_GetFilePath(m_NativePointer, buffer, (uint)buffer.Length);
if (filePathLength > 0)
m_FilePath =
Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, buffer, 0,
filePathLength));
}
return m_FilePath;
}
///
/// Gets type of current action.
///
///
public ActionType GetActionType()
{
if (m_ActionType == ActionType.Unknown)
m_ActionType = (ActionType)NativeMethods.FPDFAction_GetType(m_NativePointer);
return m_ActionType;
}
///
/// Gets URL assigned to the current action.
///
///
public string GetURIPath()
{
if (string.IsNullOrEmpty(m_URIPath))
{
byte[] buffer = new byte[4096];
int uriLength = (int) NativeMethods.FPDFAction_GetURIPath(m_Document.NativePointer, m_NativePointer, buffer, (uint)buffer.Length);
if (uriLength > 0)
m_URIPath =
Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, buffer, 0,
uriLength));
}
return m_URIPath;
}
}
#endif
}