Repository navigation
Expand file tree
/
Copy path06_watermark_pdf.py
More file actions
65 lines (51 loc) · 1.99 KB
/
Copy path06_watermark_pdf.py
File metadata and controls
65 lines (51 loc) · 1.99 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
64
65
"""Sample 06 — Add a diagonal text watermark to every page of a PDF
(Python.NET wrapper).
If no input PDF is supplied, the sample first asks the worker to render a
tiny `Hello.pdf` and then watermarks that. Pass an explicit --input to
watermark a PDF you already have on disk.
"""
import argparse
import re
from pathlib import Path
import document_sdk # type: ignore[import-not-found]
def _slug_label(label: str) -> str:
"""Make a label safe to use as part of a file name.
Strips path separators and any character that is not alphanumeric,
dash, underscore, or dot. Falls back to ``"label"`` if the result is
empty.
"""
slug = re.sub(r"[^A-Za-z0-9._-]+", "-", label).strip("-")
return slug or "label"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--input",
type=Path,
help="Path to an existing .pdf file. If omitted, a tiny one is created for you.",
)
parser.add_argument(
"--out-dir",
type=Path,
default=Path(__file__).resolve().parent.parent / "output",
)
parser.add_argument("--label", default="CONFIDENTIAL", help="Watermark text.")
return parser.parse_args()
def main() -> None:
args = parse_args()
out_dir: Path = args.out_dir.resolve()
out_dir.mkdir(parents=True, exist_ok=True)
service = document_sdk.load_service()
if args.input is None:
source = out_dir / "Hello.pdf"
# Match the wording used by sample 01 for the pythonnet track.
service.create_pdf("Hello from Python.NET and DocIO!", source)
print("Generated sample PDF:", source)
else:
source = args.input.resolve()
if not source.is_file():
raise SystemExit(f"Input PDF not found: {source}")
watermarked = out_dir / f"watermarked-{_slug_label(args.label)}.pdf"
service.watermark_pdf(source, watermarked, args.label)
print("Saved:", watermarked)
if __name__ == "__main__":
main()