CustomPdfRenderer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using Android.Content;
  2. using Android.Graphics;
  3. using PdfViewerSample.Droid;
  4. using Syncfusion.SfPdfViewer.XForms;
  5. using Syncfusion.SfPdfViewer.XForms.Droid;
  6. using System.IO;
  7. using Com.Shockwave.Pdfium;
  8. using Xamarin.Forms;
  9. [assembly: Dependency(typeof(CustomPdfRenderer))]
  10. namespace PdfViewerSample.Droid
  11. {
  12. internal class CustomPdfRenderer : 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. {
  118. if (bitmap == null)
  119. {
  120. throw new System.NullReferenceException("object reference is not set to an instance: bitmap");
  121. }
  122. if (m_pdfiumCore == null)
  123. {
  124. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  125. }
  126. else if (m_pdfDocument == null)
  127. {
  128. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  129. }
  130. else if (pageIndex < 0 && pageIndex > m_pageCount - 1)
  131. {
  132. throw new System.ArgumentOutOfRangeException("pageIndex", "Index was out of range. Must be non-negative and less than the size of the PageCount.");
  133. }
  134. else
  135. {
  136. //Sets the config of Bitmap format we required to render the PDF pages
  137. bitmap.SetConfig(m_bitmapConfig);
  138. //Opens the PDF page with the specified page index to render the page as bitmap
  139. m_pdfiumCore.OpenPage(m_pdfDocument, pageIndex);
  140. m_pdfiumCore.RenderPageBitmap(m_pdfDocument, bitmap, pageIndex, 0, 0, pageWidth, pageHeight);
  141. }
  142. }
  143. /// <summary>
  144. /// Gets the Page size of the PDF document with given page index
  145. /// </summary>
  146. /// <param name="pageIndex">Page index to the get page size</param>
  147. /// <returns>Size of the page of PDF document </returns>
  148. public Android.Util.Size GetPageSize(int pageIndex)
  149. {
  150. if (m_pdfiumCore == null)
  151. {
  152. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  153. }
  154. else if (m_pdfDocument == null)
  155. {
  156. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  157. }
  158. else if (pageIndex < 0 && pageIndex > m_pageCount - 1)
  159. {
  160. throw new System.ArgumentOutOfRangeException("pageIndex", "Index was out of range. Must be non-negative and less than the size of the PageCount.");
  161. }
  162. else
  163. {
  164. //opens the PDF page with specified index to get the Size of a Page
  165. m_pdfiumCore.OpenPage(m_pdfDocument, pageIndex);
  166. int pageHeight = m_pdfiumCore.GetPageHeightPoint(m_pdfDocument, pageIndex);
  167. int pageWidth = m_pdfiumCore.GetPageWidthPoint(m_pdfDocument, pageIndex);
  168. return new Android.Util.Size(pageWidth, pageHeight);
  169. }
  170. }
  171. /// <summary>
  172. /// Closes the initialized object to release memory
  173. /// </summary>
  174. public void Close()
  175. {
  176. //Closes the created PdfDocument instance
  177. m_pdfiumCore.CloseDocument(m_pdfDocument);
  178. //Disposes PdfiumCore instance
  179. m_pdfiumCore.Dispose();
  180. }
  181. public void Render(Bitmap destination, int pageIndex, int x, int y, int width, int height)
  182. {
  183. if (destination == null)
  184. {
  185. throw new System.NullReferenceException("object reference is not set to an instance: bitmap");
  186. }
  187. if (m_pdfiumCore == null)
  188. {
  189. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfiumCore");
  190. }
  191. else if (m_pdfDocument == null)
  192. {
  193. throw new System.NullReferenceException("object reference is not set to an instance: m_pdfDocument");
  194. }
  195. else if (pageIndex < 0 && pageIndex > m_pageCount - 1)
  196. {
  197. throw new System.ArgumentOutOfRangeException("pageIndex", "Index was out of range. Must be non-negative and less than the size of the PageCount.");
  198. }
  199. else
  200. {
  201. //Sets the config of Bitmap format we required to render the PDF pages
  202. destination.SetConfig(m_bitmapConfig);
  203. //Opens the PDF page with the specified page index to render the page as bitmap
  204. m_pdfiumCore.OpenPage(m_pdfDocument, pageIndex);
  205. m_pdfiumCore.RenderPageBitmap(m_pdfDocument, destination, pageIndex, x, y, width, height);
  206. }
  207. }
  208. }
  209. }