-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf-add-filename-as-annotation.py
More file actions
executable file
·63 lines (56 loc) · 1.7 KB
/
pdf-add-filename-as-annotation.py
File metadata and controls
executable file
·63 lines (56 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# "pdf-annotate >=0.12.0",
# ]
# ///
import sys, pathlib, subprocess
# https://github.com/plangrid/pdf-annotate
from pdf_annotate import PdfAnnotator, Location, Appearance, Metadata
from pdf_annotate.config.metadata import Flags
for fn in sys.argv[1:]:
input_file = pathlib.Path(fn)
assert input_file.exists(), input_file
annotation_text = input_file.stem
if input_file.suffix == ".pdf":
output_file = input_file.parent / (input_file.stem + ".annotated.pdf")
assert not output_file.exists(), output_file
else:
output_file = input_file.parent / (input_file.stem + ".pdf")
assert not output_file.exists(), output_file
subprocess.run(
[
# Imagemagick
"magick", "-units", "PixelsPerInch", "-density", "72",
# Formerly ["convert", "-units", "PixelsPerInch", "-density", "72"]
input_file.as_posix(),
output_file.as_posix(),
],
shell=False,
check=True,
)
input_file = output_file
top, left = 1 * 72, 6.5 * 72
width, height = 3 * 72, 1 * 72
annotator = PdfAnnotator(input_file.as_posix())
annotator.add_annotation(
annotation_type="text",
location=Location(x1=left, y1=top, x2=(left + width), y2=(top + height), page=0),
appearance=Appearance(
content=annotation_text,
fill=(0, 150/255.0, 255/255.0),
font_size=24,
stroke_width=1,
line_spacing=1,
text_align="left",
text_baseline="bottom",
# fonts="Helvetica"
),
metadata=Metadata(
flags=Flags.Print | Flags.LockedContents | Flags.Locked | Flags.ReadOnly
)
)
annotator.write(output_file.as_posix())