Comments (4)
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.
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.
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.
Close issue for lack of reaction.
from pymupdf.
Related Issues (20)
- Updating Annotations HOT 1
- False result when finding bounding boxes for lines in blocks. HOT 6
- For what reason IRect exists? HOT 1
- MuPDF error: argument error: not a dict (string) HOT 3
- Get image inside table's cell
- `'width'` in `Page.get_drawings()` returns width equal as 0 HOT 2
- trouble in page.find_tables HOT 1
- Garbled extraction for Amazon Sustainability Report HOT 6
- This pdf would cause stack overflow exception, HOT 3
- ImportError: DLL load failed while importing _extra: The specified module could not be found. HOT 1
- Story.fit_width() has a weird line HOT 2
- The position box obtained through the get_text() method is inaccurate HOT 5
- ObjStm compression and PDF linearization doesn't work together HOT 3
- SMask of Image is not detected HOT 8
- insert_htmlbox does not print out characters if there is a mix of non English characters and English characters HOT 5
- find_tables OOM HOT 1
- page.get_pixmap() fails due to `fitz.mupdf.FzErrorLimit: code=5: too many nested graphics states` HOT 5
- No OCR support: TESSDATA_PREFIX not set HOT 1
- apply_redactions moves graphics HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pymupdf.