mupdfwrap_test.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. public class HelloWorld
  2. {
  3. public static void Main(string[] args)
  4. {
  5. System.Console.WriteLine("MuPDF C# test starting.");
  6. // Check FZ_ENABLE_FB2.
  7. System.Console.WriteLine("FZ_VERSION=" + mupdf.mupdf.FZ_VERSION);
  8. System.Console.WriteLine("FZ_ENABLE_FB2=" + mupdf.mupdf.FZ_ENABLE_FB2);
  9. // Check we can load a document.
  10. mupdf.FzDocument document = new mupdf.FzDocument("zlib.3.pdf");
  11. System.Console.WriteLine("document: " + document);
  12. System.Console.WriteLine("num chapters: " + document.fz_count_chapters());
  13. mupdf.FzPage page = document.fz_load_page(0);
  14. mupdf.FzRect rect = page.fz_bound_page();
  15. System.Console.WriteLine("rect: " + rect);
  16. if ("" + rect != rect.to_string())
  17. {
  18. throw new System.Exception("rect ToString() is broken: '" + rect + "' != '" + rect.to_string() + "'");
  19. }
  20. // Test conversion to html using docx device.
  21. var buffer = page.fz_new_buffer_from_page_with_format(
  22. "docx",
  23. "html",
  24. new mupdf.FzMatrix(1, 0, 0, 1, 0, 0),
  25. new mupdf.FzCookie()
  26. );
  27. var data = buffer.fz_buffer_extract();
  28. var s = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
  29. if (s.Length < 100) {
  30. throw new System.Exception("HTML text is too short");
  31. }
  32. System.Console.WriteLine("s=" + s);
  33. // Check that previous buffer.fz_buffer_extract() cleared the buffer.
  34. data = buffer.fz_buffer_extract();
  35. s = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
  36. if (s.Length > 0) {
  37. throw new System.Exception("Buffer was not cleared.");
  38. }
  39. // Check we can create pixmap from page.
  40. var pixmap = page.fz_new_pixmap_from_page_contents(
  41. new mupdf.FzMatrix(1, 0, 0, 1, 0, 0),
  42. new mupdf.FzColorspace(mupdf.FzColorspace.Fixed.Fixed_RGB),
  43. 0 /*alpha*/
  44. );
  45. // Check returned tuple from bitmap.fz_bitmap_details().
  46. var w = 100;
  47. var h = 200;
  48. var n = 4;
  49. var xres = 300;
  50. var yres = 300;
  51. var bitmap = new mupdf.FzBitmap(w, h, n, xres, yres);
  52. (var w2, var h2, var n2, var stride) = bitmap.fz_bitmap_details();
  53. System.Console.WriteLine("bitmap.fz_bitmap_details() returned:"
  54. + " " + w2 + " " + h2 + " " + n2 + " " + stride);
  55. if (w2 != w || h2 != h) {
  56. throw new System.Exception("Unexpected tuple values from bitmap.fz_bitmap_details().");
  57. }
  58. // Check we get exception from MuPDF. As of 2024-06-14 this exception
  59. // does not contain the original MuPDF exception text, it just says
  60. // "External component has thrown an exception."
  61. //
  62. // We only do this test if not running on Mono - Mono fails
  63. // with:
  64. //
  65. // > terminate called after throwing an instance of
  66. // > 'mupdf::FzErrorSystem'
  67. //
  68. if (System.Type.GetType("Mono.Runtime") == null)
  69. {
  70. int received_exception = 0;
  71. try
  72. {
  73. mupdf.FzDocument document2 = new mupdf.FzDocument("does not exist");
  74. System.Console.WriteLine("*** Error, did not get expected exception.");
  75. }
  76. catch (System.Exception e)
  77. {
  78. received_exception = 1;
  79. System.Console.WriteLine("Received exception: " + e.Message);
  80. }
  81. if (received_exception != 1)
  82. {
  83. throw new System.Exception("Did not receive expected exception");
  84. }
  85. }
  86. else
  87. {
  88. System.Console.WriteLine("Not checking handling of exceptions because running on Mono.");
  89. }
  90. // Check we can make MuPDF open filename containing 4-byte
  91. // unicode character. This file will have been created by
  92. // `scripts/wrap/__main__.py --test-csharp`.
  93. byte[] text_utf8 =
  94. {
  95. 0xf0,
  96. 0x90,
  97. 0x90,
  98. 0xb7,
  99. };
  100. string testfile2 = "zlib.3.pdf"
  101. + System.Text.Encoding.UTF8.GetString(text_utf8)
  102. + ".pdf";
  103. System.Console.WriteLine("Opening testfile2: " + testfile2);
  104. try
  105. {
  106. mupdf.FzDocument document2 = new mupdf.FzDocument(testfile2);
  107. System.Console.WriteLine("new mupdf.FzDocument succeeded");
  108. }
  109. catch (System.Exception e)
  110. {
  111. System.Console.WriteLine("Exception: " + e.Message);
  112. throw new System.Exception("Failed to open filename containing 4-byte unicode character");
  113. }
  114. System.Console.WriteLine("MuPDF C# test finished.");
  115. }
  116. }