/* http://www.cgsoso.com/forum-211-1.html CG搜搜 Unity3d 每日Unity3d插件免费更新 更有VIP资源! CGSOSO 主打游戏开发,影视设计等CG资源素材。 插件如若商用,请务必官网购买! daily assets update for try. U should buy the asset from home store if u use it in your project! */ using Paroxe.PdfRenderer.WebGL; using System; using System.Collections; using System.IO; using System.Runtime.InteropServices; using UnityEngine; namespace Paroxe.PdfRenderer { /// /// Represents a PDF document. This class is the entry point of all functionalities. /// public class PDFDocument : IDisposable { private bool m_Disposed; private IntPtr m_NativePointer; private byte[] m_DocumentBuffer; private bool m_ValidDocument; private PDFRenderer m_ConvenienceRenderer; public static PDFJS_Promise LoadDocumentFromUrlAsync(string url) { PDFJS_Promise documentPromise = new PDFJS_Promise(); #if !UNITY_WEBGL || UNITY_EDITOR PDFJS_Library.Instance.PreparePromiseCoroutine(LoadDocumentFromWWWCoroutine, documentPromise, url).Start(); #else LoadDocumentParameters parameters = new LoadDocumentParameters(); parameters.url = url; PDFJS_Library.Instance.PreparePromiseCoroutine(LoadDocumentCoroutine, documentPromise, parameters).Start(); #endif return documentPromise; } #if !UNITY_WEBGL || UNITY_EDITOR private static IEnumerator LoadDocumentFromWWWCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object urlString) { PDFJS_Promise documentPromise = promise as PDFJS_Promise; PDFLibrary.Instance.EnsureInitialized(); while (!PDFLibrary.Instance.IsInitialized) yield return null; string url = urlString as string; WWW www = new WWW(url); yield return www; if (www.error == null) { documentPromise.HasFinished = true; documentPromise.HasSucceeded = true; documentPromise.HasReceivedJSResponse = true; documentPromise.Result = new PDFDocument(www.bytes); promiseCoroutine.ExecuteThenAction(true, documentPromise.Result); } else { documentPromise.HasFinished = true; documentPromise.HasSucceeded = false; promiseCoroutine.ExecuteThenAction(false, null); } } #endif public static PDFJS_Promise LoadDocumentFromBytesAsync(byte[] bytes) { PDFJS_Promise documentPromise = new PDFJS_Promise(); #if !UNITY_WEBGL || UNITY_EDITOR documentPromise.HasFinished = true; documentPromise.HasSucceeded = true; documentPromise.HasReceivedJSResponse = true; documentPromise.Result = new PDFDocument(bytes); #else LoadDocumentParameters parameters = new LoadDocumentParameters(); parameters.bytes = bytes; PDFJS_Library.Instance.PreparePromiseCoroutine(LoadDocumentCoroutine, documentPromise, parameters).Start(); #endif return documentPromise; } public PDFDocument(IntPtr nativePointer) { PDFLibrary.AddRef("PDFDocument"); m_NativePointer = nativePointer; m_ValidDocument = true; } #if !UNITY_WEBGL || UNITY_EDITOR /// /// Open PDF Document with the specified byte array. /// /// public PDFDocument(byte[] buffer) : this(buffer, "") { } /// /// Open PDF Document with the specified byte array. /// /// /// Can be null or empty public PDFDocument(byte[] buffer, string password) { PDFLibrary.AddRef("PDFDocument"); CommonInit(buffer, password); } /// /// Open PDF Document located at the specified path /// /// public PDFDocument(string filePath) : this(filePath, "") { } /// /// /// /// /// Can be null or empty public PDFDocument(string filePath, string password) { PDFLibrary.AddRef("PDFDocument"); #if !UNITY_WEBPLAYER && !UNITY_WINRT CommonInit(File.ReadAllBytes(filePath), password); #else m_ValidDocument = false; #endif } #endif ~PDFDocument() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (!m_Disposed) { lock (PDFLibrary.nativeLock) { if (m_NativePointer != IntPtr.Zero) { #if UNITY_WEBGL && !UNITY_EDITOR PDFJS_CloseDocument(m_NativePointer.ToInt32()); #else FPDF_CloseDocument(m_NativePointer); #endif m_NativePointer = IntPtr.Zero; } } PDFLibrary.RemoveRef("PDFDocument"); m_Disposed = true; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// /// Return a convenience PDFRenderer instance. /// public PDFRenderer Renderer { get { if (m_ConvenienceRenderer == null) m_ConvenienceRenderer = new PDFRenderer(); return m_ConvenienceRenderer; } } /// /// The byte array of the document. /// public byte[] DocumentBuffer { get { return m_DocumentBuffer; } } /// /// Return if the document is valid. The document can be invalid if the password is invalid or if the /// document itseft is corrupted. See PDFLibrary.GetLastError. /// public bool IsValid { get { return m_ValidDocument; } } public IntPtr NativePointer { get { return m_NativePointer; } } public int GetPageCount() { #if UNITY_WEBGL && !UNITY_EDITOR return PDFJS_GetPageCount(m_NativePointer.ToInt32()); #else return FPDF_GetPageCount(m_NativePointer); #endif } #if !UNITY_WEBGL || UNITY_EDITOR public Vector2 GetPageSize(int pageIndex) { double width; double height; FPDF_GetPageSizeByIndex(m_NativePointer, pageIndex, out width, out height); return new Vector2((float) width, (float) height); } #endif #if !UNITY_WEBGL || UNITY_EDITOR public int GetPageWidth(int pageIndex) { double width; double height; FPDF_GetPageSizeByIndex(m_NativePointer, pageIndex, out width, out height); return (int) width; } #endif #if !UNITY_WEBGL || UNITY_EDITOR public int GetPageHeight(int pageIndex) { double width; double height; FPDF_GetPageSizeByIndex(m_NativePointer, pageIndex, out width, out height); return (int) height; } #endif #if !UNITY_WEBGL /// /// Return the root bookmark of the document. /// /// public PDFBookmark GetRootBookmark() { return GetRootBookmark(null); } #endif #if !UNITY_WEBGL /// /// Return the root bookmark of the document. /// /// Pass the device that will receive bookmarks action /// public PDFBookmark GetRootBookmark(IPDFDevice device) { return new PDFBookmark(this, null, IntPtr.Zero, device); } #endif #if !UNITY_WEBGL || UNITY_EDITOR public PDFPage GetPage(int index) { return new PDFPage(this, index); } #endif public PDFJS_Promise GetPageAsync(int index) { return PDFPage.LoadPageAsync(this, index); } private void CommonInit(byte[] buffer, string password) { m_DocumentBuffer = buffer; if (m_DocumentBuffer != null) { #if !UNITY_WEBGL || UNITY_EDITOR m_NativePointer = FPDF_LoadMemDocument(m_DocumentBuffer, m_DocumentBuffer.Length, password); #endif m_ValidDocument = (m_NativePointer != IntPtr.Zero); } else { m_ValidDocument = false; } } #if UNITY_WEBGL && !UNITY_EDITOR class LoadDocumentParameters { public string url; public byte[] bytes; } private static IEnumerator LoadDocumentCoroutine(PDFJS_PromiseCoroutine promiseCoroutine, IPDFJS_Promise promise, object pars) { PDFJS_Promise documentPromise = promise as PDFJS_Promise; PDFLibrary.Instance.EnsureInitialized(); while (!PDFLibrary.Instance.IsInitialized) yield return null; LoadDocumentParameters parameters = pars as LoadDocumentParameters; if (!string.IsNullOrEmpty(parameters.url)) PDFJS_LoadDocumentFromURL(promise.PromiseHandle, parameters.url); else PDFJS_LoadDocumentFromBytes(promise.PromiseHandle, Convert.ToBase64String(parameters.bytes)); while (!promiseCoroutine.Promise.HasReceivedJSResponse) yield return null; if (documentPromise.HasSucceeded) { int documentHandle = int.Parse(promiseCoroutine.Promise.JSObjectHandle); PDFDocument document = new PDFDocument(new IntPtr(documentHandle)); documentPromise.Result = document; documentPromise.HasFinished = true; promiseCoroutine.ExecuteThenAction(true, documentHandle); } else { documentPromise.Result = null; documentPromise.HasFinished = true; promiseCoroutine.ExecuteThenAction(false, null); } } #endif #region NATIVE #if !UNITY_WEBGL || UNITY_EDITOR [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern void FPDF_CloseDocument(IntPtr document); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern uint FPDF_GetDocPermissions(IntPtr document); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern int FPDF_GetPageCount(IntPtr document); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY, CharSet = CharSet.Ansi)] private static extern IntPtr FPDF_LoadMemDocument(byte[] data_buf, int size, string password); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern int FPDF_GetPageSizeByIndex(IntPtr document, int page_index, out double width, out double height); #else [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern void PDFJS_LoadDocumentFromURL(string promiseHandle, string documentUrl); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern void PDFJS_LoadDocumentFromBytes(string promiseHandle, string base64); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern void PDFJS_CloseDocument(int document); [DllImport(PDFLibrary.PLUGIN_ASSEMBLY)] private static extern int PDFJS_GetPageCount(int documentHandle); #endif #endregion } }