Unify selection for newly created annotations

This commit is contained in:
2026-06-30 19:47:54 +08:00
parent a3ebad943b
commit b7be31bed9
4 changed files with 88 additions and 27 deletions
+59 -8
View File
@@ -193,13 +193,21 @@ class PdfDocument:
self.mark_changed()
return len(rects)
def add_note(self, page_index: int, text: str) -> None:
def add_note(self, page_index: int, text: str) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
point = fitz.Point(72, 72)
annot = page.add_text_annot(point, text)
annot.set_info(content=text)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=text,
rect=self._rect_tuple(annot.rect),
)
def add_text(
self,
@@ -291,7 +299,7 @@ class PdfDocument:
page_index: int,
text: str,
point: tuple[float, float] | None = None,
) -> None:
) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
x0, y0 = point or (96, 120)
@@ -312,6 +320,13 @@ class PdfDocument:
annot.set_info(content=text)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=text,
rect=self._rect_tuple(annot.rect),
)
def insert_image(
self,
@@ -389,7 +404,7 @@ class PdfDocument:
stroke_color: tuple[float, float, float] = (0.9, 0.1, 0.1),
border_width: float = 2,
opacity: float = 1.0,
) -> None:
) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
rect = self._default_rect(page)
@@ -397,6 +412,13 @@ class PdfDocument:
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=annot.info.get("content", "") if annot.info else "",
rect=self._rect_tuple(annot.rect),
)
def add_circle(
self,
@@ -404,7 +426,7 @@ class PdfDocument:
stroke_color: tuple[float, float, float] = (0.1, 0.3, 0.9),
border_width: float = 2,
opacity: float = 1.0,
) -> None:
) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
rect = self._default_rect(page)
@@ -412,6 +434,13 @@ class PdfDocument:
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=annot.info.get("content", "") if annot.info else "",
rect=self._rect_tuple(annot.rect),
)
def add_line(
self,
@@ -420,7 +449,7 @@ class PdfDocument:
stroke_color: tuple[float, float, float] = (0.1, 0.1, 0.1),
border_width: float = 2,
opacity: float = 1.0,
) -> None:
) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
rect = self._default_rect(page)
@@ -432,6 +461,13 @@ class PdfDocument:
annot.set_line_ends(fitz.PDF_ANNOT_LE_NONE, fitz.PDF_ANNOT_LE_CLOSED_ARROW)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=annot.info.get("content", "") if annot.info else "",
rect=self._rect_tuple(annot.rect),
)
def add_ink(
self,
@@ -440,9 +476,9 @@ class PdfDocument:
stroke_color: tuple[float, float, float] = (0.05, 0.05, 0.05),
border_width: float = 2,
opacity: float = 1.0,
) -> None:
) -> AnnotationInfo | None:
if len(points) < 2:
return
return None
self.snapshot()
page = self._page(page_index)
stroke_points = [(float(x), float(y)) for x, y in points]
@@ -450,6 +486,13 @@ class PdfDocument:
self._apply_annotation_style(annot, stroke_color, border_width, opacity)
annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content=annot.info.get("content", "") if annot.info else "",
rect=self._rect_tuple(annot.rect),
)
def add_cover_area(
self,
@@ -457,7 +500,7 @@ class PdfDocument:
fill_color: tuple[float, float, float] = (1, 1, 1),
opacity: float = 1.0,
text: str = "",
) -> None:
) -> AnnotationInfo:
self.snapshot()
page = self._page(page_index)
rect = self._default_rect(page)
@@ -465,6 +508,7 @@ class PdfDocument:
annot.set_colors(stroke=fill_color, fill=fill_color)
annot.set_border(width=0)
annot.set_opacity(max(0.1, min(1.0, float(opacity))))
annot.set_info(content="遮盖")
annot.update()
if text.strip():
text_annot = page.add_freetext_annot(
@@ -480,6 +524,13 @@ class PdfDocument:
text_annot.set_info(content=text.strip())
text_annot.update()
self.mark_changed()
return AnnotationInfo(
page_index=page_index,
xref=annot.xref,
annotation_type=annot.type[1],
content="遮盖",
rect=self._rect_tuple(annot.rect),
)
def insert_blank_page(self, after_page_index: int | None = None) -> int:
if not self._doc: