Giter VIP home page Giter VIP logo

Comments (4)

JorjMcKie avatar JorjMcKie commented on July 22, 2024

Inserting / Adding stuff to rotated pages can be confusing. For most methods in PyMuPDF you must pass rotated coordinates (for points, rectangles, ...) to get them in the right place.
I think this script does what you want:

import pymupdf as fitz  # PyMuPDF

RED = fitz.pdfcolor["red"]


def process_pdf(input_pdf_path, output_pdf_path):
    # Open the input PDF file
    document = fitz.open(input_pdf_path)

    # Iterate through each page
    for page in document:
        # 234 is half of the width of the page
        rect = fitz.Rect(0, 0, 234, 234)
        rot_rect = rect * page.derotation_matrix
        redact_annot = page.add_redact_annot(
            rot_rect, text=f"{page.number=}", text_color=RED
        )
        page.apply_redactions()

    document.ez_save(output_pdf_path)


if __name__ == "__main__":
    input_pdf_path = "input.pdf"  # Replace with the path to your input PDF
    output_pdf_path = "output.pdf"  # Replace with the path to your output PDF

    process_pdf(input_pdf_path, output_pdf_path)
    print(f"Processed PDF saved to {output_pdf_path}")

from pymupdf.

lyon-tonic avatar lyon-tonic commented on July 22, 2024

Thanks for responding!

This is part of the issue, but it is still not solving the issue of the redact_annot fill. The fill rectangle appears to be rendering separately from the redact_annot, and I'm not sure why.

The black fill rect is not showing up here.

import pymupdf as fitz  # PyMuPDF

RED = fitz.pdfcolor["red"]


def process_pdf(input_pdf_path, output_pdf_path):
    # Open the input PDF file
    document = fitz.open(input_pdf_path)

    # Iterate through each page
    for page in document:
        # 234 is half of the width of the page
        rect = fitz.Rect(0, 0, 234, 234)
        rot_rect = rect * page.derotation_matrix
        redact_annot = page.add_redact_annot(
            rot_rect, text=f"{page.number=}", text_color=RED
        )
        redact_annot.update(fill_color=(0, 0, 0))  # set fill color to black
        page.apply_redactions()

    document.ez_save(output_pdf_path)


if __name__ == "__main__":
    input_pdf_path = "input.pdf"  # Replace with the path to your input PDF
    output_pdf_path = "output.pdf"  # Replace with the path to your output PDF

    process_pdf(input_pdf_path, output_pdf_path)
    print(f"Processed PDF saved to {output_pdf_path}")

from pymupdf.

JorjMcKie avatar JorjMcKie commented on July 22, 2024

This file indeed does a few unexpected things!
Here is a complete solution that removes the page rotations.

import pymupdf as fitz  # PyMuPDF

RED = fitz.pdfcolor["red"]
BLACK = fitz.pdfcolor["black"]


def process_pdf(input_pdf_path, output_pdf_path):
    rect = fitz.Rect(0, 0, 234, 234)

    # Open the input PDF file
    src = fitz.open(input_pdf_path)
    doc = fitz.open()  # output file

    # Iterate through each page
    for src_page in src:
        # the output PDF will contain pages with rotation 0
        src_rect = src_page.rect
        w, h = src_rect.br
        src_rot = src_page.rotation
        src_page.set_rotation(0)
        # make output page having the visible dimension of the input
        page = doc.new_page(width=w, height=h)
        page.show_pdf_page(  # insert source page
            page.rect,
            src,
            src_page.number,
            rotate=-src_rot,  # reversed original rotation
        )
        
        # now we can redact in a worry-free manner
        redact_annot = page.add_redact_annot(
            rect, text=f"{page.number=}", text_color=RED, fill=BLACK
        )
        page.apply_redactions()

    doc.ez_save(output_pdf_path)


if __name__ == "__main__":
    input_pdf_path = "input.pdf"  # Replace with the path to your input PDF
    output_pdf_path = "output.pdf"  # Replace with the path to your output PDF

    process_pdf(input_pdf_path, output_pdf_path)
    print(f"Processed PDF saved to {output_pdf_path}")

from pymupdf.

JorjMcKie avatar JorjMcKie commented on July 22, 2024

Close issue for lack of reaction.

from pymupdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.