Add watermarks to word processing documents

Microsoft Word documents can be divided into multiple sections, and headers and footers are associated with those sections. Open the document with WordProcessingLoadOptions and pass a Word-specific watermark option to add() to target a particular section or page.

Add a watermark to a particular section

Use WordProcessingWatermarkSectionOptions and set section_index to place a watermark in a specific section.

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, Font, Color
from groupdocs.watermark.common import HorizontalAlignment, VerticalAlignment
from groupdocs.watermark.options.word_processing import (
    WordProcessingLoadOptions, WordProcessingWatermarkSectionOptions,
)

def add_watermark_to_section():
    with Watermarker("./document.docx", WordProcessingLoadOptions()) as watermarker:
        watermark = TextWatermark("CONFIDENTIAL", Font("Arial", 36.0))
        watermark.foreground_color = Color.red
        watermark.horizontal_alignment = HorizontalAlignment.CENTER
        watermark.vertical_alignment = VerticalAlignment.CENTER
        watermark.rotate_angle = 45.0

        options = WordProcessingWatermarkSectionOptions()
        options.section_index = 0
        watermarker.add(watermark, options)

        watermarker.save("./output.docx")

if __name__ == "__main__":
    add_watermark_to_section()

document.docx is the sample file used in this example. Click here to download it.

Binary file (DOCX, 13 KB)

Download full output

To watermark only specific pages, use WordProcessingWatermarkPagesOptions and set page_numbers.

Working with existing content

Watermarker.get_content() returns a WordProcessingContent that exposes the document’s sections, their shapes, and their headers and footers. For example, to add a watermark to every image inside the first section:

from groupdocs.watermark import Watermarker
from groupdocs.watermark.watermarks import TextWatermark, Font
from groupdocs.watermark.options.word_processing import WordProcessingLoadOptions

with Watermarker("./document.docx", WordProcessingLoadOptions()) as watermarker:
    watermark = TextWatermark("Protected image", Font("Arial", 8.0))
    content = watermarker.get_content()
    for image in content.sections[0].find_images():
        image.add(watermark)
    watermarker.save("./output.docx")

For the full set of Word content operations — shape effects, locking, protection, and working with existing shapes and headers/footers — see the dedicated topics:

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.