'Fit to Page' and 'Fit to Width' Using TX Text Control .NET
In the last couple of years, the monitor screen resolutions increased significantly and 24" and larger is pretty standard nowadays. On these larger screens, it makes sense to scale the page to the width of the container or to fit a whole page into the visible area to use the complete space of the screen. TX Text Control provides the ZoomFactor property that sets the zoom factor, in percent, for a TextControl. The following method fits the current page to fill it's container: private void…


In the last couple of years, the monitor screen resolutions increased significantly and 24" and larger is pretty standard nowadays.
On these larger screens, it makes sense to scale the page to the width of the container or to fit a whole page into the visible area to use the complete space of the screen.
TX Text Control provides the ZoomFactor property that sets the zoom factor, in percent, for a TextControl.
The following method fits the current page to fill it's container:
private void FitToWindow()
{
textControl1.PageUnit = MeasuringUnit.Twips;
int iVisibleGap = 65;
// get resolution to calculate convert twips 1/100 inch
Graphics g = textControl1.CreateGraphics();
int iTwipsPerPixel = (int)(1440 / g.DpiX);
SectionFormat currentSection = textControl1.Sections.GetItem().Format;
double widthZoom = 100 * textControl1.Width /
((currentSection.PageSize.Width / iTwipsPerPixel)
+ iVisibleGap);
double heightZoom = 100 * textControl1.Height /
((currentSection.PageSize.Height / iTwipsPerPixel)
+ iVisibleGap);
if (widthZoom < heightZoom)
textControl1.ZoomFactor = (int)widthZoom;
else
textControl1.ZoomFactor = (int)heightZoom;
}
The next method shows how to set the zoom factor to fill the whole width of the container:
private void FitToWidth()
{
textControl1.PageUnit = MeasuringUnit.Twips;
int iVisibleGap = 200;
// get resolution to calculate convert twips 1/100 inch
Graphics g = textControl1.CreateGraphics();
int iTwipsPerPixel = (int)(1440 / g.DpiX);
SectionFormat currentSection = textControl1.Sections.GetItem().Format;
double widthZoom = 100 * textControl1.Width /
((currentSection.PageSize.Width / iTwipsPerPixel) + iVisibleGap);
textControl1.ZoomFactor = (int)widthZoom;
}
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2008 or better
- TX Text Control .NET Server (trial sufficient)
Related Posts
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
ASP.NETJavaScriptDocument Editor
Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More
This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.