Skip to content

Commit bad764a

Browse files
Merge pull request #19 from datalogics-cgreen/pdfcloud-5464-watermark
PDFCLOUD-5554 Add client methods to watermark documents
2 parents 531efe7 + 7a26a78 commit bad764a

6 files changed

Lines changed: 2078 additions & 6 deletions

File tree

‎src/pdfrest/client.py‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
PdfFlattenFormsPayload,
102102
PdfFlattenLayersPayload,
103103
PdfFlattenTransparenciesPayload,
104+
PdfImageWatermarkPayload,
104105
PdfImportFormDataPayload,
105106
PdfInfoPayload,
106107
PdfLinearizePayload,
@@ -111,6 +112,7 @@
111112
PdfRestRawFileResponse,
112113
PdfRestrictPayload,
113114
PdfSplitPayload,
115+
PdfTextWatermarkPayload,
114116
PdfToExcelPayload,
115117
PdfToPdfaPayload,
116118
PdfToPdfxPayload,
@@ -154,12 +156,15 @@
154156
PdfRedactionInstruction,
155157
PdfRestriction,
156158
PdfRGBColor,
159+
PdfTextColor,
157160
PdfXType,
158161
PngColorModel,
159162
SummaryFormat,
160163
SummaryOutputFormat,
161164
TiffColorModel,
162165
TranslateOutputFormat,
166+
WatermarkHorizontalAlignment,
167+
WatermarkVerticalAlignment,
163168
)
164169

165170
__all__ = ("AsyncPdfRestClient", "PdfRestClient")
@@ -3615,6 +3620,106 @@ def convert_url_to_pdf(
36153620
timeout=timeout,
36163621
)
36173622

3623+
def watermark_pdf_with_text(
3624+
self,
3625+
file: PdfRestFile | Sequence[PdfRestFile],
3626+
*,
3627+
watermark_text: str,
3628+
output: str | None = None,
3629+
font: str | None = None,
3630+
text_size: int = 72,
3631+
text_color: PdfTextColor = (0, 0, 0),
3632+
opacity: float = 0.5,
3633+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
3634+
vertical_alignment: WatermarkVerticalAlignment = "center",
3635+
x: int = 0,
3636+
y: int = 0,
3637+
rotation: int = 0,
3638+
pages: PdfPageSelection | None = None,
3639+
behind_page: bool = False,
3640+
extra_query: Query | None = None,
3641+
extra_headers: AnyMapping | None = None,
3642+
extra_body: Body | None = None,
3643+
timeout: TimeoutTypes | None = None,
3644+
) -> PdfRestFileBasedResponse:
3645+
"""Apply a text watermark to a PDF."""
3646+
3647+
payload: dict[str, Any] = {
3648+
"files": file,
3649+
"watermark_text": watermark_text,
3650+
"output": output,
3651+
"font": font,
3652+
"text_size": text_size,
3653+
"text_color": text_color,
3654+
"opacity": opacity,
3655+
"horizontal_alignment": horizontal_alignment,
3656+
"vertical_alignment": vertical_alignment,
3657+
"x": x,
3658+
"y": y,
3659+
"rotation": rotation,
3660+
"pages": pages,
3661+
"behind_page": behind_page,
3662+
}
3663+
payload = {key: value for key, value in payload.items() if value is not None}
3664+
3665+
return self._post_file_operation(
3666+
endpoint="/watermarked-pdf",
3667+
payload=payload,
3668+
payload_model=PdfTextWatermarkPayload,
3669+
extra_query=extra_query,
3670+
extra_headers=extra_headers,
3671+
extra_body=extra_body,
3672+
timeout=timeout,
3673+
)
3674+
3675+
def watermark_pdf_with_image(
3676+
self,
3677+
file: PdfRestFile | Sequence[PdfRestFile],
3678+
*,
3679+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
3680+
output: str | None = None,
3681+
watermark_file_scale: float = 0.5,
3682+
opacity: float = 0.5,
3683+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
3684+
vertical_alignment: WatermarkVerticalAlignment = "center",
3685+
x: int = 0,
3686+
y: int = 0,
3687+
rotation: int = 0,
3688+
pages: PdfPageSelection | None = None,
3689+
behind_page: bool = False,
3690+
extra_query: Query | None = None,
3691+
extra_headers: AnyMapping | None = None,
3692+
extra_body: Body | None = None,
3693+
timeout: TimeoutTypes | None = None,
3694+
) -> PdfRestFileBasedResponse:
3695+
"""Apply an image watermark to a PDF."""
3696+
3697+
payload: dict[str, Any] = {
3698+
"files": file,
3699+
"watermark_file": watermark_file,
3700+
"output": output,
3701+
"watermark_file_scale": watermark_file_scale,
3702+
"opacity": opacity,
3703+
"horizontal_alignment": horizontal_alignment,
3704+
"vertical_alignment": vertical_alignment,
3705+
"x": x,
3706+
"y": y,
3707+
"rotation": rotation,
3708+
"pages": pages,
3709+
"behind_page": behind_page,
3710+
}
3711+
payload = {key: value for key, value in payload.items() if value is not None}
3712+
3713+
return self._post_file_operation(
3714+
endpoint="/watermarked-pdf",
3715+
payload=payload,
3716+
payload_model=PdfImageWatermarkPayload,
3717+
extra_query=extra_query,
3718+
extra_headers=extra_headers,
3719+
extra_body=extra_body,
3720+
timeout=timeout,
3721+
)
3722+
36183723
def convert_to_pdfa(
36193724
self,
36203725
file: PdfRestFile | Sequence[PdfRestFile],
@@ -5412,6 +5517,106 @@ async def convert_url_to_pdf(
54125517
timeout=timeout,
54135518
)
54145519

5520+
async def watermark_pdf_with_text(
5521+
self,
5522+
file: PdfRestFile | Sequence[PdfRestFile],
5523+
*,
5524+
watermark_text: str,
5525+
output: str | None = None,
5526+
font: str | None = None,
5527+
text_size: int = 72,
5528+
text_color: PdfTextColor = (0, 0, 0),
5529+
opacity: float = 0.5,
5530+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
5531+
vertical_alignment: WatermarkVerticalAlignment = "center",
5532+
x: int = 0,
5533+
y: int = 0,
5534+
rotation: int = 0,
5535+
pages: PdfPageSelection | None = None,
5536+
behind_page: bool = False,
5537+
extra_query: Query | None = None,
5538+
extra_headers: AnyMapping | None = None,
5539+
extra_body: Body | None = None,
5540+
timeout: TimeoutTypes | None = None,
5541+
) -> PdfRestFileBasedResponse:
5542+
"""Asynchronously apply a text watermark to a PDF."""
5543+
5544+
payload: dict[str, Any] = {
5545+
"files": file,
5546+
"watermark_text": watermark_text,
5547+
"output": output,
5548+
"font": font,
5549+
"text_size": text_size,
5550+
"text_color": text_color,
5551+
"opacity": opacity,
5552+
"horizontal_alignment": horizontal_alignment,
5553+
"vertical_alignment": vertical_alignment,
5554+
"x": x,
5555+
"y": y,
5556+
"rotation": rotation,
5557+
"pages": pages,
5558+
"behind_page": behind_page,
5559+
}
5560+
payload = {key: value for key, value in payload.items() if value is not None}
5561+
5562+
return await self._post_file_operation(
5563+
endpoint="/watermarked-pdf",
5564+
payload=payload,
5565+
payload_model=PdfTextWatermarkPayload,
5566+
extra_query=extra_query,
5567+
extra_headers=extra_headers,
5568+
extra_body=extra_body,
5569+
timeout=timeout,
5570+
)
5571+
5572+
async def watermark_pdf_with_image(
5573+
self,
5574+
file: PdfRestFile | Sequence[PdfRestFile],
5575+
*,
5576+
watermark_file: PdfRestFile | Sequence[PdfRestFile],
5577+
output: str | None = None,
5578+
watermark_file_scale: float = 0.5,
5579+
opacity: float = 0.5,
5580+
horizontal_alignment: WatermarkHorizontalAlignment = "center",
5581+
vertical_alignment: WatermarkVerticalAlignment = "center",
5582+
x: int = 0,
5583+
y: int = 0,
5584+
rotation: int = 0,
5585+
pages: PdfPageSelection | None = None,
5586+
behind_page: bool = False,
5587+
extra_query: Query | None = None,
5588+
extra_headers: AnyMapping | None = None,
5589+
extra_body: Body | None = None,
5590+
timeout: TimeoutTypes | None = None,
5591+
) -> PdfRestFileBasedResponse:
5592+
"""Asynchronously apply an image watermark to a PDF."""
5593+
5594+
payload: dict[str, Any] = {
5595+
"files": file,
5596+
"watermark_file": watermark_file,
5597+
"output": output,
5598+
"watermark_file_scale": watermark_file_scale,
5599+
"opacity": opacity,
5600+
"horizontal_alignment": horizontal_alignment,
5601+
"vertical_alignment": vertical_alignment,
5602+
"x": x,
5603+
"y": y,
5604+
"rotation": rotation,
5605+
"pages": pages,
5606+
"behind_page": behind_page,
5607+
}
5608+
payload = {key: value for key, value in payload.items() if value is not None}
5609+
5610+
return await self._post_file_operation(
5611+
endpoint="/watermarked-pdf",
5612+
payload=payload,
5613+
payload_model=PdfImageWatermarkPayload,
5614+
extra_query=extra_query,
5615+
extra_headers=extra_headers,
5616+
extra_body=extra_body,
5617+
timeout=timeout,
5618+
)
5619+
54155620
async def convert_to_pdfa(
54165621
self,
54175622
file: PdfRestFile | Sequence[PdfRestFile],

0 commit comments

Comments
 (0)