mupdfwrap_gui.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Basic PDF viewer using MuPDF C# bindings.
  2. //
  3. public class MuPDFGui : System.Windows.Forms.Form
  4. {
  5. // We use static pixmap to ensure it isn't garbage-collected.
  6. mupdf.FzPixmap pixmap;
  7. private System.Windows.Forms.MainMenu menu;
  8. private System.Windows.Forms.MenuItem menu_item_file;
  9. /* Zooming works by incrementing self.zoom by +/- 1 then using
  10. magnification = 2**(self.zoom/self.zoom_multiple). */
  11. private int zoom_multiple = 4;
  12. private double zoom = 0;
  13. private int page_number = 0;
  14. mupdf.FzDocument document;
  15. mupdf.FzPage page;
  16. System.Drawing.Bitmap bitmap;
  17. System.Windows.Forms.PictureBox picture_box;
  18. // Need STAThread here otherwise OpenFileDialog hangs.
  19. [System.STAThread]
  20. public static void Main()
  21. {
  22. System.Windows.Forms.Application.Run(new MuPDFGui());
  23. }
  24. public MuPDFGui()
  25. {
  26. menu_item_file = new System.Windows.Forms.MenuItem("File",
  27. new System.Windows.Forms.MenuItem[]
  28. {
  29. new System.Windows.Forms.MenuItem("&Open...", new System.EventHandler(this.open)),
  30. new System.Windows.Forms.MenuItem("&Show html", new System.EventHandler(this.show_html)),
  31. new System.Windows.Forms.MenuItem("&Quit", new System.EventHandler(this.quit))
  32. }
  33. );
  34. menu = new System.Windows.Forms.MainMenu(new System.Windows.Forms.MenuItem [] {menu_item_file});
  35. this.Menu = menu;
  36. Resize += handle_resize;
  37. KeyDown += handle_key_down;
  38. this.picture_box = new System.Windows.Forms.PictureBox();
  39. this.picture_box.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  40. this.AutoScroll = true;
  41. Controls.Add(picture_box);
  42. this.open_file("zlib.3.pdf");
  43. }
  44. public void open(System.Object sender, System.EventArgs e)
  45. {
  46. var dialog = new System.Windows.Forms.OpenFileDialog();
  47. var result = dialog.ShowDialog();
  48. if (result == System.Windows.Forms.DialogResult.OK)
  49. {
  50. this.open_file(dialog.FileName);
  51. }
  52. }
  53. void open_file(string path)
  54. {
  55. try
  56. {
  57. this.document = new mupdf.FzDocument(path);
  58. }
  59. catch (System.Exception e)
  60. {
  61. System.Console.WriteLine("Failed to open: " + path + " because: " + e);
  62. return;
  63. }
  64. this.goto_page(0, 0);
  65. }
  66. public void show_html(System.Object sender, System.EventArgs e)
  67. {
  68. System.Console.WriteLine("ShowHtml() called");
  69. var buffer = this.page.fz_new_buffer_from_page_with_format(
  70. "docx",
  71. "html",
  72. new mupdf.FzMatrix(1, 0, 0, 1, 0, 0),
  73. new mupdf.FzCookie()
  74. );
  75. System.Console.WriteLine("buffer=" + buffer);
  76. var html_bytes = buffer.fz_buffer_extract();
  77. var html_string = System.Text.Encoding.UTF8.GetString(html_bytes, 0, html_bytes.Length);
  78. var web_browser = new System.Windows.Forms.WebBrowser();
  79. web_browser.DocumentText = html_string;
  80. web_browser.Show();
  81. }
  82. public void quit(System.Object sender, System.EventArgs e)
  83. {
  84. System.Console.WriteLine("Quit() called");
  85. System.Windows.Forms.Application.Exit();
  86. }
  87. // Shows page. If width and/or height are zero we use .Width and/or .Height.
  88. //
  89. // To preserve current page and/or zoom, use .page_number and/or .zoom.
  90. //
  91. public void goto_page(int page_number, double zoom)
  92. {
  93. if (page_number < 0 || page_number >= document.fz_count_pages())
  94. {
  95. return;
  96. }
  97. this.zoom = zoom;
  98. this.page_number = page_number;
  99. this.page = document.fz_load_page(page_number);
  100. var z = System.Math.Pow(2, this.zoom / this.zoom_multiple);
  101. /* For now we always use 'fit width' view semantics. */
  102. var page_rect = this.page.fz_bound_page();
  103. var vscroll_width = System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
  104. z *= (this.ClientSize.Width - vscroll_width) / (page_rect.x1 - page_rect.x0);
  105. if (System.Type.GetType("Mono.Runtime") != null)
  106. {
  107. /* Use pixmap data without copying. This does not work on
  108. Windows.
  109. It looks like it's important to use MuPDF Fixed_RGB with
  110. alpha=1, and C#'s Format32bppRgb. Other combinations,
  111. e.g. (Fixed_RGB with alpha=0) and Format24bppRgb, result in a
  112. blank display. */
  113. var stopwatch = new System.Diagnostics.Stopwatch();
  114. stopwatch.Reset();
  115. stopwatch.Start();
  116. this.pixmap = this.page.fz_new_pixmap_from_page_contents(
  117. new mupdf.FzMatrix((float) z, 0, 0, (float) z, 0, 0),
  118. new mupdf.FzColorspace(mupdf.FzColorspace.Fixed.Fixed_RGB),
  119. 1 /*alpha*/
  120. );
  121. stopwatch.Stop();
  122. var t_pixmap = stopwatch.Elapsed;
  123. stopwatch.Reset();
  124. stopwatch.Start();
  125. this.bitmap = new System.Drawing.Bitmap(
  126. this.pixmap.fz_pixmap_width(),
  127. this.pixmap.fz_pixmap_height(),
  128. this.pixmap.fz_pixmap_stride(),
  129. System.Drawing.Imaging.PixelFormat.Format32bppRgb,
  130. (System.IntPtr) this.pixmap.fz_pixmap_samples_int()
  131. );
  132. stopwatch.Stop();
  133. var t_bitmap = stopwatch.Elapsed;
  134. stopwatch.Reset();
  135. stopwatch.Start();
  136. // This is slow for large pixmaps/bitmaps.
  137. //check(pixmap, bitmap, 4);
  138. stopwatch.Stop();
  139. var t_check = stopwatch.Elapsed;
  140. /*System.Console.WriteLine(""
  141. + " t_pixmap=" + t_pixmap
  142. + " t_bitmap=" + t_bitmap
  143. + " t_check=" + t_check
  144. );*/
  145. }
  146. else
  147. {
  148. /* Copy pixmap's pixels into bitmap. This works on both Linux
  149. (Mono) and Windows.
  150. Unlike above, it seems that we need to use MuPDF Fixed_RGB with
  151. alpha=0, and C#'s Format32bppRgb. Other combinations give a
  152. blank display (possibly with alpha=0 for each pixel). */
  153. this.pixmap = this.page.fz_new_pixmap_from_page_contents(
  154. new mupdf.FzMatrix((float) z, 0, 0, (float) z, 0, 0),
  155. new mupdf.FzColorspace(mupdf.FzColorspace.Fixed.Fixed_RGB),
  156. 0 /*alpha*/
  157. );
  158. this.bitmap = new System.Drawing.Bitmap(
  159. this.pixmap.fz_pixmap_width(),
  160. this.pixmap.fz_pixmap_height(),
  161. System.Drawing.Imaging.PixelFormat.Format32bppRgb
  162. );
  163. long samples = pixmap.fz_pixmap_samples_int();
  164. int stride = pixmap.fz_pixmap_stride();
  165. for (int x=0; x<bitmap.Width; x+=1)
  166. {
  167. for (int y=0; y<bitmap.Height; y+=1)
  168. {
  169. unsafe
  170. {
  171. byte* sample = (byte*) samples + stride * y + 3 * x;
  172. var color = System.Drawing.Color.FromArgb(sample[0], sample[1], sample[2]);
  173. this.bitmap.SetPixel( x, y, color);
  174. }
  175. }
  176. }
  177. //check(pixmap, bitmap, 3);
  178. }
  179. this.picture_box.Image = this.bitmap;
  180. }
  181. private void handle_key_down(object sender, System.Windows.Forms.KeyEventArgs e)
  182. {
  183. //System.Console.WriteLine("HandleKeyDown: " + e.KeyCode);
  184. if (e.Shift && e.KeyCode == System.Windows.Forms.Keys.PageUp)
  185. {
  186. goto_page(this.page_number - 1, this.zoom);
  187. }
  188. else if (e.Shift && e.KeyCode == System.Windows.Forms.Keys.PageDown)
  189. {
  190. goto_page(this.page_number + 1, this.zoom);
  191. }
  192. else if (e.KeyCode == System.Windows.Forms.Keys.D0)
  193. {
  194. goto_page(this.page_number, 0);
  195. }
  196. else if (e.KeyCode == System.Windows.Forms.Keys.Add
  197. || e.KeyCode == System.Windows.Forms.Keys.Oemplus
  198. )
  199. {
  200. goto_page(this.page_number, this.zoom + 1);
  201. }
  202. else if (e.KeyCode == System.Windows.Forms.Keys.Subtract
  203. || e.KeyCode == System.Windows.Forms.Keys.OemMinus)
  204. {
  205. goto_page(this.page_number, this.zoom - 1);
  206. }
  207. }
  208. private void handle_resize(object sender, System.EventArgs e)
  209. {
  210. goto_page(page_number, zoom);
  211. }
  212. // Throws exception if pixmap and bitmap differ.
  213. void check(mupdf.FzPixmap pixmap, System.Drawing.Bitmap bitmap, int pixmap_bytes_per_pixel)
  214. {
  215. long samples = pixmap.fz_pixmap_samples_int();
  216. if (pixmap.fz_pixmap_width() != bitmap.Width || pixmap.fz_pixmap_height() != bitmap.Height)
  217. {
  218. throw new System.Exception("Inconsistent sizes:"
  219. + " pixmap=(" + pixmap.fz_pixmap_width() + " " + pixmap.fz_pixmap_height()
  220. + " bitmap=(" + bitmap.Width + " " + bitmap.Height
  221. );
  222. }
  223. int stride = pixmap.fz_pixmap_stride();
  224. for (int x=0; x<bitmap.Width; x+=1)
  225. {
  226. for (int y=0; y<bitmap.Height; y+=1)
  227. {
  228. unsafe
  229. {
  230. byte* sample = (byte*) samples + stride * y + pixmap_bytes_per_pixel * x;
  231. System.Drawing.Color color = bitmap.GetPixel( x, y);
  232. if (color.R != sample[0] || color.G != sample[1] || color.B != sample[2])
  233. {
  234. string pixmap_pixel_text = "";
  235. for (int i=0; i<pixmap_bytes_per_pixel; ++i)
  236. {
  237. if (i > 0) pixmap_pixel_text += " ";
  238. pixmap_pixel_text += sample[i];
  239. }
  240. throw new System.Exception("Pixels differ: (" + x + " " + y + "):"
  241. + " pixmap: (" + pixmap_pixel_text + ")"
  242. + " bitmap: " + color);
  243. }
  244. }
  245. }
  246. }
  247. }
  248. }