Fitting width/height/page when PDFViewCtrl has pages of different size

Q: When viewing a document with different sized pages, then SetPageViewMode works based on the largest page, as opposed to the current page. Is there a away to change this?

try
{
Current_View.DocLockRead();
int cp = Current_View.GetCurrentPage();
int margin = 2;

// Fit Page
pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
pageRect.Inflate(pageSpacing);
Current_View.ShowRect(cp, margin);
Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);

// Fit Width
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//double heightRatio = Current_View.ActualHeight / Current_View.ActualWidth;
//double visiblePageHeight = heightRatio * pageRect.Width();
//if (visiblePageHeight < pageRect.Height())
//{
// pageRect.y1 = pageRect.y2 - visiblePageHeight;
//}
//pageRect.Inflate(margin);
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
//Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);

// Fit Height
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//double widthRatio = Current_View.ActualWidth / Current_View.ActualHeight;
//double visiblePageWidth = widthRatio * pageRect.Height();
//if (visiblePageWidth < pageRect.Width())
//{
// pageRect.x2 = pageRect.x1 + visiblePageWidth;
//}
//pageRect.Inflate(margin);
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
//Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);
}
catch
{

}
finally
{
Current_View.DocUnlockRead();
}

Margin is just an arbitrary number to add a little bit of space around the page. You can experiment with this as you wish.

If you want to replicate SetPageViewMode more closely (that is, you don’t want to go to the top left corner of the page, but just change the zoom to fit width/height/page, you can use SetZoom together with the ratio of Page rectangle to Viewer rectangle. Like so:

try
{
Current_View.DocLockRead();
int cp = Current_View.GetCurrentPage();
pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
pdftron.PDF.Rect viewerRect = new pdftron.PDF.Rect(0, 0, Current_View.ActualWidth, Current_View.ActualHeight);
viewerRect.Inflate(-5); // this should corrspond to the page spacing (e.g. PDFViewWpf.SetPageSpacing(x, y, xPaf, yPad).
// In this case, I used PDFViewWpf.SetPageSpacing(5, 5, 5, 5)
double zoom = Current_View.GetZoom();
double hRatio = viewerRect.Width() / pageRect.Width();
double vRatio = viewerRect.Height() / pageRect.Height();
double minRatio = Math.Min(vRatio, hRatio);
Current_View.SetZoom(minRatio);
}
catch
{

}
finally
{
Current_View.DocUnlockRead();
}

Instead of using the minRatio, you can choose the vRatio or hRatio to fit height or width.
A: You can show the current page in a fitting mode using ShowRect as follows:

Hello Thomas,

i have a problem to implement your code above for the possibility that the page is rotate to 90 or 270 degrees. Then the width and height are interchanged. When i check the rotate state and interchanged the width and height property of pageRect then the fit to with code works for rotate pages like the fit to height/fit to page code. How can i changed the first code example to regard rotate pages?

Best regards
Daniel
Am Dienstag, 18. November 2014 19:01:10 UTC+1 schrieb Tomas Hofmann:

Q: When viewing a document with different sized pages, then SetPageViewMode works based on the largest page, as opposed to the current page. Is there a away to change this?

A: You can show the current page in a fitting mode using ShowRect as follows:

try
{
Current_View.DocLockRead();
int cp = Current_View.GetCurrentPage();
int margin = 2;

// Fit Page
pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
pageRect.Inflate(pageSpacing);
Current_View.ShowRect(cp, margin);
Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);

// Fit Width
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//double heightRatio = Current_View.ActualHeight / Current_View.ActualWidth;
//double visiblePageHeight = heightRatio * pageRect.Width();
//if (visiblePageHeight < pageRect.Height())
//{
// pageRect.y1 = pageRect.y2 - visiblePageHeight;
//}
//pageRect.Inflate(margin);
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
//Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);

// Fit Height
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//double widthRatio = Current_View.ActualWidth / Current_View.ActualHeight;
//double visiblePageWidth = widthRatio * pageRect.Height();
//if (visiblePageWidth < pageRect.Width())
//{
// pageRect.x2 = pageRect.x1 + visiblePageWidth;
//}
//pageRect.Inflate(margin);
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetVScrollPos(Current_View.GetVScrollPos() - margin);
//Current_View.SetHScrollPos(Current_View.GetHScrollPos() - margin);
}
catch
{

}
finally
{
Current_View.DocUnlockRead();
}

Margin is just an arbitrary number to add a little bit of space around the page. You can experiment with this as you wish.

If you want to replicate SetPageViewMode more closely (that is, you don’t want to go to the top left corner of the page, but just change the zoom to fit width/height/page, you can use SetZoom together with the ratio of Page rectangle to Viewer rectangle. Like so:

try
{
Current_View.DocLockRead();
int cp = Current_View.GetCurrentPage();
pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
pdftron.PDF.Rect viewerRect = new pdftron.PDF.Rect(0, 0, Current_View.ActualWidth, Current_View.ActualHeight);
viewerRect.Inflate(-5); // this should corrspond to the page spacing (e.g. PDFViewWpf.SetPageSpacing(x, y, xPaf, yPad).
// In this case, I used PDFViewWpf.SetPageSpacing(5, 5, 5, 5)
double zoom = Current_View.GetZoom();
double hRatio = viewerRect.Width() / pageRect.Width();
double vRatio = viewerRect.Height() / pageRect.Height();
double minRatio = Math.Min(vRatio, hRatio);
Current_View.SetZoom(minRatio);
}
catch
{

}
finally
{
Current_View.DocUnlockRead();
}

Instead of using the minRatio, you can choose the vRatio or hRatio to fit height or width.

Hi Daniel,

I have done some new calculations to include page rotation. I also think I devices a smarter way of making sure the horizontal scroll bar isn’t there.

try
{
Current_View.DocLockRead();
int cp = Current_View.GetCurrentPage();
pdftron.PDF.Page.Rotate currentRotation = Current_View.GetRotation();

int extraWidth = 5; // this “magic” number is 2 more than the padding at the side of pages passed in to the PDFViewWPF.SetPageSpacing (to allow for the borders around pages).
double newWidth = (Current_View.ActualWidth - (2 * extraWidth)); // This is the width the content should have in order to make room for padding
// note, you might want to do the same thing with height if you’re in a non-continuous mode, event use the smallest of the with/height ratios for zooming

// Fit Page
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetZoom(Current_View.GetZoom() * (newWidth / Current_View.ActualWidth)); // sets the zoom to fit the viewer’s padding, (hence, no scrollbar in non-facing modes)

// Fit Width
//pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
//pageRect.Normalize();
//double heightRatio = Current_View.ActualHeight / Current_View.ActualWidth;
//double width = pageRect.Width();
//double height = pageRect.Height();
//if (currentRotation == pdftron.PDF.Page.Rotate.e_90 || currentRotation == pdftron.PDF.Page.Rotate.e_270)
//{
// double temp = width;
// width = height;
// height = temp;
//}
//double visiblePageHeight = heightRatio * width;
//if (visiblePageHeight < height)
//{
// switch (currentRotation)
// {
// case pdftron.PDF.Page.Rotate.e_90:
// pageRect.x2 = visiblePageHeight;
// break;
// case pdftron.PDF.Page.Rotate.e_180:
// pageRect.y2 = visiblePageHeight;
// break;
// case pdftron.PDF.Page.Rotate.e_270:
// pageRect.x1 = pageRect.x2 - visiblePageHeight;
// break;
// default:
// pageRect.y1 = pageRect.y2 - visiblePageHeight;
// break;
// }
//}
//Current_View.ShowRect(cp, pageRect);
//Current_View.SetZoom(Current_View.GetZoom() * (newWidth / Current_View.ActualWidth));

// Fit Height
pdftron.PDF.Rect pageRect = _pdfdoc.GetPage(cp).GetCropBox();
double widthRatio = Current_View.ActualWidth / Current_View.ActualHeight;
double width = pageRect.Width();
double height = pageRect.Height();
if (currentRotation == pdftron.PDF.Page.Rotate.e_90 || currentRotation == pdftron.PDF.Page.Rotate.e_270)
{
double temp = width;
width = height;
height = temp;
}
double visiblePageWidth = widthRatio * height;
if (visiblePageWidth < width)
{
switch (currentRotation)
{
case pdftron.PDF.Page.Rotate.e_90:
pageRect.y2 = visiblePageWidth;
break;
case pdftron.PDF.Page.Rotate.e_180:
pageRect.x1 = pageRect.x2 - visiblePageWidth;
break;
case pdftron.PDF.Page.Rotate.e_270:
pageRect.y1 = pageRect.y2 - visiblePageWidth;
break;
default:
pageRect.x2 = visiblePageWidth;
break;
}
}
if (visiblePageWidth < pageRect.Width())
{
pageRect.x2 = pageRect.x1 + visiblePageWidth;
}
Current_View.ShowRect(cp, pageRect);
Current_View.SetZoom(Current_View.GetZoom() * (newWidth / Current_View.ActualWidth));
}
catch
{

}
finally
{
Current_View.DocUnlockRead();
}

Best Regards,
Tomas Hofmann