Я пишу скрипт Python для изменения цвета текста и границ встроенных аннотаций, вставленных с помощью okular в PDF-документ.
Этот скрипт вместо изменения только текста и границ кажется измененным вся аннотация красного цвета (вместо этого отображается полностью заполненный красный прямоугольник).
Поддерживается ли это вообще? Я понимаю, насколько сложен формат PDF, но надеялся, что это простое изменение не потребует ничего необычного.
#!/usr/bin/env python3
import sys
import pikepdf
from pikepdf import Name
import re
def change_annotation_font_and_border_to_red(input_path):
output_path = input_path.replace(".pdf", "-output.pdf")
# Open the PDF file
with pikepdf.open(input_path) as pdf:
for page_number, page in enumerate(pdf.pages, start=1):
# Check if the page has annotations
if "/Annots" in page:
print(f"Page {page_number} contains annotations:")
# Iterate over each annotation in the /Annots array
for annot in page["/Annots"]:
print(f" - Annotation detected: {annot}")
# Set the border color to red (RGB)
annot[Name("/C")] = pikepdf.Array([1, 0, 0]) # RGB for red border
# Update the default appearance string (/DA) to set text color to red
if "/DA" in annot:
da_str = str(annot[Name("/DA")])
print(f" Original /DA: {da_str}")
# Remove any existing color settings in the DA string
da_str = re.sub(r"(\d+\.?\d*\s){1,3}[rg]", "", da_str)
# Append red color setting for text color
da_str = da_str.strip() + " 1 0 0 rg"
annot[Name("/DA")] = da_str
print(f" Updated /DA: {annot[Name('/DA')]}")
# Remove the appearance stream to apply changes if present
if "/AP" in annot:
del annot[Name("/AP")]
print(" Removed /AP to force appearance regeneration")
# Save the modified PDF with '-output.pdf' suffix
pdf.save(output_path)
print(f"Font and border color changed to red. Saved as: {output_path}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python change_annotation_font_and_border_to_red.py ")
else:
input_path = sys.argv[1]
change_annotation_font_and_border_to_red(input_path)
Подробнее здесь: https://stackoverflow.com/questions/791 ... notation-i
Как изменить цвет границы и шрифта во встроенной текстовой/произвольной текстовой аннотации в документе PDF? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение