Getting the Absolute Position of Anchored Objects
TX Text Control offers multiple Add overloads in the FrameBase class for positioning TextFrames, Images, BarcodeFrames, and ChartFrames. Anchored objects return relative coordinates, so calculating absolute position requires combining the TextChar bounds with the ScrollLocation offset.

TextFrames, Images, BarcodeFrames and ChartFrames provide different ways to position those objects in the document. All objects are positioned in the same way as they are inherited from the base class FrameBase.
Based on TextFrames, let's have a look at the various positioning options:
public bool Add(TextFrame textFrame, int textPosition);
public bool Add(TextFrame textFrame,
int page,
System.Drawing.Point location,
TextFrameInsertionMode insertionMode);
public bool Add(TextFrame textFrame,
HorizontalAlignment alignment,
int textPosition,
TextFrameInsertionMode insertionMode);
public bool Add(TextFrame textFrame,
System.Drawing.Point location,
int textPosition,
TextFrameInsertionMode insertionMode);
The first overload inserts a TextFrame at a specific text position and the absolute location can be easily obtained using the TextChar class or the InputPositon. The second method inserts the TextFrame at a fixed position on a page and the Location property of the TextFrame returns the absolute position automatically.
Getting the absolute position of TextFrames that are inserted using the last two methods is a little tricky, but easily solvable with TextControl's flexible API. The following method returns the absolute position of a TextFrame that is anchored to a text position.
private Rectangle GetAbsolutePositionOfAnchoredTextFrame(TextFrame TextFrame)
{
Graphics g = this.CreateGraphics();
int iDPIFactor = (int)(1440 / g.DpiX);
Rectangle rectAbsolutePosition = new Rectangle();
if (textControl1.TextChars[frame.TextPosition] == null)
throw new Exception("Text position doesn\'t exist");
Rectangle rectAnchorOffset =
textControl1.TextChars[frame.TextPosition].Bounds;
rectAbsolutePosition.X = (frame.Location.X +
rectAnchorOffset.X -
textControl1.ScrollLocation.X) / iDPIFactor;
rectAbsolutePosition.Y = (frame.Location.Y +
rectAnchorOffset.Y -
textControl1.ScrollLocation.Y) / iDPIFactor;
rectAbsolutePosition.Width = frame.Size.Width / iDPIFactor;
rectAbsolutePosition.Height = frame.Size.Height / iDPIFactor;
return rectAbsolutePosition;
}
Anchored objects return the relative Location to the anchor position. This location of the anchor position can be obtained using the TextChar class:
Rectangle rectAnchorOffset =
textControl1.TextChars[frame.TextPosition].Bounds;
Additionally, the current ScrollLocation must be included in the calculation. The iDPIFactor is used to convert Twips to the required 1/100 inch that are used in .NET Windows Forms.
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.
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…
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.
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.
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.
