123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- /*
- 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
- {
- /// <summary>
- /// Represents a PDF document. This class is the entry point of all functionalities.
- /// </summary>
- 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<PDFDocument> LoadDocumentFromUrlAsync(string url)
- {
- PDFJS_Promise<PDFDocument> documentPromise = new PDFJS_Promise<PDFDocument>();
- #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<PDFDocument> documentPromise = promise as PDFJS_Promise<PDFDocument>;
- 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<PDFDocument> LoadDocumentFromBytesAsync(byte[] bytes)
- {
- PDFJS_Promise<PDFDocument> documentPromise = new PDFJS_Promise<PDFDocument>();
- #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
- /// <summary>
- /// Open PDF Document with the specified byte array.
- /// </summary>
- /// <param name="buffer"></param>
- public PDFDocument(byte[] buffer)
- : this(buffer, "")
- { }
- /// <summary>
- /// Open PDF Document with the specified byte array.
- /// </summary>
- /// <param name="buffer"></param>
- /// <param name="password">Can be null or empty</param>
- public PDFDocument(byte[] buffer, string password)
- {
- PDFLibrary.AddRef("PDFDocument");
- CommonInit(buffer, password);
- }
- /// <summary>
- /// Open PDF Document located at the specified path
- /// </summary>
- /// <param name="filePath"></param>
- public PDFDocument(string filePath)
- : this(filePath, "")
- { }
- /// <summary>
- ///
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="password">Can be null or empty</param>
- 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);
- }
- /// <summary>
- /// Return a convenience PDFRenderer instance.
- /// </summary>
- public PDFRenderer Renderer
- {
- get
- {
- if (m_ConvenienceRenderer == null)
- m_ConvenienceRenderer = new PDFRenderer();
- return m_ConvenienceRenderer;
- }
- }
- /// <summary>
- /// The byte array of the document.
- /// </summary>
- public byte[] DocumentBuffer
- {
- get { return m_DocumentBuffer; }
- }
- /// <summary>
- /// 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.
- /// </summary>
- 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
- /// <summary>
- /// Return the root bookmark of the document.
- /// </summary>
- /// <returns></returns>
- public PDFBookmark GetRootBookmark()
- {
- return GetRootBookmark(null);
- }
- #endif
- #if !UNITY_WEBGL
- /// <summary>
- /// Return the root bookmark of the document.
- /// </summary>
- /// <param name="device">Pass the device that will receive bookmarks action</param>
- /// <returns></returns>
- 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<PDFPage> 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<PDFDocument> documentPromise = promise as PDFJS_Promise<PDFDocument>;
- 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
- }
- }
|