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: