Skip to content

Convert all text's color in PDF to black while ensuring text is selectable #1640

Discussion options

You must be logged in to vote

Here is a better version, which only makes text completely black, other stuff will be a light gray. So the last page is readable again.

import fitz

skips = (b"k", b"K", b"rg", b"RG", b"sc", b"SC", b"scn", b"SCN", b"gs", b"cs")
doc = fitz.open("Demo.pdf")
for page in doc:
    page.clean_contents()
    xref = page.get_contents()[0]
    lines = page.read_contents().splitlines()
    for i in range(len(lines)):
        if lines[i].endswith(skips):
            lines[i] = b""
            continue
        if lines[i] == b"q":
            lines[i] = b"q 0.9 g 0.9 G"
        elif lines[i] == b"BT":
            lines[i] = b"BT 0 g 0 G"
        elif lines[i] == b"ET":
            lines[i] = b"ET 0.9…

Replies: 8 comments 24 replies

Comment options

You must be logged in to vote
5 replies
@qwertynik
Comment options

@JorjMcKie
Comment options

@qwertynik
Comment options

@JorjMcKie
Comment options

@qwertynik
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
9 replies
@JorjMcKie
Comment options

@qwertynik
Comment options

@JorjMcKie
Comment options

@qwertynik
Comment options

@qwertynik
Comment options

Answer selected by qwertynik
Comment options

You must be logged in to vote
1 reply
@qwertynik
Comment options

Comment options

You must be logged in to vote
1 reply
@qwertynik
Comment options

Comment options

You must be logged in to vote
4 replies
@JorjMcKie
Comment options

@qwertynik
Comment options

@JorjMcKie
Comment options

@qwertynik
Comment options

Comment options

You must be logged in to vote
3 replies
@qwertynik
Comment options

@JorjMcKie
Comment options

@qwertynik
Comment options

Comment options

You must be logged in to vote
1 reply
@qwertynik
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants