Add page management and annotation tools
This commit is contained in:
@@ -96,6 +96,22 @@ class PdfDocument:
|
||||
annot.update()
|
||||
return len(rects)
|
||||
|
||||
def underline_text(self, page_index: int, text: str) -> int:
|
||||
page = self._page(page_index)
|
||||
rects = page.search_for(text.strip())
|
||||
for rect in rects:
|
||||
annot = page.add_underline_annot(rect)
|
||||
annot.update()
|
||||
return len(rects)
|
||||
|
||||
def strikeout_text(self, page_index: int, text: str) -> int:
|
||||
page = self._page(page_index)
|
||||
rects = page.search_for(text.strip())
|
||||
for rect in rects:
|
||||
annot = page.add_strikeout_annot(rect)
|
||||
annot.update()
|
||||
return len(rects)
|
||||
|
||||
def add_note(self, page_index: int, text: str) -> None:
|
||||
page = self._page(page_index)
|
||||
point = fitz.Point(72, 72)
|
||||
@@ -115,6 +131,106 @@ class PdfDocument:
|
||||
rect = fitz.Rect(72, 120, 72 + width, 120 + height)
|
||||
page.insert_image(rect, filename=str(image_path), keep_proportion=True)
|
||||
|
||||
def add_rectangle(self, page_index: int) -> None:
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
annot = page.add_rect_annot(rect)
|
||||
annot.set_colors(stroke=(0.9, 0.1, 0.1))
|
||||
annot.set_border(width=2)
|
||||
annot.update()
|
||||
|
||||
def add_circle(self, page_index: int) -> None:
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
annot = page.add_circle_annot(rect)
|
||||
annot.set_colors(stroke=(0.1, 0.3, 0.9))
|
||||
annot.set_border(width=2)
|
||||
annot.update()
|
||||
|
||||
def add_line(self, page_index: int, arrow: bool = False) -> None:
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
start = fitz.Point(rect.x0, rect.y0)
|
||||
end = fitz.Point(rect.x1, rect.y1)
|
||||
annot = page.add_line_annot(start, end)
|
||||
annot.set_colors(stroke=(0.1, 0.1, 0.1))
|
||||
annot.set_border(width=2)
|
||||
if arrow:
|
||||
annot.set_line_ends(fitz.PDF_ANNOT_LE_NONE, fitz.PDF_ANNOT_LE_CLOSED_ARROW)
|
||||
annot.update()
|
||||
|
||||
def add_cover_area(self, page_index: int) -> None:
|
||||
page = self._page(page_index)
|
||||
rect = self._default_rect(page)
|
||||
annot = page.add_rect_annot(rect)
|
||||
annot.set_colors(stroke=(1, 1, 1), fill=(1, 1, 1))
|
||||
annot.set_border(width=0)
|
||||
annot.update()
|
||||
|
||||
def insert_blank_page(self, after_page_index: int | None = None) -> int:
|
||||
if not self._doc:
|
||||
raise RuntimeError("没有打开 PDF")
|
||||
if self.page_count:
|
||||
source = self._page(after_page_index if after_page_index is not None else self.page_count - 1)
|
||||
width = source.rect.width
|
||||
height = source.rect.height
|
||||
else:
|
||||
width = 595
|
||||
height = 842
|
||||
insert_at = self.page_count if after_page_index is None else after_page_index + 1
|
||||
self._doc.new_page(pno=insert_at, width=width, height=height)
|
||||
return insert_at
|
||||
|
||||
def insert_pdf(self, source_path: str | Path, after_page_index: int | None = None) -> int:
|
||||
if not self._doc:
|
||||
raise RuntimeError("没有打开 PDF")
|
||||
insert_at = self.page_count if after_page_index is None else after_page_index + 1
|
||||
with fitz.open(source_path) as source:
|
||||
count = source.page_count
|
||||
self._doc.insert_pdf(source, start_at=insert_at)
|
||||
return count
|
||||
|
||||
def extract_pages(self, page_numbers: list[int], target_path: str | Path) -> None:
|
||||
if not self._doc:
|
||||
raise RuntimeError("没有打开 PDF")
|
||||
new_doc = fitz.open()
|
||||
try:
|
||||
for page_number in page_numbers:
|
||||
page_index = page_number - 1
|
||||
if page_index < 0 or page_index >= self.page_count:
|
||||
raise ValueError(f"页码超出范围:{page_number}")
|
||||
new_doc.insert_pdf(self._doc, from_page=page_index, to_page=page_index)
|
||||
new_doc.save(target_path, garbage=4, deflate=True)
|
||||
finally:
|
||||
new_doc.close()
|
||||
|
||||
def split_to_folder(self, target_folder: str | Path) -> None:
|
||||
if not self._doc:
|
||||
raise RuntimeError("没有打开 PDF")
|
||||
folder = Path(target_folder)
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
base_name = self.path.stem if self.path else "PDFReaderLite"
|
||||
for page_index in range(self.page_count):
|
||||
new_doc = fitz.open()
|
||||
try:
|
||||
new_doc.insert_pdf(self._doc, from_page=page_index, to_page=page_index)
|
||||
new_doc.save(folder / f"{base_name}_第{page_index + 1}页.pdf", garbage=4, deflate=True)
|
||||
finally:
|
||||
new_doc.close()
|
||||
|
||||
@staticmethod
|
||||
def merge_pdfs(source_paths: list[str | Path], target_path: str | Path) -> None:
|
||||
if not source_paths:
|
||||
raise ValueError("没有选择 PDF 文件")
|
||||
merged = fitz.open()
|
||||
try:
|
||||
for source_path in source_paths:
|
||||
with fitz.open(source_path) as source:
|
||||
merged.insert_pdf(source)
|
||||
merged.save(target_path, garbage=4, deflate=True)
|
||||
finally:
|
||||
merged.close()
|
||||
|
||||
def delete_page(self, page_index: int) -> None:
|
||||
if not self._doc:
|
||||
raise RuntimeError("没有打开 PDF")
|
||||
@@ -156,3 +272,11 @@ class PdfDocument:
|
||||
if page_index < 0 or page_index >= len(self._doc):
|
||||
raise IndexError("页码超出范围")
|
||||
return self._doc.load_page(page_index)
|
||||
|
||||
@staticmethod
|
||||
def _default_rect(page: fitz.Page) -> fitz.Rect:
|
||||
width = min(220, page.rect.width * 0.45)
|
||||
height = min(120, page.rect.height * 0.2)
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user