API_Usage.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Paroxe.PdfRenderer.Examples
  5. {
  6. public class API_Usage : MonoBehaviour
  7. {
  8. #if !UNITY_WEBGL
  9. private IEnumerator Start()
  10. {
  11. // UnityWebRequest or WWW can be use instead of PDFWebRequest.
  12. using (PDFWebRequest www = new PDFWebRequest("https://www.dropbox.com/s/tssavtnvaym2t6b/DocumentationEN.pdf?raw=1"))
  13. {
  14. www.SendWebRequest();
  15. Debug.Log("Downloading document...");
  16. yield return www;
  17. if (www == null || !string.IsNullOrEmpty(www.error) || !www.isDone)
  18. yield break;
  19. PDFDocument document = new PDFDocument(www.bytes, "");
  20. Debug.Log("Page count: " + document.GetPageCount());
  21. TextPageAPI(document);
  22. SearchAPI(document);
  23. BookmarkAPI(document);
  24. }
  25. }
  26. private void TextPageAPI(PDFDocument document)
  27. {
  28. Debug.Log("TEXTPAGE API-----------------------");
  29. PDFPage page = document.GetPage(1);
  30. Debug.Log("Page size: " + page.GetPageSize());
  31. PDFTextPage textPage = page.GetTextPage();
  32. int countChars = textPage.CountChars();
  33. Debug.Log("Page chars count: " + countChars);
  34. string str = textPage.GetText(0, countChars);
  35. Debug.Log("Page text: " + str);
  36. int rectCount = textPage.CountRects(0, countChars);
  37. Debug.Log("Rect count: " + rectCount);
  38. string boundedText = textPage.GetBoundedText(0, 0, page.GetPageSize().x, page.GetPageSize().y * 0.5f, 2000);
  39. Debug.Log("Bounded text: " + boundedText);
  40. }
  41. private void SearchAPI(PDFDocument document)
  42. {
  43. Debug.Log("SEARCH API-------------------------");
  44. IList<PDFSearchResult> results = document.GetPage(1).GetTextPage().Search("pdf");
  45. Debug.Log("Search results count: " + results.Count);
  46. Debug.Log("First result char index: " + results[0].StartIndex + " and chars count: " + results[0].Count);
  47. // Get all rects corresponding to the first search result
  48. int rectsCount = document.GetPage(1).GetTextPage().CountRects(results[0].StartIndex, results[0].Count);
  49. Debug.Log("Search result rects count: " + rectsCount);
  50. }
  51. private void BookmarkAPI(PDFDocument document)
  52. {
  53. Debug.Log("BOOKMARK API-----------------------");
  54. PDFBookmark rootBookmark = document.GetRootBookmark();
  55. OutputBookmarks(rootBookmark, 0);
  56. }
  57. private void OutputBookmarks(PDFBookmark bookmark, int indent)
  58. {
  59. string line = "";
  60. for (int i = 0; i < indent; ++i)
  61. line += " ";
  62. line += bookmark.GetTitle();
  63. Debug.Log(line);
  64. foreach (PDFBookmark child in bookmark.EnumerateChildrenBookmarks())
  65. {
  66. OutputBookmarks(child, indent + 1);
  67. }
  68. }
  69. #endif
  70. }
  71. }