TX Text Control - Fit to Page

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;
}

The sample project can be downloaded here: tx_view_modes.zip. At minimum, Visual Studio 2008 and a trial version of TX Text Control .NET for Windows Forms 17.0 is required.