Page.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "mupdf/fitz.h"
  2. #include "mupdf/pdf.h"
  3. #include "MuPDF.h"
  4. #ifndef __PAGE
  5. #define __PAGE
  6. #pragma once
  7. using namespace System;
  8. namespace MuPDF {
  9. public enum class PageBoxType {
  10. Media,
  11. Crop,
  12. Bleed,
  13. Trim,
  14. Art,
  15. Unknown
  16. };
  17. public ref class Page sealed : IDisposable, IEquatable<Page^> {
  18. public:
  19. property int PageNumber {
  20. int get() { return _pageNumber; }
  21. }
  22. /// <summary>
  23. /// Determine the page size in points, taking page rotation into account. The page size is taken to be the crop box if it exists (visible area after cropping), otherwise the media box will be used (possibly including printing marks).
  24. /// </summary>
  25. property Box Bound {
  26. Box get() { return pdf_bound_page(Context::Ptr, _pdfPage, FZ_CROP_BOX); }
  27. }
  28. property Box MediaBox {
  29. Box get() { return pdf_dict_get_rect(Context::Ptr, _pdfPage->obj, (pdf_obj*)PdfNames::MediaBox); }
  30. }
  31. property Box CropBox {
  32. Box get() { return pdf_dict_get_rect(Context::Ptr, _pdfPage->obj, (pdf_obj*)PdfNames::CropBox); }
  33. }
  34. property Box ArtBox {
  35. Box get() { return pdf_dict_get_rect(Context::Ptr, _pdfPage->obj, (pdf_obj*)PdfNames::ArtBox); }
  36. }
  37. property Box BleedBox {
  38. Box get() { return pdf_dict_get_rect(Context::Ptr, _pdfPage->obj, (pdf_obj*)PdfNames::BleedBox); }
  39. }
  40. property Box TrimBox {
  41. Box get() { return pdf_dict_get_rect(Context::Ptr, _pdfPage->obj, (pdf_obj*)PdfNames::TrimBox); }
  42. }
  43. property int Rotation {
  44. int get() {
  45. return pdf_dict_get_int(Context::Ptr, PagePtr, PDF_NAME(Rotate));
  46. }
  47. }
  48. property float UserUnit {
  49. float get() {
  50. return pdf_dict_get_real_default(Context::Ptr, PagePtr, PDF_NAME(UserUnit), 1);
  51. }
  52. }
  53. property bool HasTransparency {
  54. bool get() { return pdf_page_has_transparency(Context::Ptr, _pdfPage); }
  55. }
  56. property int AssociatedFileCount {
  57. int get() { return pdf_count_page_associated_files(Context::Ptr, _pdfPage); }
  58. }
  59. property PdfDictionary^ PdfObject {
  60. PdfDictionary^ get() {
  61. return gcnew PdfDictionary(_pdfPage->obj);
  62. }
  63. }
  64. property PdfDictionary^ Resources {
  65. PdfDictionary^ get() {
  66. auto r = pdf_page_resources(Context::Ptr, _pdfPage);
  67. return r ? gcnew PdfDictionary(r) : nullptr;
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the /Contents object, which can be a stream or an array, from the page dictionary
  72. /// </summary>
  73. property MuPDF::PdfObject^ Contents {
  74. MuPDF::PdfObject^ get() {
  75. auto c = pdf_page_contents(Context::Ptr, _pdfPage);
  76. return c ? MuPDF::PdfObject::Wrap(c) : nullptr;
  77. }
  78. }
  79. property MuPDF::TextPage^ TextPage {
  80. MuPDF::TextPage^ get() {
  81. return _textPage != nullptr ? _textPage : (_textPage = gcnew MuPDF::TextPage(fz_new_stext_page_from_page(Context::Ptr, _page, NULL)));
  82. }
  83. }
  84. Box BoundPageBox(PageBoxType boxType) {
  85. return pdf_bound_page(Context::Ptr, _pdfPage, (fz_box_type)boxType);
  86. }
  87. void SetPageBox(PageBoxType boxType, Box box) {
  88. pdf_set_page_box(Context::Ptr, _pdfPage, (fz_box_type)boxType, box);
  89. }
  90. PdfArray^ GetPageBox(PageBoxType boxType);
  91. array<Byte>^ GetContentBytes() {
  92. auto c = pdf_page_contents(Context::Ptr, _pdfPage);
  93. if (c) {
  94. auto s = gcnew Stream(pdf_open_contents_stream(Context::Ptr, _pdfPage->doc, c));
  95. return s->ReadAll();
  96. }
  97. return nullptr;
  98. }
  99. void Run(Device^ dev, Matrix ctm, Cookie^ cookie);
  100. void Run(Device^ dev, Cookie^ cookie) {
  101. Run(dev, Matrix::Identity, cookie);
  102. }
  103. void RunContents(Device^ dev, Matrix ctm, Cookie^ cookie);
  104. void RunContents(Device^ dev, Cookie^ cookie) {
  105. RunContents(dev, Matrix::Identity, cookie);
  106. }
  107. void RunAnnotations(Device^ dev, Matrix ctm, Cookie^ cookie);
  108. void RunAnnotations(Device^ dev, Cookie^ cookie) {
  109. RunAnnotations(dev, Matrix::Identity, cookie);
  110. }
  111. void RunWidgets(Device^ dev, Matrix ctm, Cookie^ cookie);
  112. void RunWidgets(Device^ dev, Cookie^ cookie) {
  113. RunWidgets(dev, Matrix::Identity, cookie);
  114. }
  115. void FlattenInheritablePageItems() {
  116. pdf_flatten_inheritable_page_items(Context::Ptr, _pdfPage->obj);
  117. }
  118. void RefreshPageCache() {
  119. pdf_sync_page(Context::Ptr, _pdfPage);
  120. RefreshTextPage();
  121. }
  122. void RefreshTextPage() {
  123. if (_textPage) {
  124. delete _textPage;
  125. _textPage = nullptr;
  126. }
  127. }
  128. Equatable(Page, _page)
  129. internal:
  130. Page(fz_page* page, int pageNumber) : _page(page) {
  131. _pdfPage = pdf_page_from_fz_page(Context::Ptr, page);
  132. _pageNumber = pageNumber;
  133. pdf_flatten_inheritable_page_items(Context::Ptr, _pdfPage->obj);
  134. };
  135. ~Page() {
  136. ReleaseHandle();
  137. }
  138. property pdf_obj* PagePtr {
  139. pdf_obj* get() { return _pdfPage->obj; }
  140. }
  141. protected:
  142. !Page() {
  143. ReleaseHandle();
  144. }
  145. private:
  146. fz_page* _page;
  147. pdf_page* _pdfPage;
  148. int _pageNumber;
  149. MuPDF::TextPage^ _textPage;
  150. array<Byte>^ _contents;
  151. void ReleaseHandle() {
  152. fz_drop_page(Context::Ptr, _page);
  153. if (_textPage) {
  154. delete _textPage;
  155. _textPage = nullptr;
  156. }
  157. _page = NULL;
  158. _pdfPage = NULL;
  159. }
  160. };
  161. };
  162. #endif // !__PAGE