Add V2 area markup and object editing enhancements
This commit is contained in:
@@ -206,6 +206,8 @@ class PdfDocument:
|
||||
page_index: int,
|
||||
text: str,
|
||||
point: tuple[float, float] | None = None,
|
||||
font_size: int = 14,
|
||||
text_color: tuple[float, float, float] = (0, 0, 0),
|
||||
) -> AnnotationInfo:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
@@ -218,9 +220,9 @@ class PdfDocument:
|
||||
annot = page.add_freetext_annot(
|
||||
rect,
|
||||
text,
|
||||
fontsize=14,
|
||||
fontsize=max(8, min(72, int(font_size))),
|
||||
fontname="china-s",
|
||||
text_color=(0, 0, 0),
|
||||
text_color=text_color,
|
||||
border_width=0,
|
||||
)
|
||||
annot.set_info(content=text)
|
||||
@@ -234,6 +236,56 @@ class PdfDocument:
|
||||
rect=self._rect_tuple(annot.rect),
|
||||
)
|
||||
|
||||
def add_area_highlight(
|
||||
self,
|
||||
page_index: int,
|
||||
rect: tuple[float, float, float, float],
|
||||
color: tuple[float, float, float] = (1.0, 0.92, 0.2),
|
||||
opacity: float = 0.35,
|
||||
) -> AnnotationInfo:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
annot = page.add_rect_annot(self._normalized_rect(rect))
|
||||
annot.set_colors(stroke=color, fill=color)
|
||||
annot.set_border(width=0)
|
||||
annot.set_opacity(max(0.1, min(1.0, float(opacity))))
|
||||
annot.set_info(content="区域高亮")
|
||||
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 add_area_line_mark(
|
||||
self,
|
||||
page_index: int,
|
||||
rect: tuple[float, float, float, float],
|
||||
strikeout: bool = False,
|
||||
color: tuple[float, float, float] = (0.9, 0.1, 0.1),
|
||||
border_width: float = 2,
|
||||
opacity: float = 1.0,
|
||||
) -> AnnotationInfo:
|
||||
self.snapshot()
|
||||
page = self._page(page_index)
|
||||
area = self._normalized_rect(rect)
|
||||
y = area.y0 + area.height * (0.55 if strikeout else 0.88)
|
||||
annot = page.add_line_annot(fitz.Point(area.x0, y), fitz.Point(area.x1, y))
|
||||
self._apply_annotation_style(annot, color, border_width, opacity)
|
||||
annot.set_info(content="区域删除线" if strikeout else "区域下划线")
|
||||
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_text_stamp(
|
||||
self,
|
||||
page_index: int,
|
||||
@@ -656,6 +708,61 @@ class PdfDocument:
|
||||
annot = annot.next
|
||||
return False
|
||||
|
||||
def duplicate_annotation_info(
|
||||
self,
|
||||
annotation: AnnotationInfo,
|
||||
target_page_index: int,
|
||||
offset: tuple[float, float] = (18, 18),
|
||||
) -> AnnotationInfo | None:
|
||||
if not annotation.rect:
|
||||
return None
|
||||
dx, dy = offset
|
||||
rect = fitz.Rect(*annotation.rect) + (dx, dy, dx, dy)
|
||||
content = annotation.content or ""
|
||||
annot_type = annotation.annotation_type.lower()
|
||||
self.snapshot()
|
||||
page = self._page(target_page_index)
|
||||
|
||||
if annot_type == "freetext":
|
||||
duplicate = page.add_freetext_annot(
|
||||
rect,
|
||||
content,
|
||||
fontsize=14,
|
||||
fontname="china-s",
|
||||
text_color=(0, 0, 0),
|
||||
border_width=0,
|
||||
)
|
||||
elif annot_type == "text":
|
||||
duplicate = page.add_text_annot(rect.tl, content)
|
||||
elif annot_type in {"square", "rect"}:
|
||||
duplicate = page.add_rect_annot(rect)
|
||||
duplicate.set_colors(stroke=(0.1, 0.35, 0.85))
|
||||
duplicate.set_border(width=2)
|
||||
elif annot_type in {"circle", "ellipse"}:
|
||||
duplicate = page.add_circle_annot(rect)
|
||||
duplicate.set_colors(stroke=(0.1, 0.35, 0.85))
|
||||
duplicate.set_border(width=2)
|
||||
elif annot_type == "line":
|
||||
start = fitz.Point(rect.x0, rect.y0 + rect.height / 2)
|
||||
end = fitz.Point(rect.x1, rect.y0 + rect.height / 2)
|
||||
duplicate = page.add_line_annot(start, end)
|
||||
duplicate.set_colors(stroke=(0.1, 0.35, 0.85))
|
||||
duplicate.set_border(width=2)
|
||||
else:
|
||||
return None
|
||||
|
||||
if content:
|
||||
duplicate.set_info(content=content)
|
||||
duplicate.update()
|
||||
self.mark_changed()
|
||||
return AnnotationInfo(
|
||||
page_index=target_page_index,
|
||||
xref=duplicate.xref,
|
||||
annotation_type=duplicate.type[1],
|
||||
content=content,
|
||||
rect=self._rect_tuple(duplicate.rect),
|
||||
)
|
||||
|
||||
def delete_annotation(self, page_index: int, xref: int) -> bool:
|
||||
page = self._page(page_index)
|
||||
annot = page.first_annot
|
||||
@@ -915,6 +1022,16 @@ class PdfDocument:
|
||||
y0 = max(36, page.rect.height * 0.16)
|
||||
return fitz.Rect(x0, y0, x0 + width, y0 + height)
|
||||
|
||||
@staticmethod
|
||||
def _normalized_rect(rect: tuple[float, float, float, float]) -> fitz.Rect:
|
||||
x0, y0, x1, y1 = rect
|
||||
normalized = fitz.Rect(float(x0), float(y0), float(x1), float(y1)).normalize()
|
||||
if normalized.width < 2:
|
||||
normalized.x1 = normalized.x0 + 2
|
||||
if normalized.height < 2:
|
||||
normalized.y1 = normalized.y0 + 2
|
||||
return normalized
|
||||
|
||||
@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))
|
||||
|
||||
Reference in New Issue
Block a user