How to add watermark to doc's every page

How to add text as watermark in every page?I try it, but some pages don’t have watermark.
code:

doc = aw.Document(str(file_path))
text_watermark_options = aw.TextWatermarkOptions()
text_watermark_options.font_family = 'arial'
text_watermark_options.font_size = 30
text_watermark_options.color = aspose.pydrawing.Color.blue
text_watermark_options.layout = aw.WatermarkLayout.DIAGONAL
text_watermark_options.is_semitrasparent = False
doc.watermark.set_text(watermark, text_watermark_options)
doc.save(str(new_file_path))

watermark is chinese

@David_Matin

To add a text watermark to every page of a Word document using Aspose.Words, follow these steps to ensure that the watermark appears consistently across all pages:

Cause
If some pages are missing the watermark, it may be due to the headers and footers not being linked correctly across sections in the document.

Solution
You need to insert the watermark in the headers of each section and ensure that all headers and footers are linked to the previous section. This way, the watermark will be replicated across all pages.

Code
Here’s a sample code snippet to add a text watermark to every page:

using Aspose.Words;
using Aspose.Words.Drawing;

public void InsertWatermarkText(Document doc, string watermarkText)
{
    foreach (Section section in doc.Sections)
    {
        // Create a watermark shape
        Shape watermark = new Shape(doc, ShapeType.TextPlainText);
        watermark.TextPath.Text = watermarkText;
        watermark.TextPath.FontFamily = "Arial";
        watermark.Width = 500;
        watermark.Height = 100;
        watermark.Rotation = -40;
        watermark.Fill.Color = Color.Gray;
        watermark.StrokeColor = Color.Empty;

        // Set the watermark to be behind the text
        watermark.WrapType = WrapType.None;
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        // Add the watermark to the header of the section
        section.HeadersFooters.HeaderFooterType.HeaderPrimary.AddChild(watermark);
    }
    
    // Link all headers and footers to the previous section
    foreach (Section section in doc.Sections)
    {
        section.HeadersFooters.LinkToPrevious(true);
    }
}

// Usage
Document doc = new Document("YourDocument.docx");
InsertWatermarkText(doc, "Your Watermark Text");
doc.Save("WatermarkedDocument.docx");

Next Steps

  1. Ensure that your document has multiple sections if needed.
  2. Run the code to insert the watermark and save the document.
  3. Open the saved document to verify that the watermark appears on every page.

This approach will ensure that your watermark is consistently applied throughout the document. If you have any further questions or need assistance, feel free to ask!

It’s the image in the document that’s blocking the watermark. Is there any way to get the watermark at the top there?

@David_Matin Could you please attach your problematic input and output documents here for our reference? We will check the issue and provide you more information.

@alexey.noskov Anthoer question,How can I make the watermark appear on the top of the image instead of the bottom. Right now pages with images have the watermark overwritten

@David_Matin You can try using the following code to place a watermark at the top of the page:

doc = aw.Document("input.docx")

# Determine the maximum ZOrder of shapes in the document.
z_order = 0
shapes = doc.get_child_nodes(aw.NodeType.SHAPE, True)
for shape in shapes:
    z_order = max(z_order, shape.as_shape().z_order)
z_order += 1  # Increment ZOrder for the new watermark

# Create a watermark shape.
watermark = aw.drawing.Shape(doc, aw.drawing.ShapeType.TEXT_PLAIN_TEXT)
watermark.name = "WaterMark"

# Set up the text of the watermark
watermark.text_path.text = "In Front Watermark"
watermark.text_path.bold = True
watermark.text_path.size = 100
watermark.text_path.font_family = "Arial"
watermark.width = 500
watermark.height = 100
watermark.fill.fore_color = aspose.pydrawing.Color.light_gray
watermark.stroke_color = aspose.pydrawing.Color.light_gray

# Text will be directed from the bottom-left to the top-right corner.
watermark.rotation = 45

# Place the watermark in the page center.
watermark.relative_horizontal_position = aw.drawing.RelativeHorizontalPosition.PAGE
watermark.relative_vertical_position = aw.drawing.RelativeVerticalPosition.PAGE
watermark.wrap_type = aw.drawing.WrapType.NONE
watermark.horizontal_alignment = aw.drawing.HorizontalAlignment.CENTER
watermark.vertical_alignment = aw.drawing.VerticalAlignment.CENTER
watermark.z_order = z_order

# Get paragraphs in the document.
paragraphs = doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)

layout_collector = aw.layout.LayoutCollector(doc)
page_to_insert = 1
for para in paragraphs:
    para = para.as_paragraph()
    # Process only paragraphs in the main body.
    if para.get_ancestor(aw.NodeType.BODY) is None:
        continue

    para_page = layout_collector.get_end_page_index(para)
    if para_page == page_to_insert:
        page_to_insert += 1
        para.append_child(watermark.clone(True))

# Since we have used LayoutCollector, Aspose.Words cached layout of the document
# and the modifications we have made are not in the cache.
# Update page layout to reflect the changes in the output fixed Page formats.
doc.update_page_layout()
doc.save("output.pdf")

@vyacheslav.deryushev Thank u for help.I need to add watermark in docx, not pdf.And I tried u code,and saved as docx,but watermark still under image.

@David_Matin Could you please provide the problematic file?

watermark file:
new_副本v1.6.4.docx (9.4 MB)
original file:
副本v1.6.4.docx (9.4 MB)

@David_Matin You are adding a watermark inside the header, in this case there is no option to move the shape above the image. Please check the above code How to add watermark to doc's every page - #6 by vyacheslav.deryushev

Your document has continuation tables on some pages, and unfortunately in this case it is difficult to determine the correct place for the watermark.

@vyacheslav.deryushev The screenshot above is what I generated using the code you replied to. Except I saved it as a docx instead of a pdf.

@David_Matin Here is the output file produced with the code:

output.docx (9.4 MB)