How to fit + center document in view


I want to fit my document in my view, with the following constraints:

  • If the document is smaller than the view, the maximum scale must be 1.0 and the document must be centered in the view.

If you can give me the code, I think it will be usefull for other users.



The easiest thing to do is to pretend there is a sheet in the view, even if you haven’t already set GoView.BackgroundHasSheet to true because you don’t want to display any sheet:
goView1.SheetStyle = GoViewSheetStyle.Sheet;
This will disable the normal behavior of GoView.LimitDocPosition, so it will be possible to scroll beyond the extent defined by the values of GoView.DocumentTopLeft and GoView.DocumentSize.
Then when you want to center things, call:
public static void CenterDocumentInView(GoView v) { // if GoView.SheetStyle == GoViewSheetStyle.Sheet
Size dispSize = v.DisplayRectangle.Size;
RectangleF b = v.ComputeDocumentBounds();
PointF c = new PointF(b.X+b.Width/2, b.Y+b.Height/2);
float s = 1;
if (b.Width > 0 && b.Height > 0)
s = Math.Min((dispSize.Width / b.Width), (dispSize.Height / b.Height));
if (s > 1) s = 1;
v.RescaleWithCenter(s, c);

Well… I do not understand all… but works perfectly!

Thanks Walter