PDFRenderer.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.IO;
  3. using Android.Content;
  4. using Android.Graphics;
  5. using Android.Util;
  6. using Com.Shockwave.Pdfium;
  7. using Syncfusion.SfPdfViewer.XForms;
  8. using Syncfusion.SfPdfViewer.XForms.Droid;
  9. [assembly: Xamarin.Forms.Dependency(typeof(PRS.Mobile.Droid.PDFRenderer))]
  10. namespace PRS.Mobile.Droid
  11. {
  12. public class PDFRenderer : ICustomPdfRenderer, ICustomPdfRendererService
  13. {
  14. internal PdfiumCore m_pdfiumCore;
  15. internal PdfDocument m_pdfDocument;
  16. internal int m_pageCount;
  17. internal Bitmap.Config m_bitmapConfig;
  18. /// <summary>
  19. /// Gets or sets the total page count of the PDF document
  20. /// </summary>
  21. public int PageCount
  22. {
  23. get
  24. {
  25. return m_pageCount;
  26. }
  27. set
  28. {
  29. m_pageCount = value;
  30. }
  31. }
  32. /// <summary>
  33. /// Gets or sets Bitmap.Config to render the bitmap from PDF document
  34. /// </summary>
  35. public Bitmap.Config BitmapConfig
  36. {
  37. get
  38. {
  39. return m_bitmapConfig;
  40. }
  41. set
  42. {
  43. m_bitmapConfig = value;
  44. }
  45. }
  46. /// <summary>
  47. /// Gets the dependency object of ICustomPdfRenderer
  48. /// </summary>
  49. public object AlternatePdfRenderer
  50. {
  51. get
  52. {
  53. return this;
  54. }
  55. }
  56. /// <summary>
  57. /// Initializes the required object
  58. /// </summary>
  59. /// <param name="context">Context of the application</param>
  60. /// <param name="inputStream">PDF document stream</param>
  61. public void Initialize(Context context, Stream inputStream)
  62. {
  63. if (inputStream == null)
  64. {
  65. throw new System.NullReferenceException("object reference is not set to an instance: inputStream");
  66. }
  67. //Initializes the PdfiumCore instance
  68. m_pdfiumCore = new PdfiumCore(context);
  69. byte[] byteArray = ReadBytes(inputStream);
  70. if (m_pdfiumCore == null)
  71. {
  72. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  73. }
  74. //Creates the PdfDocument instance from the PDF byte array
  75. m_pdfDocument = m_pdfiumCore.NewDocument(byteArray);
  76. if (m_pdfDocument == null)
  77. {
  78. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  79. }
  80. if (m_bitmapConfig == null)
  81. {
  82. m_bitmapConfig = Bitmap.Config.Rgb565;
  83. }
  84. //Gets the total number of pages from the PDF document
  85. m_pageCount = m_pdfiumCore.GetPageCount(m_pdfDocument);
  86. }
  87. /// <summary>
  88. /// Converts stream to byte array to render the PDF document using Pdfium renderer
  89. /// </summary>
  90. /// <param name="inputStream">PDF document stream to convert into byte array</param>
  91. /// <returns>byte array of PDF document</returns>
  92. private static byte[] ReadBytes(Stream input)
  93. {
  94. if (input.CanSeek)
  95. {
  96. input.Position = 0;
  97. }
  98. byte[] buffer = new byte[16 * 1024];
  99. using (MemoryStream ms = new MemoryStream())
  100. {
  101. int read;
  102. while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  103. {
  104. ms.Write(buffer, 0, read);
  105. }
  106. return ms.ToArray();
  107. }
  108. }
  109. /// <summary>
  110. /// Renders the PDF page as bitmap with specified page index
  111. /// </summary>
  112. /// <param name="bitmap">Bitmap to draw the content of the PDF page </param>
  113. /// <param name="pageIndex">Render the bitmap of PDF page with the page index</param>
  114. /// <param name="pageWidth">Width of the page to draw on to the bitmap</param>
  115. /// <param name="pageHeight">Height of the page to draw on to the bitmap</param>
  116. public void Render(Bitmap bitmap, int pageIndex, int pageWidth, int pageHeight)
  117. => Render(bitmap, pageIndex, 0, 0, pageWidth, pageHeight);
  118. private int _pageIndex = int.MinValue;
  119. private bool bFirst = true;
  120. public void Render(Bitmap bitmap, int pageIndex, int x, int y, int width, int height)
  121. {
  122. if (bitmap == null)
  123. {
  124. throw new System.NullReferenceException("object reference is not set to an instance: bitmap");
  125. }
  126. if (m_pdfiumCore == null)
  127. {
  128. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  129. }
  130. else if (m_pdfDocument == null)
  131. {
  132. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  133. }
  134. else if (pageIndex < 0 && pageIndex > m_pageCount - 1)
  135. {
  136. throw new System.ArgumentOutOfRangeException("pageIndex", "Index was out of range. Must be non-negative and less than the size of the PageCount.");
  137. }
  138. else
  139. {
  140. if (bFirst)
  141. {
  142. //Sets the config of Bitmap format we required to render the PDF pages
  143. bFirst = false;
  144. bitmap.SetConfig(m_bitmapConfig);
  145. }
  146. //Opens the PDF page with the specified page index to render the page as bitmap
  147. if (_pageIndex != pageIndex)
  148. {
  149. _pageIndex = pageIndex;
  150. m_pdfiumCore.OpenPage(m_pdfDocument, pageIndex);
  151. }
  152. m_pdfiumCore.RenderPageBitmap(m_pdfDocument, bitmap, pageIndex, x, y, width, height);
  153. }
  154. }
  155. /// <summary>
  156. /// Gets the Page size of the PDF document with given page index
  157. /// </summary>
  158. /// <param name="pageIndex">Page index to the get page size</param>
  159. /// <returns>Size of the page of PDF document </returns>
  160. public Size GetPageSize(int pageIndex)
  161. {
  162. if (m_pdfiumCore == null)
  163. {
  164. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  165. }
  166. else if (m_pdfDocument == null)
  167. {
  168. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  169. }
  170. else if (pageIndex < 0 && pageIndex > m_pageCount - 1)
  171. {
  172. throw new System.ArgumentOutOfRangeException("pageIndex", "Index was out of range. Must be non-negative and less than the size of the PageCount.");
  173. }
  174. else
  175. {
  176. //opens the PDF page with specified index to get the Size of a Page
  177. m_pdfiumCore.OpenPage(m_pdfDocument, pageIndex);
  178. int pageHeight = m_pdfiumCore.GetPageHeightPoint(m_pdfDocument, pageIndex);
  179. int pageWidth = m_pdfiumCore.GetPageWidthPoint(m_pdfDocument, pageIndex);
  180. return new Size(pageWidth, pageHeight);
  181. }
  182. }
  183. /// <summary>
  184. /// Closes the initialized object to release memory
  185. /// </summary>
  186. public void Close()
  187. {
  188. //Closes the created PdfDocument instance
  189. m_pdfiumCore.CloseDocument(m_pdfDocument);
  190. //Disposes PdfiumCore instance
  191. m_pdfiumCore.Dispose();
  192. }
  193. }
  194. }