Add annotation object selection editing

This commit is contained in:
2026-06-29 17:48:48 +08:00
parent bf9e8fb7cd
commit a23b2cc366
6 changed files with 310 additions and 13 deletions
+84 -2
View File
@@ -29,6 +29,7 @@ class AnnotationInfo:
xref: int
annotation_type: str
content: str
rect: tuple[float, float, float, float] | None = None
class EncryptedPdfError(RuntimeError):
@@ -189,8 +190,22 @@ class PdfDocument:
) -> None:
self.snapshot()
page = self._page(page_index)
text_point = fitz.Point(*(point or (72, 96)))
page.insert_text(text_point, text, fontsize=14, fontname="china-s", color=(0, 0, 0))
x0, y0 = point or (72, 96)
line_count = max(1, len(text.splitlines()))
max_line = max((len(line) for line in text.splitlines()), default=len(text))
width = max(160, min(page.rect.width - x0 - 24, max_line * 10 + 32))
height = max(42, line_count * 22 + 20)
rect = fitz.Rect(x0, y0, x0 + width, y0 + height)
annot = page.add_freetext_annot(
rect,
text,
fontsize=14,
fontname="china-s",
text_color=(0, 0, 0),
border_width=0,
)
annot.set_info(content=text)
annot.update()
self.mark_changed()
def insert_image(
@@ -443,11 +458,65 @@ class PdfDocument:
xref=annot.xref,
annotation_type=annot.type[1],
content=content,
rect=self._rect_tuple(annot.rect),
)
)
annot = annot.next
return items
def annotation_at_point(
self,
page_index: int,
point: tuple[float, float],
) -> AnnotationInfo | None:
page = self._page(page_index)
hit = fitz.Point(*point)
matches: list[AnnotationInfo] = []
annot = page.first_annot
while annot:
if annot.rect.contains(hit):
content = annot.info.get("content", "") if annot.info else ""
matches.append(
AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=content,
rect=self._rect_tuple(annot.rect),
)
)
annot = annot.next
return matches[-1] if matches else None
def annotation_rect(self, page_index: int, xref: int) -> tuple[float, float, float, float] | None:
annot = self._annotation(page_index, xref)
return self._rect_tuple(annot.rect) if annot else None
def update_annotation_rect(
self,
page_index: int,
xref: int,
rect: tuple[float, float, float, float],
) -> bool:
annot = self._annotation(page_index, xref)
if not annot:
return False
x0, y0, x1, y1 = rect
if x1 - x0 < 12 or y1 - y0 < 12:
return False
self.snapshot()
annot.set_rect(fitz.Rect(x0, y0, x1, y1))
annot.update()
self.mark_changed()
return True
def move_annotation(self, page_index: int, xref: int, dx: float, dy: float) -> bool:
annot = self._annotation(page_index, xref)
if not annot:
return False
rect = annot.rect + (dx, dy, dx, dy)
return self.update_annotation_rect(page_index, xref, self._rect_tuple(rect))
def delete_annotation(self, page_index: int, xref: int) -> bool:
page = self._page(page_index)
annot = page.first_annot
@@ -612,6 +681,15 @@ class PdfDocument:
raise IndexError("页码超出范围")
return self._doc.load_page(page_index)
def _annotation(self, page_index: int, xref: int):
page = self._page(page_index)
annot = page.first_annot
while annot:
if annot.xref == xref:
return annot
annot = annot.next
return None
def mark_changed(self) -> None:
self._revision += 1
self.clear_cache()
@@ -679,3 +757,7 @@ class PdfDocument:
x0 = max(36, page.rect.width * 0.12)
y0 = max(36, page.rect.height * 0.16)
return fitz.Rect(x0, y0, x0 + width, y0 + height)
@staticmethod
def _rect_tuple(rect: fitz.Rect) -> tuple[float, float, float, float]:
return (float(rect.x0), float(rect.y0), float(rect.x1), float(rect.y1))