Aspose.Total Net Adding image as watermark to document files

Hi
We have purchased Aspose.Total license recently. We are trying to add image as watermark to all pages in doc. But we want to add watermark to full page covering all areas of page. We tried adding it but looks like stretched image. I am attaching files for your references.
watermarked_test.docx (23.9 KB)

test.docx (23.6 KB)

Can you please help us?
Thanks
Krushna

@krushna.barhate

Cause

The issue arises because the image you are using as a watermark is being stretched to fit the page dimensions, resulting in distortion. To ensure that the watermark covers the entire page without stretching, you need to set the width and height of the watermark to match the page dimensions accurately.

Solution

To resolve this, you can use the ImageStamp class in Aspose.Words or Aspose.PDF, depending on your document type. Here’s how to set the watermark to cover the full page:

  1. Set the watermark’s position to the bottom-left corner of the page.
  2. Adjust the width and height of the watermark to match the dimensions of the page.
  3. Optionally, set the opacity and rotation properties to enhance the appearance.

Code

Here’s a sample code snippet in C# that demonstrates how to add a full-page image watermark to a document:

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

// Load the document
Document doc = new Document("test.docx");

// Create an image stamp
ImageStamp stamp = new ImageStamp("watermark.png");

// Set the position and size to cover the entire page
foreach (Page page in doc.GetChildNodes(NodeType.Page, true))
{
    stamp.SetOrigin(0, 0); // Set the origin to the bottom-left corner
    stamp.Width = page.PageInfo.Width; // Set width to page width
    stamp.Height = page.PageInfo.Height; // Set height to page height
    stamp.Background = true; // Set as background
    page.AddStamp(stamp); // Add the watermark to the page
}

// Save the document
doc.Save("watermarked_test.docx");

Next Steps

  1. Replace "test.docx" and "watermark.png" with the actual paths to your document and watermark image.
  2. Adjust the Opacity and Rotate properties of the ImageStamp as needed to fit your design requirements.
  3. Test the output to ensure the watermark appears as desired across all pages.

If you have any further questions or need additional assistance, please feel free to ask!

@krushna.barhate The following code generates multiple watermarks on the page:

Document doc = new Document("input.docx");

WatermarkHelper.InsertWatermarkText(doc, "Watermark");

doc.Save("output.docx");

public static class WatermarkHelper
{
    private const int WATERMARK_FONT_WIDTH = 30;
    private const int WATERMARK_FONT_HEIGHT = 30;
    private const int WATERMARK_FONT_ROTATION = -45;
    private static readonly Color watermark_color = Color.FromArgb(128, 128, 128, 128);

    public static void InsertWatermarkText(Document doc, string watermarkText)
    {
        if (string.IsNullOrWhiteSpace(watermarkText))
        {
            throw new Exception("Watermark text is not configured, cannot add watermark to document");
        }

        foreach (Section section in doc.Sections)
        {
            InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderPrimary);
            InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderFirst);
            InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderEven);
        }
    }

    private static Shape BuildTextShape(Document doc, string watermarkText, double left, double top)
    {
        Shape watermark = new Shape(doc, ShapeType.TextPlainText)
        {
            TextPath = {
            Text = watermarkText,
            FontFamily = "Arial"
        },
            Width = watermarkText.Length * WATERMARK_FONT_WIDTH,
            Height = WATERMARK_FONT_HEIGHT * 3,
            Rotation = WATERMARK_FONT_ROTATION,
            Fill = { Color = watermark_color },
            StrokeColor = watermark_color,
            Left = left,
            Top = top,
            WrapType = WrapType.None,
            BehindText = true
        };
        return watermark;
    }

    private static void InsertWatermarkIntoHeader(Document doc, string watermarkText, Section section, HeaderFooterType headerType)
    {
        HeaderFooter header = section.HeadersFooters[headerType];
        if (header == null)
        {
            header = new HeaderFooter(section.Document, headerType);
            section.HeadersFooters.Add(header);
        }

        Random random = new Random();
        Shape waterShape;
        int beginLeft = -320 - random.Next(50);

        for (int top = 50; top < 800; top += 250)
        {
            for (int left = beginLeft; left < 700; left += 300)
            {
                if (left == beginLeft)
                {
                    int splitNo = new Random().Next(0, watermarkText.Length);
                    string firstWater = watermarkText.Substring(splitNo) + new string(' ', splitNo);
                    waterShape = BuildTextShape(doc, firstWater, left, top);
                }
                else
                {
                    waterShape = BuildTextShape(doc, watermarkText, left, top);
                }

                Paragraph headerPara = (Paragraph)header.GetChild(NodeType.Paragraph, 0, true);
                if (headerPara == null)
                {
                    Paragraph watermarkPara = new Paragraph(doc);
                    watermarkPara.AppendChild(waterShape);
                    header.AppendChild(watermarkPara.Clone(true));
                }
                else
                {
                    headerPara.AppendChild(waterShape);
                }
            }
        }
    }
}