textpage.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. # SPDX-FileCopyrightText: 2026 geisserml <geisserml@gmail.com>
  2. # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
  3. __all__ = ("PdfTextPage", "PdfTextSearcher")
  4. import ctypes
  5. import logging
  6. import pypdfium2.raw as pdfium_c
  7. import pypdfium2.internal as pdfium_i
  8. from pypdfium2._helpers.misc import PdfiumError
  9. from pypdfium2._helpers.pageobjects import PdfTextObj
  10. c_double = ctypes.c_double
  11. logger = logging.getLogger(__name__)
  12. class PdfTextPage (pdfium_i.AutoCloseable):
  13. """
  14. Text page helper class.
  15. Note:
  16. PDFium's text APIs generally output CRLF (``\\r\\n``) style line breaks.
  17. This may be undesirable or confusing in some situations, e.g. when processing the output with an (unaware) parser on the command line.
  18. If this is an issue, replace ``\\r\\n`` with just ``\\n``.
  19. Hint:
  20. (py)pdfium itself does not implement layout analysis, such as detecting words/lines/paragraphs.
  21. However, there may be third-party extensions for this job, e.g.: https://github.com/VikParuchuri/pdftext
  22. Attributes:
  23. raw (FPDF_TEXTPAGE):
  24. The underlying PDFium textpage handle.
  25. page (PdfPage):
  26. Reference to the page this textpage belongs to.
  27. """
  28. def __init__(self, raw, page):
  29. self.raw = raw
  30. self.page = page
  31. super().__init__(pdfium_c.FPDFText_ClosePage)
  32. @property
  33. def parent(self): # AutoCloseable hook
  34. return self.page
  35. def get_text_bounded(self, left=None, bottom=None, right=None, top=None, errors="ignore"):
  36. """
  37. Extract text from given boundaries, in PDF canvas units.
  38. If a boundary value is None, it defaults to the corresponding value of :meth:`.PdfPage.get_bbox`.
  39. Parameters:
  40. errors (str): Error treatment when decoding the data (see :meth:`bytes.decode`).
  41. Returns:
  42. str: The text on the page area in question, or an empty string if no text was found.
  43. """
  44. bbox = self.page.get_bbox()
  45. if left is None:
  46. left = bbox[0]
  47. if bottom is None:
  48. bottom = bbox[1]
  49. if right is None:
  50. right = bbox[2]
  51. if top is None:
  52. top = bbox[3]
  53. args = (self, left, top, right, bottom)
  54. n_chars = pdfium_c.FPDFText_GetBoundedText(*args, None, 0)
  55. if n_chars <= 0:
  56. return ""
  57. buffer = ctypes.create_string_buffer(n_chars * 2)
  58. buffer_ptr = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_ushort))
  59. pdfium_c.FPDFText_GetBoundedText(*args, buffer_ptr, n_chars)
  60. return buffer.raw.decode("utf-16-le", errors=errors)
  61. def _get_active_text_range(self, c_start, c_end, l_passive=0, r_passive=0):
  62. if c_start > c_end:
  63. return 0 # no active chars in range
  64. t_start = pdfium_c.FPDFText_GetTextIndexFromCharIndex(self, c_start)
  65. if t_start == -1:
  66. return self._get_active_text_range(c_start+1, c_end, l_passive+1, r_passive)
  67. t_end = pdfium_c.FPDFText_GetTextIndexFromCharIndex(self, c_end)
  68. if t_end == -1:
  69. return self._get_active_text_range(c_start, c_end-1, l_passive, r_passive+1)
  70. return t_start, t_end, l_passive, r_passive
  71. def get_text_range(self, index=0, count=-1, errors="ignore"):
  72. """
  73. Extract text from a given range.
  74. Parameters:
  75. index (int): Index of the first char to include.
  76. count (int): Number of chars to cover, relative to the internal char list. Defaults to -1 for all remaining chars after *index*.
  77. errors (str): Error handling when decoding the data (see :meth:`bytes.decode`).
  78. Returns:
  79. str: The text in the range in question, or an empty string if no text was found.
  80. Warning:
  81. This method is limited to UCS-2, whereas :meth:`.get_text_bounded` provides full Unicode support.
  82. Note:
  83. * The returned text's length does not have to match *count*, even if it will for most PDFs.
  84. This is because the underlying API may exclude/insert chars compared to the internal list, although rare in practice.
  85. This means, if the char at ``i`` is excluded, ``get_text_range(i, 2)[1]`` will raise an index error.
  86. Pdfium provides raw APIs ``FPDFText_GetTextIndexFromCharIndex()`` / ``FPDFText_GetCharIndexFromTextIndex()`` to translate between the two views and identify excluded/inserted chars.
  87. * In case of leading/trailing excluded characters, pypdfium2 modifies *index* and *count* accordingly to prevent pdfium from unexpectedly reading beyond ``range(index, index+count)``.
  88. """
  89. if count == -1:
  90. count = self.count_chars() - index
  91. # https://github.com/pypdfium2-team/pypdfium2/issues/261
  92. # https://crbug.com/pdfium/2079
  93. active_range = self._get_active_text_range(index, index+count-1)
  94. if active_range == 0:
  95. return ""
  96. # NOTE since we have converted indices from char to text, they will shift accordingly for inserted/excluded chars, so this will calculate the exact output count
  97. t_start, t_end, l_passive, r_passive = active_range
  98. index += l_passive
  99. count -= l_passive + r_passive
  100. in_count = t_end+2 - t_start # including NUL terminator
  101. buffer = ctypes.create_string_buffer(in_count * 2)
  102. buffer_ptr = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_ushort))
  103. out_count = pdfium_c.FPDFText_GetText(self, index, count, buffer_ptr)
  104. assert in_count >= out_count, f"Buffer too small: {in_count} vs {out_count}"
  105. return buffer.raw[:(out_count-1)*2].decode("utf-16-le", errors=errors)
  106. def count_chars(self):
  107. """
  108. Returns:
  109. int: The number of characters on the text page.
  110. """
  111. n_chars = pdfium_c.FPDFText_CountChars(self)
  112. if n_chars == -1:
  113. raise PdfiumError("Failed to get character count.")
  114. return n_chars
  115. def count_rects(self, index=0, count=-1):
  116. """
  117. Parameters:
  118. index (int): Start character index.
  119. count (int): Character count to consider (defaults to -1 for all remaining).
  120. Returns:
  121. int: The number of text rectangles in the given character range.
  122. """
  123. n_rects = pdfium_c.FPDFText_CountRects(self, index, count)
  124. if n_rects == -1:
  125. raise PdfiumError("Failed to count rectangles.")
  126. return n_rects
  127. def get_index(self, x, y, x_tol, y_tol):
  128. """
  129. Get the index of a character by position.
  130. Parameters:
  131. x (float): Horizontal position (in PDF canvas units).
  132. y (float): Vertical position.
  133. x_tol (float): Horizontal tolerance.
  134. y_tol (float): Vertical tolerance.
  135. Returns:
  136. int | None: The index of the character at or nearby the point (x, y).
  137. May be None if there is no character. If an internal error occurred, an exception will be raised.
  138. """
  139. index = pdfium_c.FPDFText_GetCharIndexAtPos(self, x, y, x_tol, y_tol)
  140. if index == -1:
  141. return None
  142. elif index == -3:
  143. raise PdfiumError("An error occurred on attempt to get char index by pos.")
  144. assert index >= 0, "Negative return is not permitted (unhandled error code?)"
  145. return index
  146. def get_charbox(self, index, loose=False):
  147. """
  148. Get the bounding box of a single character.
  149. Parameters:
  150. index (int):
  151. Index of the character to work with, in the page's character array.
  152. loose (bool):
  153. Get a more comprehensive box covering the entire font bounds, as opposed to the default tight box specific to the one character.
  154. Returns:
  155. Float values for left, bottom, right and top in PDF canvas units.
  156. """
  157. if loose:
  158. rect = pdfium_c.FS_RECTF()
  159. ok = pdfium_c.FPDFText_GetLooseCharBox(self, index, rect)
  160. l, b, r, t = rect.left, rect.bottom, rect.right, rect.top
  161. else:
  162. l, b, r, t = c_double(), c_double(), c_double(), c_double()
  163. ok = pdfium_c.FPDFText_GetCharBox(self, index, l, r, b, t) # yes, lrbt!
  164. l, b, r, t = l.value, b.value, r.value, t.value
  165. if not ok:
  166. raise PdfiumError("Failed to get charbox.")
  167. return l, b, r, t
  168. def get_rect(self, index):
  169. """
  170. Get the bounding box of a text rectangle at the given index.
  171. Attention:
  172. :meth:`.count_rects` must be called once with default params before subsequent :meth:`.get_rect` calls for this function to work.
  173. Returns:
  174. Float values for left, bottom, right and top in PDF canvas units.
  175. """
  176. l, b, r, t = c_double(), c_double(), c_double(), c_double()
  177. ok = pdfium_c.FPDFText_GetRect(self, index, l, t, r, b) # yes, ltrb!
  178. if not ok:
  179. raise PdfiumError("Failed to get rectangle. (Make sure count_rects() was called with default params once before subsequent get_rect() calls.)")
  180. return (l.value, b.value, r.value, t.value)
  181. def get_textobj(self, index):
  182. """
  183. Returns:
  184. PdfTextObj | None: A handle to the textobject that includes the char at *index*, or None if it could not be resolved (e.g. escape character).
  185. Tip:
  186. Textobjects can also be obtained through :meth:`.PdfPage.get_objects`.
  187. """
  188. raw_obj = pdfium_c.FPDFText_GetTextObject(self, index)
  189. if not raw_obj:
  190. return None
  191. # The raw_obj is _not_ owned by the caller, and the textpage must remain alive while the textobject lives.
  192. return PdfTextObj(raw_obj, textpage=self)
  193. def search(self, text, index=0, match_case=False, match_whole_word=False, consecutive=False, flags=0):
  194. """
  195. Locate text on the page.
  196. Parameters:
  197. text (str):
  198. The string to search for.
  199. index (int):
  200. Character index at which to start searching.
  201. match_case (bool):
  202. If True, the search will be case-specific (upper and lower letters treated as different characters).
  203. match_whole_word (bool):
  204. If True, substring occurrences will be ignored (e. g. `cat` would not match `category`).
  205. consecutive (bool):
  206. If False (the default), :meth:`.search` will skip past the current match to look for the next match.
  207. If True, parts of the previous match may be caught again (e. g. searching for `aa` in `aaaa` would match 3 rather than 2 times).
  208. flags (int):
  209. Passthrough of raw pdfium searching flags. Note that you may want to use the boolean options instead.
  210. Returns:
  211. PdfTextSearcher: A helper object to search text.
  212. """
  213. if len(text) == 0:
  214. raise ValueError("Text length must be greater than 0.")
  215. if match_case:
  216. flags |= pdfium_c.FPDF_MATCHCASE
  217. if match_whole_word:
  218. flags |= pdfium_c.FPDF_MATCHWHOLEWORD
  219. if consecutive:
  220. flags |= pdfium_c.FPDF_CONSECUTIVE
  221. enc_text = (text + "\x00").encode("utf-16-le")
  222. enc_text_ptr = ctypes.cast(enc_text, ctypes.POINTER(ctypes.c_ushort))
  223. raw_searcher = pdfium_c.FPDFText_FindStart(self, enc_text_ptr, flags, index)
  224. searcher = PdfTextSearcher(raw_searcher, self)
  225. self._add_kid(searcher)
  226. return searcher
  227. class PdfTextSearcher (pdfium_i.AutoCloseable):
  228. """
  229. Text searcher helper class.
  230. Attributes:
  231. raw (FPDF_SCHHANDLE): The underlying PDFium searcher handle.
  232. textpage (PdfTextPage): Reference to the textpage this searcher belongs to.
  233. """
  234. def __init__(self, raw, textpage):
  235. self.raw = raw
  236. self.textpage = textpage
  237. super().__init__(pdfium_c.FPDFText_FindClose)
  238. @property
  239. def parent(self): # AutoCloseable hook
  240. return self.textpage
  241. def _get_occurrence(self, find_func):
  242. ok = find_func(self)
  243. if not ok:
  244. return None
  245. index = pdfium_c.FPDFText_GetSchResultIndex(self)
  246. count = pdfium_c.FPDFText_GetSchCount(self)
  247. return index, count
  248. def get_next(self):
  249. """
  250. Returns:
  251. (int, int) | None: Start character index and count of the next occurrence, or None if the last occurrence was passed.
  252. """
  253. return self._get_occurrence(pdfium_c.FPDFText_FindNext)
  254. def get_prev(self):
  255. """
  256. Returns:
  257. (int, int) | None: Start character index and count of the previous occurrence (i. e. the one before the last valid occurrence), or None if the last occurrence was passed.
  258. """
  259. return self._get_occurrence(pdfium_c.FPDFText_FindPrev)