MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / write_text

Method write_text

src/__init__.py:17072–17204  ·  view source on GitHub ↗

Write the text to a PDF page having the TextWriter's page size. Args: page: a PDF page having same size. color: override text color. opacity: override transparency. overlay: put in foreground or background. morph: tuple(Point, Matr

(self, page, color=None, opacity=-1, overlay=1, morph=None, matrix=None, render_mode=0, oc=0)

Source from the content-addressed store, hash-verified

17070 return new_lines # return non-written lines
17071
17072 def write_text(self, page, color=None, opacity=-1, overlay=1, morph=None, matrix=None, render_mode=0, oc=0):
17073 """Write the text to a PDF page having the TextWriter's page size.
17074
17075 Args:
17076 page: a PDF page having same size.
17077 color: override text color.
17078 opacity: override transparency.
17079 overlay: put in foreground or background.
17080 morph: tuple(Point, Matrix), apply a matrix with a fixpoint.
17081 matrix: Matrix to be used instead of 'morph' argument.
17082 render_mode: (int) PDF render mode operator 'Tr'.
17083 """
17084 CheckParent(page)
17085 if abs(self.rect - page.rect) > 1e-3:
17086 raise ValueError("incompatible page rect")
17087 if morph is not None:
17088 if (type(morph) not in (tuple, list)
17089 or type(morph[0]) is not Point
17090 or type(morph[1]) is not Matrix
17091 ):
17092 raise ValueError("morph must be (Point, Matrix) or None")
17093 if matrix is not None and morph is not None:
17094 raise ValueError("only one of matrix, morph is allowed")
17095 if getattr(opacity, "__float__", None) is None or opacity == -1:
17096 opacity = self.opacity
17097 if color is None:
17098 color = self.color
17099
17100 if 1:
17101 pdfpage = page._pdf_page()
17102 alpha = 1
17103 if opacity >= 0 and opacity < 1:
17104 alpha = opacity
17105 ncol = 1
17106 dev_color = [0, 0, 0, 0]
17107 if color:
17108 ncol, dev_color = JM_color_FromSequence(color)
17109 if ncol == 3:
17110 colorspace = mupdf.fz_device_rgb()
17111 elif ncol == 4:
17112 colorspace = mupdf.fz_device_cmyk()
17113 else:
17114 colorspace = mupdf.fz_device_gray()
17115
17116 resources = mupdf.pdf_new_dict(pdfpage.doc(), 5)
17117 contents = mupdf.fz_new_buffer(1024)
17118 dev = mupdf.pdf_new_pdf_device( pdfpage.doc(), mupdf.FzMatrix(), resources, contents)
17119 #log( '=== {dev_color!r=}')
17120 mupdf.fz_fill_text(
17121 dev,
17122 self.this,
17123 mupdf.FzMatrix(),
17124 colorspace,
17125 dev_color,
17126 alpha,
17127 mupdf.FzColorParams(mupdf.fz_default_color_params),
17128 )
17129 mupdf.fz_close_device( dev)

Callers 6

test_textbox1Function · 0.95
test_textbox3Function · 0.95
test_textbox4Function · 0.95
test_4141Function · 0.95
test_techwriter_appendFunction · 0.95
bbox_countFunction · 0.95

Calls 13

CheckParentFunction · 0.85
JM_color_FromSequenceFunction · 0.85
JM_merge_resourcesFunction · 0.85
JM_EscapeStrFromBufferFunction · 0.85
_format_gFunction · 0.85
MatrixClass · 0.85
JM_TUPLEFunction · 0.85
repair_mono_fontFunction · 0.85
_pdf_pageMethod · 0.80
_get_optional_contentMethod · 0.80
pretranslateMethod · 0.80
_insert_contentsMethod · 0.80

Tested by 6

test_textbox1Function · 0.76
test_textbox3Function · 0.76
test_textbox4Function · 0.76
test_4141Function · 0.76
test_techwriter_appendFunction · 0.76
bbox_countFunction · 0.76