Products Technologies Demo Docs Blog Support Company

Inserting Background Images into TX Text Control

Background images can be easily added to pages in TX Text Control by placing the images behind the text. Thanks to flexible methods and insertion mode options, you can simply loop through all pages in order to insert an image at a fixed position on the current page behind the text. In order to center the image horizontally, the image width must be subtracted from the page width divided by 2 to get the x value for the location. The vertical value is calculated analogically with the horizontal…

Inserting Background Images into TX Text Control

Background images can be easily added to pages in TX Text Control by placing the images behind the text. Thanks to flexible methods and insertion mode options, you can simply loop through all pages in order to insert an image at a fixed position on the current page behind the text.

Inserting background images

In order to center the image horizontally, the image width must be subtracted from the page width divided by 2 to get the x value for the location. The vertical value is calculated analogically with the horizontal value.

// factor to convert 1/100 inch to twips
int dpix = (int)(1440 / textControl1.CreateGraphics().DpiX);

// loop through all pages
foreach (TXTextControl.Page page in textControl1.GetPages())
{
    System.Drawing.Image img = Image.FromFile("bgimage.png");

    // calculate the position to center the image on the current page
    int xLocation =
        (int)(((textControl1.Sections[page.Section].Format.PageSize.Width
        - img.Size.Width) * dpix) / 2);

    int yLocation =
        (int)(((textControl1.Sections[page.Section].Format.PageSize.Height
        - img.Size.Height) * dpix) / 2);

    TXTextControl.Image TXimg = new TXTextControl.Image(img);

    // add the image to the page
    textControl1.Images.Add(
        TXimg,
        page.Number,
        new Point(xLocation, yLocation),
        TXTextControl.ImageInsertionMode.FixedOnPage |
            TXTextControl.ImageInsertionMode.BelowTheText);
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsGetting StartedTutorial

Windows Forms Tutorial: Create Your First Windows Forms C# Application

This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.


ASP.NETASP.NET CoreMailMerge

How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…


AngularDocument EditorNode.js

Creating an Angular Document Editor Application with a Node.js WebSocket Server

This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.


ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.