WebGL_API_Usage.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using System.Collections;
  3. using Paroxe.PdfRenderer.WebGL;
  4. using UnityEngine.UI;
  5. namespace Paroxe.PdfRenderer.Examples
  6. {
  7. public class WebGL_API_Usage : MonoBehaviour
  8. {
  9. public RawImage m_RawImage;
  10. private IEnumerator Start()
  11. {
  12. Debug.Log("WebGLTest: ");
  13. PDFJS_Promise<PDFDocument> documentPromise = PDFDocument.LoadDocumentFromBytesAsync(PDFBytesSupplierExample.PDFSampleByteArray);
  14. while (!documentPromise.HasFinished)
  15. yield return null;
  16. if (!documentPromise.HasSucceeded)
  17. {
  18. Debug.Log("Fail: documentPromise");
  19. yield break;
  20. }
  21. Debug.Log("Success: documentPromise");
  22. PDFDocument document = documentPromise.Result;
  23. PDFJS_Promise<PDFPage> pagePromise = document.GetPageAsync(0);
  24. while (!pagePromise.HasFinished)
  25. yield return null;
  26. if (!pagePromise.HasSucceeded)
  27. {
  28. Debug.Log("Fail: pagePromise");
  29. yield break;
  30. }
  31. Debug.Log("Success: pagePromise");
  32. PDFPage page = pagePromise.Result;
  33. PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(page, (int)page.GetPageSize().x, (int)page.GetPageSize().y);
  34. while (!renderPromise.HasFinished)
  35. yield return null;
  36. if (!renderPromise.HasSucceeded)
  37. {
  38. Debug.Log("Fail: pagePromise");
  39. yield break;
  40. }
  41. Texture2D renderedPageTexture = renderPromise.Result;
  42. ((RectTransform)m_RawImage.transform).sizeDelta = new Vector2(renderedPageTexture.width, renderedPageTexture.height);
  43. m_RawImage.texture = renderedPageTexture;
  44. }
  45. }
  46. }