Add click placement for PDF objects

This commit is contained in:
2026-06-29 16:44:45 +08:00
parent c770dd5498
commit bf9e8fb7cd
6 changed files with 139 additions and 17 deletions
+32 -12
View File
@@ -181,36 +181,56 @@ class PdfDocument:
annot.update()
self.mark_changed()
def add_text(self, page_index: int, text: str) -> None:
def add_text(
self,
page_index: int,
text: str,
point: tuple[float, float] | None = None,
) -> None:
self.snapshot()
page = self._page(page_index)
point = fitz.Point(72, 96)
page.insert_text(point, text, fontsize=14, fontname="china-s", color=(0, 0, 0))
text_point = fitz.Point(*(point or (72, 96)))
page.insert_text(text_point, text, fontsize=14, fontname="china-s", color=(0, 0, 0))
self.mark_changed()
def insert_image(self, page_index: int, image_path: str | Path) -> None:
def insert_image(
self,
page_index: int,
image_path: str | Path,
point: tuple[float, float] | None = None,
) -> None:
self.snapshot()
page = self._page(page_index)
page_rect = page.rect
width = page_rect.width * 0.35
height = width * 0.75
rect = fitz.Rect(72, 120, 72 + width, 120 + height)
x0, y0 = point or (72, 120)
rect = fitz.Rect(x0, y0, x0 + width, y0 + height)
page.insert_image(rect, filename=str(image_path), keep_proportion=True)
self.mark_changed()
def add_signature(self, page_index: int, image_path: str | Path) -> None:
def add_signature(
self,
page_index: int,
image_path: str | Path,
point: tuple[float, float] | None = None,
) -> None:
self.snapshot()
page = self._page(page_index)
page_rect = page.rect
width = page_rect.width * 0.28
height = width * 0.42
margin = 54
rect = fitz.Rect(
page_rect.width - width - margin,
page_rect.height - height - margin,
page_rect.width - margin,
page_rect.height - margin,
)
if point:
x0, y0 = point
rect = fitz.Rect(x0, y0, x0 + width, y0 + height)
else:
rect = fitz.Rect(
page_rect.width - width - margin,
page_rect.height - height - margin,
page_rect.width - margin,
page_rect.height - margin,
)
page.insert_image(rect, filename=str(image_path), keep_proportion=True)
self.mark_changed()