Skip to content

Commit f95f0e5

Browse files
committed
Add page recolor method
1 parent 2f7ef3d commit f95f0e5

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docs/document.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,14 +923,14 @@ For details on **embedded files** refer to Appendix 3.
923923

924924
.. method:: get_page_fonts(pno, full=False)
925925

926-
PDF only: Return a list of all fonts (directly or indirectly) referenced by the page.
926+
PDF only: Return a list of all fonts (directly or indirectly) referenced by the page object definition.
927927

928928
:arg int pno: page number, 0-based, `-∞ < pno < page_count`.
929929
:arg bool full: whether to also include the referencer's :data:`xref`. If *True*, the returned items are one entry longer. Use this option if you need to know, whether the page directly references the font. In this case the last entry is 0. If the font is referenced by an `/XObject` of the page, you will find its :data:`xref` here.
930930

931931
:rtype: list
932932

933-
:returns: a list of fonts referenced by this page. Each entry looks like
933+
:returns: a list of fonts referenced by the object definition of the page. Each entry looks like
934934

935935
**(xref, ext, type, basefont, name, encoding, referencer)**,
936936

@@ -958,7 +958,12 @@ For details on **embedded files** refer to Appendix 3.
958958

959959
.. note::
960960
* This list has no duplicate entries: the combination of :data:`xref`, *name* and *referencer* is unique.
961-
* In general, this is a superset of the fonts actually in use by this page. The PDF creator may e.g. have specified some global list, of which each page only makes partial use.
961+
* In general, this is a true superset of the fonts actually in use by this page. The PDF creator may e.g. have specified some global list, of which each page make only partial use.
962+
* Be aware that font names returned by some variants of :meth:`Page.get_text` (respectively :ref:`TextPage` methods) need not (exactly) equal the base font name shown here. Reasons for any differences include:
963+
964+
- This method always shows any subset prefixes (the pattern ``ABCDEF+``), whereas text extractions do not do this by default.
965+
- Text extractions use the base library to access the font name, which has a length cap of 31 bytes and generally interrogates the font file binary to access the name. Method ``get_page_fonts()`` however looks at the PDF definition source.
966+
- Text extractions work for all supported document types in exactly the same way -- not just for PDFs. Consequently they do not contain PDF-specifics.
962967

963968
.. method:: get_page_text(pno, output="text", flags=3, textpage=None, sort=False)
964969

docs/page.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ In a nutshell, this is what you can do with PyMuPDF:
106106
:meth:`Page.load_widget` PDF only: load a specific field
107107
:meth:`Page.load_links` return the first link on a page
108108
:meth:`Page.new_shape` PDF only: create a new :ref:`Shape`
109+
:meth:`Page.recolor` PDF only: change the colorspace of objects
109110
:meth:`Page.remove_rotation` PDF only: set page rotation to 0
110111
:meth:`Page.replace_image` PDF only: replace an image
111112
:meth:`Page.search_for` search for a string
@@ -1924,6 +1925,14 @@ In a nutshell, this is what you can do with PyMuPDF:
19241925

19251926
:arg int rotate: An integer specifying the required rotation in degrees. Must be an integer multiple of 90. Values will be converted to one of 0, 90, 180, 270.
19261927

1928+
.. method:: recolor(components=1)
1929+
1930+
PDF only: Change the colorspace components of all objects on page.
1931+
1932+
:arg int components: The desired count of color components. Must be one of 1, 3 or 4, which results in color space DeviceGray, DeviceRGB and DeviceCMYK respectively. The method affects text, images and vector graphics. For instance, with the default value 1, a page will be converted to gray-scale.
1933+
1934+
The changes made are **permanent** and cannot be reverted.
1935+
19271936
.. method:: remove_rotation()
19281937

19291938
PDF only: Set page rotation to 0 while maintaining appearance and page content.

src/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8694,6 +8694,19 @@ def annots(self, types=None):
86948694
annot._yielded=True
86958695
yield annot
86968696

8697+
def recolor(self, components=1):
8698+
"""Convert colorspaces of objects on the page.
8699+
8700+
Valid values are 1, 3 and 4.
8701+
"""
8702+
if components not in (1, 3, 4):
8703+
raise ValueError("components must be one of 1, 3, 4")
8704+
pdfdoc = _as_pdf_document(self.parent)
8705+
ropt = mupdf.pdf_recolor_options()
8706+
ropt.num_comp = components
8707+
ropts = mupdf.PdfRecolorOptions(ropt)
8708+
mupdf.pdf_recolor_page(pdfdoc, self.number, ropts)
8709+
86978710
@property
86988711
def artbox(self):
86998712
"""The ArtBox"""

0 commit comments

Comments
 (0)