CustomPdfRenderer.cs 9.3 KB

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