Add Watermark To PDF
While converting Excel file to pdf, you may have requirements to add watermark to pdf file. The following examples shows how to add text and image watermark to pdf while rendering to pdf.
Add text watermark to PDF
You can easily add text watermark to pdf by specifying text and the corresponding font. Also, you can set alignment, offset, rotation, opacity, foreground/background, and scale to page in RenderingWatermark.
from aspose.cells import PaperSizeType, PdfSaveOptions, TextAlignmentType, Workbook | |
from aspose.cells.rendering import RenderingFont, RenderingWatermark | |
from aspose.pydrawing import Color | |
# prepare a workbook with 3 pages. | |
wb = Workbook() | |
wb.worksheets[0].cells.get("A1").put_value("Page1") | |
index = wb.worksheets.add() | |
wb.worksheets[index].cells.get("A1").put_value("Page2") | |
index = wb.worksheets.add() | |
wb.worksheets[index].cells.get("A1").put_value("Page3") | |
wb.worksheets[index].page_setup.paper_size = PaperSizeType.PAPER_A3 | |
# create a font for watermark, and specify bold, italic, color. | |
font = RenderingFont("Calibri", 68) | |
font.italic = True | |
font.bold = True | |
font.color = Color.blue | |
# create a watermark from text and the specified font. | |
watermark = RenderingWatermark("Watermark", font) | |
# specify horizontal and vertical alignment | |
watermark.h_alignment = TextAlignmentType.CENTER | |
watermark.v_alignment = TextAlignmentType.CENTER | |
# specify rotation | |
watermark.rotation = 30.0 | |
# specify opacity | |
watermark.opacity = 0.6 | |
# specify the scale to page(e.g. 100, 50) in percent. | |
watermark.scale_to_page_percent = 50 | |
# spcify watermark for rendering to pdf. | |
options = PdfSaveOptions() | |
options.watermark = watermark | |
wb.save("output_text_watermark.pdf", options) |
Add image watermark to PDF
You can add image watermark to pdf just by specifying image bytes of an image. Also, you can set alignment, offset, rotation, opacity, foreground/background, and scale to page in RenderingWatermark.
from aspose.cells import PaperSizeType, PdfSaveOptions, Workbook | |
from aspose.cells.rendering import RenderingWatermark | |
# prepare a workbook with 3 pages. | |
wb = Workbook() | |
wb.worksheets[0].cells.get("A1").put_value("Page1") | |
index = wb.worksheets.add() | |
wb.worksheets[index].cells.get("A1").put_value("Page2") | |
index = wb.worksheets.add() | |
wb.worksheets[index].cells.get("A1").put_value("Page3") | |
wb.worksheets[index].page_setup.paper_size = PaperSizeType.PAPER_A3 | |
# create a watermark from image(you need to prepare image bytes). | |
watermark = RenderingWatermark(imageBytes) | |
# specify offset to alignment. | |
watermark.offset_x = 100.0 | |
watermark.offset_y = 200.0 | |
# specify rotation | |
watermark.rotation = 30.0 | |
# specify watermark to background. | |
watermark.is_background = True | |
# specify opacity | |
watermark.opacity = 0.6 | |
# specify the scale to page(e.g. 100, 50) in percent. | |
watermark.scale_to_page_percent = 50 | |
# spcify watermark for rendering to pdf. | |
options = PdfSaveOptions() | |
options.watermark = watermark | |
wb.save("oputput_image_watermark.pdf", options) |