Skip to content

Moving Text/Rects in a PDF? #2433

Closed Answered by Tomblarom
Tomblarom asked this question in Looking for help
May 31, 2023 · 2 comments · 10 replies
Discussion options

You must be logged in to vote

I figured that the text I wanted to move is always at the same x position and that there are no other objects on this value. I took @JorjMcKie code from #1640 (comment) and rewrote it, so that it alters the positions:

import fitz

doc = fitz.open("pdf_in.pdf")
for page in doc:
    page.clean_contents()
    xref = page.get_contents()[0]
    lines = page.read_contents().splitlines()
    imgs = doc.get_page_images(page.number)
    
    for i in range(len(lines)):
        # Find images
        if lines[i].endswith(b"Do"): # OR if b"/Im" in lines[i]:
            for img in imgs:
                if lines[i].decode()[1:4] in img:
                    print(img)
        
        # Move text element

Replies: 2 comments 10 replies

Comment options

You must be logged in to vote
2 replies
@Tomblarom
Comment options

@JorjMcKie
Comment options

Comment options

You must be logged in to vote
8 replies
@Tomblarom
Comment options

@JorjMcKie
Comment options

@JorjMcKie
Comment options

@Tomblarom
Comment options

@JorjMcKie
Comment options

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