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

GitHub

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 for ASP.NET (trial sufficient)