Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

Inserting Watermark Images to All Pages Dynamically

This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This property is used to tag the inserted watermark images in order to find them in the ImageCollection. In a first step, all watermark images are removed. In order to remove the specific images, all tagged images are collected in a List and finally removed from the ImageCollection: After that, on each page,…

Inserting Watermark Images to All Pages Dynamically

This sample project shows how to create and insert a watermark image on all pages dynamically. Image objects have the Name property to store additional string information with an image. This property is used to tag the inserted watermark images in order to find them in the ImageCollection.

In a first step, all watermark images are removed. In order to remove the specific images, all tagged images are collected in a List and finally removed from the ImageCollection:

private void RemoveWatermarkImages(TXTextControl.TextControl textControl)
{
    // List of images to be deleted
    List<TXTextControl.Image> lImagesToRemove = 
        new List<TXTextControl.Image>();

    // loop through all images and check for the Name
    foreach (TXTextControl.Image img in textControl1.Images)
    {
        // add watermark images to the List
        if (img.Name == "Watermark")
            lImagesToRemove.Add(img);
    }

    // remove all tagged watermark images
    foreach(TXTextControl.Image img in lImagesToRemove)
    {
        textControl1.Images.Remove(img);
    }
}

After that, on each page, a new watermark image is inserted at a fixed position, vertically and horizontally centered. The Name property is set to "Watermark" and the image won't be sizable and moveable.

private void InsertWatermarkImages()
{
    // remove all existing watermarks
    RemoveWatermarkImages(textControl1);

    foreach (TXTextControl.Page page in textControl1.GetPages())
    {
        // create a new watermark image
        Bitmap bmp = CreateDraftImage();

        // create a new TXTextControl.Image object and mark
        // as watermark
        TXTextControl.Image img = new TXTextControl.Image(bmp);
        img.Name = "Watermark";
        img.Sizeable = false;
        img.Moveable = false;

        // calculate the location to center the image
        Point pImageLocation = new Point(
            (page.Bounds.Width / 2) - (PixelToTwips(bmp.Size.Width)) / 2,
            (page.Bounds.Height / 2) - (PixelToTwips(bmp.Size.Height)) / 2);

        // add the image to the page
        textControl1.Images.Add(
            img,
            page.Number,
            pImageLocation,
            TXTextControl.ImageInsertionMode.BelowTheText);
    }
}

On the TextControl Changed event, a simple condition checks whether the current number of pages has been increased. If yes, a new watermark is inserted:

private int iNumberOfPages = 1;

// update the watermarks if the number of pages are different
private void textControl1_Changed(object sender, EventArgs e)
{
    if (textControl1.Pages > iNumberOfPages)
    {
        InsertWatermarkImages();
        iNumberOfPages = textControl1.Pages;
    }
}

To complete the code description: The method CreateDraftImage returns a Bitmap with the gray text "DRAFT" that is used as a watermark image.

// creates a new "DRAFT" sample image
private Bitmap CreateDraftImage()
{
    string sText = "DRAFT";

    Bitmap destination = new Bitmap(400, 400);
    using (Graphics g = Graphics.FromImage(destination))
    {
        GraphicsUnit units = GraphicsUnit.Pixel;

        g.Clear(Color.White);
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;

        g.DrawString(sText, new Font(
            "Arial", 
            90, 
            FontStyle.Bold,
            GraphicsUnit.Pixel), 
            new SolidBrush(
                Color.FromArgb(64, Color.Black)),
                destination.GetBounds(ref units),
                stringFormat);
    }

    return destination;
}

The following screenshot shows the watermarks in the TX Text Control .NET for Windows Forms:

Inserting watermark images to all pages dynamically

Download the sample from GitHub and test it on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2012 or better
  • TX Text Control .NET for Windows Forms (trial sufficient)

Reporting

The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.

See Reporting products

Related Posts

ReportingWindows FormsGitHub

Windows Forms: Printing Multiple Pages Per Sheet

This sample project implements the class MultipagePrintDocument that inherits from System.Drawing.Printing.PrintDocument to print multiple pages of a document per sheet. The constructor of…


ReportingWindows FormsGitHub

MailMerge: Conditional INCLUDETEXT Fields

In documents and reports, often sentences or an appendix are added only under specific conditions. The Text Control reporting engine MailMerge provides the concept of INCLUDETEXT fields to include…


ASP.NETReportingGitHub

Merging Documents with RESTful Web API's

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for…


ReportingWindows FormsTutorial

Reporting: Sorting Merge Block Rows by Column Name

The concept of Text Control Reporting is to accept pre-shaped data. That means that Text Control's MailMerge component is merging the data rows 'as-is' and data shaping, sorting or queries are…


ReportingWindows FormsGitHub

Reporting: Merging MS Word Documents with DocVariables

TX Text Control supports all MS Word fields including the typical merge fields, content control fields, legacy form fields or special fields such as DOCVARIABLE. All of those fields can be found…