Add adjustable raster placement layer

This commit is contained in:
2026-06-29 18:12:05 +08:00
parent a23b2cc366
commit 3a448d16cb
6 changed files with 206 additions and 28 deletions
+42 -13
View File
@@ -214,14 +214,29 @@ class PdfDocument:
image_path: str | Path,
point: tuple[float, float] | None = None,
) -> None:
self.snapshot()
self.insert_image_rect(page_index, image_path, self.image_rect(page_index, point))
def image_rect(
self,
page_index: int,
point: tuple[float, float] | None = None,
) -> tuple[float, float, float, float]:
page = self._page(page_index)
page_rect = page.rect
width = page_rect.width * 0.35
height = width * 0.75
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)
return (x0, y0, x0 + width, y0 + height)
def insert_image_rect(
self,
page_index: int,
image_path: str | Path,
rect: tuple[float, float, float, float],
) -> None:
self.snapshot()
page = self._page(page_index)
page.insert_image(fitz.Rect(*rect), filename=str(image_path), keep_proportion=True)
self.mark_changed()
def add_signature(
@@ -230,7 +245,13 @@ class PdfDocument:
image_path: str | Path,
point: tuple[float, float] | None = None,
) -> None:
self.snapshot()
self.insert_image_rect(page_index, image_path, self.signature_rect(page_index, point))
def signature_rect(
self,
page_index: int,
point: tuple[float, float] | None = None,
) -> tuple[float, float, float, float]:
page = self._page(page_index)
page_rect = page.rect
width = page_rect.width * 0.28
@@ -238,15 +259,23 @@ class PdfDocument:
margin = 54
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)
return (x0, y0, x0 + width, y0 + height)
return (
page_rect.width - width - margin,
page_rect.height - height - margin,
page_rect.width - margin,
page_rect.height - margin,
)
def add_signature_rect(
self,
page_index: int,
image_path: str | Path,
rect: tuple[float, float, float, float],
) -> None:
self.snapshot()
page = self._page(page_index)
page.insert_image(fitz.Rect(*rect), filename=str(image_path), keep_proportion=True)
self.mark_changed()
def add_rectangle(self, page_index: int) -> None: