Remove hidden text from a PDF page. Args: cont_lines: list of lines with /Contents content. Should have status from after page.cleanContents(). Returns: List of /Contents lines from which hidden text has been removed. Notes:
(cont_lines)
| 4160 | xml_metadata: bool = True, |
| 4161 | ) -> None: |
| 4162 | def remove_hidden(cont_lines): |
| 4163 | """Remove hidden text from a PDF page. |
| 4164 | |
| 4165 | Args: |
| 4166 | cont_lines: list of lines with /Contents content. Should have status |
| 4167 | from after page.cleanContents(). |
| 4168 | |
| 4169 | Returns: |
| 4170 | List of /Contents lines from which hidden text has been removed. |
| 4171 | |
| 4172 | Notes: |
| 4173 | The input must have been created after the page's /Contents object(s) |
| 4174 | have been cleaned with page.cleanContents(). This ensures a standard |
| 4175 | formatting: one command per line, single spaces between operators. |
| 4176 | This allows for drastic simplification of this code. |
| 4177 | """ |
| 4178 | out_lines = [] # will return this |
| 4179 | in_text = False # indicate if within BT/ET object |
| 4180 | suppress = False # indicate text suppression active |
| 4181 | make_return = False |
| 4182 | for line in cont_lines: |
| 4183 | if line == b"BT": # start of text object |
| 4184 | in_text = True # switch on |
| 4185 | out_lines.append(line) # output it |
| 4186 | continue |
| 4187 | if line == b"ET": # end of text object |
| 4188 | in_text = False # switch off |
| 4189 | out_lines.append(line) # output it |
| 4190 | continue |
| 4191 | if line == b"3 Tr": # text suppression operator |
| 4192 | suppress = True # switch on |
| 4193 | make_return = True |
| 4194 | continue |
| 4195 | if line[-2:] == b"Tr" and line[0] != b"3": |
| 4196 | suppress = False # text rendering changed |
| 4197 | out_lines.append(line) |
| 4198 | continue |
| 4199 | if line == b"Q": # unstack command also switches off |
| 4200 | suppress = False |
| 4201 | out_lines.append(line) |
| 4202 | continue |
| 4203 | if suppress and in_text: # suppress hidden lines |
| 4204 | continue |
| 4205 | out_lines.append(line) |
| 4206 | if make_return: |
| 4207 | return out_lines |
| 4208 | else: |
| 4209 | return None |
| 4210 | |
| 4211 | if not doc.is_pdf: # only works for PDF |
| 4212 | raise ValueError("is no PDF") |